4
$\begingroup$

I have this script with the module collision which is called when the player collides with any of two cylinders in the scene, the module increments the counter in a text object that in a separate scene that overlays the main scene

import bge

scene = bge.logic.getCurrentScene()

dynamicText = scene.objects['Text']

def collision():
    dynamicText.text = str(int(dynamicText.text)+1)

This the logic behind the cylinders that triggers the collision and send the message to the text object to increment the counter.

enter image description here

This is the login in the the text object

enter image description here

The problem is that the counter is incremented twice per collision, and I don’t see an obvious way to filter the second time the module is called.

This is the blend file.

$\endgroup$

2 Answers 2

5
$\begingroup$

If I remember correctly, collision sensors fire twice by design. Once when set True, and another when False.

This isn't a problem when connected to an And or Not controller (because they check) but in a script you'll have to do something like this:

def collision(cont):
    # Loops through all connected sensors and returns if one is False
    # Basically makes it work like an And conroller.
    for sens in cont.sensors:
        if not sens.positive:
            return
    ...
$\endgroup$
1
  • $\begingroup$ Also this helped me to figure out a way to test only the sensor that receives the message sent by the colliding cylinder. $\endgroup$
    – rraallvv
    Commented Nov 18, 2014 at 23:59
1
$\begingroup$

I don't know why message pulses twice but

I changed the logic on Text to message> property "collideCount" add 1

Then added an always sensor true level triggering at frequency 3 connected to script

import bge    
scene = bge.logic.getCurrentScene()

dynamicText = scene.objects['Text']

def main():
    dynamicText.text = str(dynamicText["collideCount"])
main()

new logic http://ball2000.net/blender/images/messageBounce.png

$\endgroup$
3
  • $\begingroup$ I see what you mean, the first time message is triggered it adds 1 to collideCount, the second time it copies the value to text, also adding 0.5 each time messageis called could work too. $\endgroup$
    – rraallvv
    Commented Nov 18, 2014 at 3:16
  • $\begingroup$ A good a theory as any; I honestly don't know why it works! : ) $\endgroup$
    – ball2000
    Commented Nov 18, 2014 at 9:55
  • $\begingroup$ Prof Monster doesn't say anything about messages pulsing twice; and I couldn't get the double-count thing to happen by modifying the 'homework...' or 'ending' the message-sending object.. Just something weird about your blend I think... blenderartists.org/forum/showthread.php?249078 $\endgroup$
    – ball2000
    Commented Nov 18, 2014 at 20:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .