How to initial Global Variable?

Uhmmmm i just want to know how to initialize a global variable that can be accessed by any object…

if it needs an object, how do i setup up one?

and also i want to initialize it ones not in every frame… but can be access and updated anytime by any object…

Thanks in advance.:smiley:

GameLogic.name =

Use an ALWAYS Actuator thats NOT set to True or False Pulse, so it will ONLY run once. Make sure its at the VERY TOP of your logic bricks and LINK it to a python script CONTROLLER.


+--------+                +--------------+
| Always |o--------------o| Python Script|
| [.][.] |                | [  Foo.py  ] |
+--------+                +--------------+


## Foo.py
import GameLogic

GameLogic.myvar = "something here"

thanks…

but is this the right way to access it?

if GameLogic.Life <= 0:
doDie()

Yes, but if the attribute has yet been created you will get an error. As a execept use this:

if hasattr(GameLogic, "life"):
    if GameLogic.life &lt;= 0:
    doDie()
else:
    GameLogic.life = "assignt my init value"

Notice I used a lowercase “l” and not a highercase “L”, some reason in the recent version it wont keep the varible :frowning:

Okay thanks a lot… I’ll keep that in mind! :):):slight_smile:

Notice I used a lowercase “l” and not a highercase “L”, some reason in the recent version it wont keep the varible

That’s a little scary. Anyway, it’s kind of a coding standard to start with a lower case with variables and functions, upper case with classes. Whoever did the Text property apparently wasn’t following it.