Keyboard sensor firing my script twice

First, the blend: Journal.blend (154 KB)

If you play the game, then hit J, then stop the game, you can see in the console that the script fires twice. The first pass “opens the journal” (that the scene doesn’t actually change is my next problem, but if you can help me on that too, great!) and prints that it’s doing so, then it runs a second pass for some reason, automatically closing it.

This behavior is observed with the Tap modifier enabled or disabled, with the frames between pulses set to several seconds’ delay, with the True/False/Both pulse modes enabled, etc. The only time it fires once is when the sensor is changed to Always (with no Tap).

I hate asking for the really simple things like this because it seems like I should just be able to RTM…but it takes a month for the GameKit 2 book to get from the EU to here, I guess, so does anyone have some insight on why it’s doing that, and what I can do to have a key press only fire the script once?

I made an example blend.

Download it for 2.49:
>> tagscript.blend

It demonstrates how you can use sensor.getKeyStatus() to achieve victory.

Ah, thank you! Digging through the documentation for functions that I’m not even sure exist is tedious at best. Thanks for showing me how to do this.

A more full explanation:

Most Blender sensors fire both a true pulse (when activated) and a false pulse (when deactivated). Python scripts (and sometimes expression controllers… maybe NAND and NOR controllers too?) will activate on both true and false pulses… and unless your script knows what to do with each kind of pulse, it will do the same thing for both times.

You can also use the very simple sensor.positive variable. For example:

This code will print 2 yays when connected to a keyboard sensor.

print "yay"

And this code will only print one:

sens = GameLogic.getCurrentController().sensors['sensor']
if sens.positive:
     print "yay"

Hope that helps!


Ah, I see. Thanks for the info, that’ll save me a lot of headaches later, I’m sure : )