My scripts running twice

Can anyone please tell me why this should happen I’m totally stuck. All of my scripts seem to run twice and I can’t figure out why.

http://www.tdsm.co.uk/blender/twice.blend

I’ve attached a link to a test file. If you press p to run it then hit the spacebar, it prints to the console twice. I even tried just attaching the keyboard sensor to the controller, skipping the update of my counter but I get the same result.

In my game I have a mouse over and left button sensor instead of just a keyboard sensor but I get the same result.

Am I just doing something stupid?

Thanks all!

The sensors provide the controllers with two pulses
a positive pulse when changing from negative to positive state and
a negative pulse when changing from positive to negative.
This means each controller will be triggered twice. The python script controller is also a controller. The script must decide if it want to handle the pulse or not.

In your case you get a positive pulse when pressing space. This triggers the actuator to add 1 to the property.

Your change sensor produces a true pulse as the property changes from 0 to 1. This let your script run.

If you release space you get a negative pulse. The AND controller does not forward this pulse to the actuator as the condition is false. the property remains unchanged.

The change sensor sends a negative pulse as the property does not change anymore. The script is runs again.

Ah. I see. So should I always be doing something like;


if GameLogic.getCurrentController().getSensor("sensor").inPositive() :
     #Do something

Thanks Monster!