Global variable naming problem


For some reason the code on below do not work:

Lets say code is connected to ‘i_ce’ named object.

cont = GameLogic.getCurrentController()
own = cont.owner


GameLogic.NAME = own.worldPosition

It creates global variable named as a ‘GameLogic.NAME’ instead of the GameLogic.OBi_ce’

How this should fix?

You should use the setattr() function:

#setattr(object, attribute, value)
setattr(GameLogic, NAME, own.worldPosition)

The code is not connected to the object, but to a controller owned by this object. This is a small but important difference. And it is the reason why you get your object by cont.owner.

Now to your script:


In script mode:
This creates a script variable NAME that you never use.
It lives as long as the script is executed. It dies with the end of the script run.
In module mode:
This creates a module attribute NAME.
It lives as long as the module lives (until the BGE is stopped).
Be aware in module mode this code is executed once at the first call to a function of this module.

GameLogic.NAME = own.worldPosition
This creates an attribute NAME for module GameLogic.
It lives as long as the <i>module GameLogic lives</i> (until the BGE is stopped).
So it works as it should be. There is nothing to fix.
Andrew got it :D
I hope it helps