Simple variable save load scripts?

Okay, I’m new to python, and I’d like to know who to write two separate scripts. One to do an action based on where a variable is one or zero in a file. I’d also like to know the script that writes 1/0. This would be useful for saving and loading in a game.

I’d like to start small, so just a small script for 1/0 variables. I know about the GameDict thing, but it seems far too complicated for me right now.

Thanks in advance!

I think you are “simplifying” at the wrong aspect. The complexity of a solution does not depend if you have 0/1, because it does not matter if you want to store a boolean, string, number or number. In any case you need the managing overhead, which can add quite a lot of overhead.

To answer your question you can use this code:


import bge
def storeVariableValue():
   variable = 1
   bge.logic.globalDict["variable"] = variable
   print("stored variable value of",variable)

then activate the SaveActuator to write it to a file

the other way around:
Activate the LoadActuator to read data from a file

the next frame restore the value:


import bge
def restoreVariableValue():
   variable = bge.logic.globalDict["variable"] 
   print("restored variable value of",variable)

You see this is not that complicated. It does not even matter if you want to store/restore 0,1,“teddy bear” or 3.121415. [Yes, it will become more complex when you store complex objects such as lists, dictionaries and KX_GameObjects.]

Now you might be confused, especially because the code uses globalDict and you explicitly did not want to use it. The truth is, if you are not able to use as dictionary you can’t use any other solution too. It is a “ready-to-use” and pretty simple method. Opening and accessing a file adds more complexity which is much harder to understand.

Anyway. The real question is:

WHAT do you want to store WHEN and WHERE to get them?
and related:
WHAT do you want to restore WHEN and WHERE to put them?

I do not use the term save/load as this is another operation that can be done by the actuators.

I was wanted to save variables that I use to determine where an object is to be, such as some logic bricks that detect 0-3 and based on the data it goes to a certain position.

Also, would you mind some explanation of the code? And, are you talking about the save thing in the game actuator?

You can find some detailed explanations at the SaveLoader. Even if you do not use it it might help you to organize your method of storing/saving/loading/restoring.

In general you have 4 operations:

A) store
B) save

and the counterparts:
C) load
D) restore

B) and C) can easily be done via Save/Load actuator. So you do not need to write a single line of code. But you can if you want. If that is the case I suggest to look for Python file operations as this is not Blender nor BGE specific.

A) means you grab data from the scene e.g. the position of a game object and copy it to a container that can be saved. I recommend to use globalDict as container as this is what you can use with B) and C). Any other savable container is fine too. [Hint: avoid any object references]

D) means you take the data from the container and copy it to the scene. This means you need to know what data belongs to what part of the scene. Be aware this is not the same scene the data was taken from by A).

Okay two more questions;

Where is a save/load actuator?

And can I save a variable currently present in the game? Like say a variable telling a spot lamp that a flashlight model has been taken?

It is a mode of the game actuator

I guess you mean a property not a variable.

Anyway, a property consists of:
A) a name
B) a value
C) an object it belongs to

You need to store all information in a way that you can restore it. Be aware C) does not exist at the time of restore anymore. So you need to find a sufficient other object (e.g. an object with the same name)

Sorry for bothering you, but how would I do that?

for example you store the object name, property name and property value:


properties = {}
properties["myProperty"] = myObject["myProperty"]
...

objectData = {}
objectData["properties"] = properties
...

objectsData = {}
objectsData[myObject.name] = objectData

globalDict["objectsData"] = objectsData

This way you can search for the according object in the other scene by name.


objectsData = globalDict["objectsData"]

for objectName in objectsData.keys():
  gameObject = scene.objects[objectName]
  objectData = objectsData[objectName]
  properties = objectData["properties"]
  for propertyName in properties.keys():
    propertyValue = properties[propertyName]
    gameObject[propertyName] = propertyValue

This is just an untested quick and dirty example to show you what you can do.

There are more things to keep in mind: multiple scenes, objects not found, multiple objects with the same name etc.

Uh, Is “myObject” the object with the property in question? Is myProperty the property that is saved? Also how is the property value saved/loaded?

yes

On the right side yes, it is the property name from the game object.
On the left side, no it is the key it gets stored under in the storage.

Using the same name in both cases makes it easier to restore it later.

Be aware a property has 3 characteristics:

  • a name
  • a value
  • an owner

You do not.

You save/load the storage. That is why you need to move the date into the storage (store operation) and out of the storage (restore operation).

As the property name and value are part of the storage (but separated from the game object) they will be saved with it.

After loading the storage contains the property name and value. As the owning object does not exist anymore, you need to find an appropriate candidate to create a new property to receive the property value. Both value and name are retrieved from storage.

To determine an “appropriate candidate” you can (and should) store according criteria such as the object name in the storage too (see post#9).