creating global variable?

I was wondering what the easiest way would be to complete the following task. I have a script that I’m running on a mouseover any event set to pulse so the script is called every time the mouse is over an object. Right now I’m grabbing the current object on collision, but I’m needing to store the current object in either an empty in the scene or a global variable so that it can be called accessed the next time the script is ran.

say there are two objects, object1 and object 2. The user hovers over object1 and the value is processed but not saved anywhere. When the user hovers over object2 the script needs to know to set the previous object to invisible so that it is no longer displayed…

any suggestions? also (and I know i’ll probably get yelled at for this) I’m still running blender 2.49 because this is what I started the project in (yeah back when 2.49 was current) and I’m trying to finish it out before updating…

A common place to store variables is globalDict:
http://www.blender.org/documentation/blender_python_api_2_63_13/bge.logic.html#bge.logic.globalDict

However it mentions that it does not work as expected for GameObjects. To work around that you could just store the name of the object. Then you can just get it back from the scene:


obj = bge.logic.getCurrentScene().objects[bge.logic.globalDict["previous"]]

Alternatively you can try saving the object on the controller owner:


own = bge.logic.getCurrentController().owner
own["previous"] = obj

globalDict is a dictionary, where as an object’s properties only have dictionary like access (they aren’t full dictionaries). Coincidentally this is how you access properties set in the UI. In case you are not already familiar with Python dictionaries, here is some documentation:
http://docs.python.org/py3k/tutorial/datastructures.html#dictionaries

Better store such information at the object that manages the MouseOverAny (Kupoman’s second suggestion).
It belongs to that game object only and should no be stored at an unrelated place.

I strongly suggest to avoid globalDict to store any references to game objects into it.
Reason: You can save globalDict to a file. But after loading all references will be invalid.

Worked like a charm. Greatly appreciated guys! I love how I’m finally getting to the point where I can create and utilize custom scripts, it’s a great feeling and wouldn’t have been possible without this site and it’s eager to participate members! Many thanks! :cool: