I have one property which gets assigned a value via this SaveLoad script:
from bge import logicpath = logic.expandPath("//")
def save():
cont = logic.getCurrentController()
own = cont.owner
# 'info' is what will be saved to the file.
# Example:
# info = str(*What you want to save*)
info = str(own['prop'])
file = open(path+str(own)+".txt", 'w')
file.write(str(info))
def load():
cont = logic.getCurrentController()
own = cont.owner
file = open(path+str(own)+'.txt','r')
line = file.readline().replace('
','').split(',')
own['prop'] = int(line[0])
I also have a text object/curve, “Text” that i want to assign with the value of “prop”.
so i want to something along the lines of prop = Text
prop being a property on one object and Text being a property on another.
from bge import logicpath = logic.expandPath("//")
from bge.logic import getCurrentController, globalDict
def store():
if notAllSensorsPositive(): # I think you can write this by yourself ;)
return
globalDict["myValue"] = getOwner()["myProperty"]
def restore():
if notAllSensorsPositive():
return
getOwner()["myOtherProperty"] = globalDict["myValue"]
def save():
# better activate the SaveActuator
# or pickle global dict
# or save global dict however you like
def load():
# better activate the loadActuator
# or unpickle global dict
* * # or load global dict*however you like
def allSensorsPositive():
for sensor in getCurrentController().sensors:
if not sensor.positive:
return False
return True
def getOwner():
return getCurrentController().owner
I recommend to separate storing (game state -> to storage) from saveing (storage to persistent place). This are two different things even if they are often used in a sequence. As you (hopefully) see if you separate them you can replace saving with a different implementation without affecting storing (and vice verse).
Further on, I suggest to think about a way to provide the property names in a more generic/configurable way rather then hard code them. In addition you might want to think about how you can store more information in the storage (e.g. scene data, data of more objects etc.)