SaveLoad : Save and Load [Script Example]

Use:
Python controller (name does not matter) that use module
‘SaveLoad.save’ to save your scene
‘SaveLoad.load’ to load your scene

Features
:
Save/Load instanced objects
Save/Load not instanced objects
Save/Load object Position
Save/Load object Orientation
Save/Load object Properties
You can choose were you want to create a save.
A blacklist (Objects in this list wont be saved)
Settings menu (in the script)
Settings can be set with object properties
Save encryption (None,Base64,Base32,Binhex)
Checksum (you can choose if you want to use it or not)
Whitelist (only objects listed in it will be saved, must be activated via settings)
Save globalDict. (If you want to save the globaldict inside the save file)

Object needed:
An empty named ‘Spawner’ somewhere in the active layer.

Example:
Arrow keys to move the cursor
WASD to rotate the cursor
1 to Save
2 to Load

Attachments

SaveLoad.zip (76 KB)

SWEEET! this is what i need thanks!

ok so i cant get it to work what i have is on the main cube when i hit 1 it has the save module and when i hit 2 i has the load module but nothing happens.

Do you have the right to save a file?
Check if there’s a file named ‘save.sav’ in the blend directory.

Edit: Re download the blend.

I edited two things:
Detect if you can save in the blend directory, if you can’t, it will save in a place with no right needed.
(look in the console)

Name are reloaded correctly .

ya it saved the save.sav to the desktop so i guess its not loading it when i hit 2

when i hit 1 is saves the save.sav but when i hit 2 i shows this in the console.

Attachments


Seems like it have a problem when you save the orientation and the script can’t parse the saved data.

is there anyway of fixing this anything on my end or in the script?

Yes.

Try the attachment.
I rewrote the script that save and load the orientation.

Attachments

SaveLoad.zip (69.7 KB)

this works great thanks!

cool!!! great!!!
i tried also to add some children to the object in hidden layer
for now no one error…

Small update (see the first post).

Added a blacklist (Line 11 in the script).
Listed object wont be saved.

Monster
Be careful when adding parents. They add their children too. That can result in two objects (the first added via the parent, the second added via the object itself).

Big update. (See first post)

New features:
Settings menu (in the script)
Settings can be set with object properties
Save encryption (None,Base64,Base32,Binhex)
Checksum (you can choose if you want it or not)
Whitelist (only objects listed in it will be saved, must be activated via settings)
Save globalDict. (If you want to save the global dict inside the save)

Attachments


Hi. Your work interrest me. Actually, I create a video game and I work on the savegame system. I’d like understand some thinks : in your dictionnary, you have saved the world position with string. I don’t understand why you haven’t done the same thing for the orientation.

Second question :


# save
dict = {'save_pos' : str(gl.player.worldPosition),
          'save_rot' = list(gl.player.worldOrientation)
         }
                                                   
          save.dump(dict)
                        
          print('save ok')
            
          data.close()

#load            
                                
    with open('Savegame.dat', 'rb') as data :
            loading = pickle.Unpickler(data)
                
            coord = loading.load()
            
            pos = coord
            pos = pos.strip('<>Vector() ')
            pos = pos.split(',')
            
            x = float(pos[0])
            y = float(pos[1])
            z = float(pos[2])
                        
            gl.player.worldPosition = mathutils.Vector((x, y, z))

You can see I succeed to load position but how can I do for two things if I must use list for record the orientation ?

Thanks you.

1: First, the orientation was saved as a string but it was very buggy during the load so I changed it into 3 lists.The position is still saved as a string because it work perfectly and when I convert the worldposition in a list, it create huge floats (ex: 1.1 would become 1.18364825893652958926e -20).

2: When you load data from your file, it is supposed to return the dictionary you saved .

So ‘coord[‘save_pos’]’ would be the position and ‘coord[‘save_rot’]’ would be the orientation.
Then you could handle different type of data.

Furthermore I see that you converted the whole worldOrientation as a list. This return a list of 3 vector and, if I recall correctly, you can’t save vector with pickle (that’s why I converted the worldposition as a string).

Instead do:
‘save_rot1’ : list(obj.worldOrientation[0]),
‘save_rot2’ : list(obj.worldOrientation[1]),
‘save_rot3’ : list(obj.worldOrientation[2]),

It will return 3 list that will be interpreted as vector when you will call ‘player.worldOrientation = ()’

I understand better now. Thanks you very mutch !

hi dberube4! again :wink:

in you system, you think you can solve the problem of references?

ex … player combat monster (monster is a duplicate object (monster N121)
player has a property [“enemy”] with the ID of the monster.

the camera also is following the monster (another ID)

Therefore, when the game starts, there should be more references right.

you plan to make such a thing?

think that’s possible?

This is great, thank you!
Is it possible to customize the keys somehow or to assign a sensor to a specific task
(for example “if property xy=TRUE then save”) ?

You could add an ‘‘if’’ loop in the script
OR (if you are not familiar with python)
you could use a ‘‘and’’ controller connected to a ‘‘signal’’ actuator. A ‘‘signal’’ sensor receive the signal and execute the save script .

Sorry for the reply so long after the thread ended…

The script works well, but for some reason it scales down the objects that it saves when it loads them again. How can I avoid this?