Game properties on scene instead of objects

Hello :slight_smile:

I didn’t find it into to API documentation but I tried to add properties on the scene instead of objects and it’s working quite well. But I wonder if I can do it? Can it cause some issues or is that fine?

scene = bge.logic.getCurrentScene()
scene["myProp"] = "property"

Hello! Many types of objects in the bge support item assignment… I can’t see any reason why do without… But I can make mistake.
You can say bge.blabla = “blabla” and this works too. Or bge.logic.myList = []

use logic.globalDict – it’s a dictionary assigned to the logic specifically for this. It also avoids writing over any other functions in logic/bge. For example: if you try[0] = [“foo”], it won’t work, because is a thing.

Be aware if the scene dies, the properties will die with them.

globalDict is for storing data to be saved. Avoid using it for other purposes.

My first idea was to store the property in the globalDict but I read in the doc that the globalDict wasn’t made to store GameObjects. That’s why I tried scene[“prop”]. It’s logical that the prop will die with the scene. It’s then just perfect for me. Thanks :slight_smile:

This is really nice! Thanks for the info. I used it to store character information between scenes and it works incredibly well! I couldn’t do simpler without it :cool:

you can even place the data on your own module:

import ...

myDataStorage = {}

def storeSomething():
    myDataStorage["myKey"] = "something"

can be used even by other modules.

import myModule

def storeSomething():
   myModule.myDataStorage ["myKey"] = "something"

Is it better to access directly to the property like you’ve just shown than make a more global property with bge.logic.myDataStorage=[] ?