2
$\begingroup$

Got this strange little issue: I've got a separate module, besides variables such as the player object and animation functions there is also the boolean variable for the W key.

w_active = bge.logic.KX_INPUT_ACTIVE == bge.logic.keyboard.events[bge.events.WKEY]

Now when I import it from the module to another script and go like:

if w_active:
   run_animation()

It won't play it but when I don't use the variable and write the bge.logic.KX_INPUT_ACTIVE = bge.logic.keyboard.events(bge.events.WKEY) right after the if it does work.

I also checked it out with print(w_active) it seems the problem is that the boolean stays on False even when I press the W key.

$\endgroup$
2
$\begingroup$

This appears to because the module will only be executed once. Variables assigned directly inside the module will retain their initial value and won't be re-evaluated when accessed later.

To re-evaluate a variable, define it inside a function and call the function from wherever you want to access the current value of the variable.

For example, in the module you might create a function to return True if the W key is pressed:

import bge

def is_w_active():
    return(bge.logic.KX_INPUT_ACTIVE == bge.logic.keyboard.events[bge.events.WKEY])

Then when this is called from a script (or anywhere else) like so:

import bge, keyboardmodule

print(keyboardmodule.is_w_active())

it will return True or False accordingly.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.