for object in objects:
if type(object)==bge.types.KX_GameObject:
bge.logic.globalDict["position"][str(object)] = list(object.worldPosition)
bge.logic.globalDict["orientation"][str(object)] = list(object.worldOrientation[0]),list(object.worldOrientation[1]),list(object.worldOrientation[2])
for property in object.getPropertyNames():
if len(object.getPropertyNames()) > 0:
bge.logic.globalDict["properties"][str(object)] = {}
bge.logic.globalDict["properties"][str(object)][property] = object[property]
#sets position and orientation
bge.logic.saveGlobalDict()
print("Saved File")
endgame = [sens for sens in controller.sensors if type(sens) == bge.types.SCA_KeyboardSensor]
if len(endgame)>0:
if endgame[0].key == bge.events.ESCKEY:
bge.logic.endGame()
def load(): #imports properties from the save actuator
bge.logic.loadGlobalDict()
#sets the properties from the globalDict imports
for ob in bge.logic.globalDict["position"].keys():
if ob in objects:
object = objects[ob]
#sets the orientations from the globalDict imports
object.worldPosition = tuple(bge.logic.globalDict["position"][ob])
object.worldOrientation = tuple(bge.logic.globalDict["orientation"][ob])
if ob in bge.logic.globalDict["properties"]:
for property in bge.logic.globalDict["properties"][ob].keys():
object[property] = bge.logic.globalDict["properties"][ob][property]
print("Loaded File")
It work, but when i restart the scene it stop to work, any1 have a solution?LO
sure, sorry i not know how i do tx you. this is the code:
import bge
scene = bge.logic.getCurrentScene()
objects = scene.objects
#set global varialbles instances
if not "properties" in bge.logic.globalDict:
bge.logic.globalDict["properties"] ={}
if not "position" in bge.logic.globalDict:
bge.logic.globalDict["position"] ={}
if not "orientation" in bge.logic.globalDict:
bge.logic.globalDict["orientation"] ={}
def save():
controller = bge.logic.getCurrentController()
owner = controller.owner
#sets local properties
for object in objects:
if type(object)==bge.types.KX_GameObject:
bge.logic.globalDict["position"][str(object)] = list(object.worldPosition)
bge.logic.globalDict["orientation"][str(object)] = list(object.worldOrientation[0]),list(object.worldOrientation[1]),list(object.worldOrientation[2])
for property in object.getPropertyNames():
if len(object.getPropertyNames()) > 0:
bge.logic.globalDict["properties"][str(object)] = {}
bge.logic.globalDict["properties"][str(object)][property] = object[property]
#sets position and orientation
bge.logic.saveGlobalDict()
print("Saved File")
endgame = [sens for sens in controller.sensors if type(sens) == bge.types.SCA_KeyboardSensor]
if len(endgame)>0:
if endgame[0].key == bge.events.ESCKEY:
bge.logic.endGame()
def load():
#imports properties from the save actuator
bge.logic.loadGlobalDict()
#sets the properties from the globalDict imports
for ob in bge.logic.globalDict["position"].keys():
if ob in objects:
object = objects[ob]
#sets the orientations from the globalDict imports
object.worldPosition = tuple(bge.logic.globalDict["position"][ob])
object.worldOrientation = tuple(bge.logic.globalDict["orientation"][ob])
if ob in bge.logic.globalDict["properties"]:
for property in bge.logic.globalDict["properties"][ob].keys():
object[property] = bge.logic.globalDict["properties"][ob][property]
print("Loaded File")
import bge
scene = bge.logic.getCurrentScene()
objects = scene.objects
#set global varialbles instances
if not "properties" in bge.logic.globalDict:
bge.logic.globalDict["properties"] ={}
if not "position" in bge.logic.globalDict:
bge.logic.globalDict["position"] ={}
if not "orientation" in bge.logic.globalDict:
bge.logic.globalDict["orientation"] ={}
def save():
controller = bge.logic.getCurrentController()
owner = controller.owner
#sets local properties
for object in objects:
if type(object)==bge.types.KX_GameObject:
bge.logic.globalDict["position"][str(object)] = list(object.worldPosition)
bge.logic.globalDict["orientation"][str(object)] = list(object.worldOrientation[0]),list(object.worldOrientation[1]),list(object.worldOrientation[2])
for property in object.getPropertyNames():
if len(object.getPropertyNames()) > 0:
bge.logic.globalDict["properties"][str(object)] = {}
bge.logic.globalDict["properties"][str(object)][property] = object[property]
#sets position and orientation
bge.logic.saveGlobalDict()
print("Saved File")
endgame = [sens for sens in controller.sensors if type(sens) == bge.types.SCA_KeyboardSensor]
if len(endgame)>0:
if endgame[0].key == bge.events.ESCKEY:
bge.logic.endGame()
def load():
#imports properties from the save actuator
bge.logic.loadGlobalDict()
#sets the properties from the globalDict imports
for ob in bge.logic.globalDict["position"].keys():
if ob in objects:
object = objects[ob]
#sets the orientations from the globalDict imports
object.worldPosition = tuple(bge.logic.globalDict["position"][ob])
object.worldOrientation = tuple(bge.logic.globalDict["orientation"][ob])
if ob in bge.logic.globalDict["properties"]:
for property in bge.logic.globalDict["properties"][ob].keys():
object[property] = bge.logic.globalDict["properties"][ob][property]
print("Loaded File")
separate storage locations
I suggest to store data of one object together rather than data of the same type (positions). You will see this makes the data handling much more comfortable as you can focus on one object at the time.
non-unique object names
You use object names as key. This is only sufficient if you guaranty there is no other object with the same name. This can be a valid assumption, but make sure it always matches your situation.
full qualified attribute access
In general I think it is a good idea to use full qualified names (bge.logic.globalDict). But as you use it that much in a very narrow context it might be a good idea to store it in an intermediate variable and use this variable. This is much more readable, and you avoid unnecessary access processing.
e.g.
storage = bge.logic.globalDict
storage["key"] = value
storage["otherkey"] = otherValue
Outdated data
Never ever store objects (incl. owner) or even the scene in a module variable (variable set at indentation level 0) without a way to update it later. It will be incorrect when adding/removing objects or switching the scene.
scene = bge.logic.getCurrentScene()
objects = scene.objects
Finally there is no need to do that, as you always can grab the current data from the API when you need it. In conjunction with the above statement there is no problem to use a local variable to hold the data for the current processing but within the local context (rather then a global context).
Separation of concerns
Your function is called save() - this let me assume it saves something.
Why does it contain something that is called “endgame” - this has nothing to do with saving and should not be there.
If you really want it there call your function saveAndEnd(). But I recommend to keep ending away from saving.
thank you for your reply and your time,
I’m searching a code that can make a “SAVE/LOAD” in a scene also if this scene was reload whit logic brick “restartScene”.
this code works only if i not restart the game, after stop to save and load.
Do you know or have a code that can do this?
i assicure that i will quote you in my credits game…it’s becoming very nice and soon I’ll make it available…but first i need a script that can save e load correctly
If you restart the scene you need to run the code again for to load the values again, you tried it? just attach a message sensor to the script, and when the scene is restarting send the message.
I’m pretty sure it is because of Outdated data. This is because a Python module will be initialized once per game session. You switch scenes in-between. After loading a new scene the module variables “scene” and “objects” are simply wrong.
(restarting a scene means it switches to a new scene too. If a scene has equal content and equal name it is not the same scene. It is an equal scene.)
i think too…but i don’t know how i can get out this variable…have you a idea?or other script?i’m not the maker of this script, i know only java script, reason i can understand the script but not write it
import bge
#set global varialbles instances
if not "properties" in bge.logic.globalDict:
bge.logic.globalDict["properties"] ={}
if not "position" in bge.logic.globalDict:
bge.logic.globalDict["position"] ={}
if not "orientation" in bge.logic.globalDict:
bge.logic.globalDict["orientation"] ={}
def save():
controller = bge.logic.getCurrentController()
owner = controller.owner
#sets local properties
for object in getObjects():
if type(object)==bge.types.KX_GameObject:
bge.logic.globalDict["position"][str(object)] = list(object.worldPosition)
bge.logic.globalDict["orientation"][str(object)] = list(object.worldOrientation[0]),list(object.worldOrientation[1]),list(object.worldOrientation[2])
for property in object.getPropertyNames():
if len(object.getPropertyNames()) > 0:
bge.logic.globalDict["properties"][str(object)] = {}
bge.logic.globalDict["properties"][str(object)][property] = object[property]
#sets position and orientation
bge.logic.saveGlobalDict()
print("Saved File")
endgame = [sens for sens in controller.sensors if type(sens) == bge.types.SCA_KeyboardSensor]
if len(endgame)>0:
if endgame[0].key == bge.events.ESCKEY:
bge.logic.endGame()
def load():
#imports properties from the save actuator
bge.logic.loadGlobalDict()
#sets the properties from the globalDict imports
objects = getObjects()
for ob in bge.logic.globalDict["position"].keys():
if ob in objects:
object = objects[ob]
#sets the orientations from the globalDict imports
object.worldPosition = tuple(bge.logic.globalDict["position"][ob])
object.worldOrientation = tuple(bge.logic.globalDict["orientation"][ob])
if ob in bge.logic.globalDict["properties"]:
for property in bge.logic.globalDict["properties"][ob].keys():
object[property] = bge.logic.globalDict["properties"][ob][property]
print("Loaded File")
def getObjects():
scene = bge.logic.getCurrentScene()
objects = scene.objects