move data through scenes

hi again!
well, this time, my question is how can i move data(by data i mean python variables)betwen scenes, at first i thought this was easy, but i tried a lot of things(messages, globaldict, and some others i dont remember) and couldnt make it work, so anyone has any idea of how i can do this?

python Global?

Where are you having issues?

The easiest way is to alter values directly across the scenes. So if object1 dies, and you want to add a score, then get object1 to get the object in the overlay scene and directly change it’s value.

What’s wrong with bge.logic.globalDict ?

To use it, in one scene:

dict = bge.logic.globalDict
dict['data'] = 'some data here'

and in another:

dict = bge.logic.globalDict
print(dict['data'])

so, it now works perfectly,except this error:


do you know why this happens and how to fix it?

Are you setting the key in the globalDict before you try to refer to it? For example:



logic.globalDict['item'] = 'potion'

print (logic.globalDict['item']) # Only works after the 'item' key has been set


If you refer to the key before it’s set, then it will interrupt Python with the error and halt the logic execution of the rest of the game scene, if I recall (making that error repeat the next time, as well). If that’s not part of the problem, try posting your script.

yep i set it previusly in the other scene, like this
in inventory scene:

dict = bge.logic.globalDict
dict["item"] = part

“part” is an intenger that controlls some stuff related with the game

and in game scene:

dict = bge.logic.globalDict
type = dict["item"]

this actually works as i want, it just throws this error!

Python is pretty direct and usually very true with the Error messages. Sometimes it is surprisingly easy ;).

Your message means there is no key “item” in whatever you have on line 20 of file “manager”.

Such things are usually followup errors of design issues. In this case I guess you try to read a dict before this “item” was written.
Now you should think if you

  • deal with the error (try…except, using get(), or check with “in”) - simple but might be wrong
  • review and update your design -> maybe the read is to early so something else prevented to write the “item”

Python is pretty direct and usually very true with the Error messages. Sometimes it is surprisingly easy ;).

Your message means there is no key “item” in whatever you have on line 20 of file “manager”.

Such things are usually followup errors of design issues. In this case I guess you try to read a dict before this “item” was written.
Now you should think if you deal with the error
-> (try…except, using get(), or check with “in”)

  • simple but might be wrong

or you review and update your design
-> maybe the read is to early so something else prevented to write the “item”
-> maybe you have a timing problem (I guess it is)

hey thanks, that made it, now it keeps working, but it throws no errors at all :smiley:
i just changed this:

dict = bge.logic.globalDict
type = dict["item"]

for this:

dict = bge.logic.globalDict
type = dict.get("item")

thanks again!