SaveLoader 2.7

Hello All,


2011-11-04:
bugfix

SaveLoader is compatible with BGE 2.49b AND BGE 2.5+ !

updated to 2.7

  • Bugfix: Symptom: Missing objects after loading. Problem: Instances are not found in BGE 2.59+. Solution: Corrected name search
  • corrected logmessage

updated to 2.6

  • Bugfix: If a prepared Scene did not need to get restored the contained instance objects are not removed anymore.

updated to 2.5

  • added makeSingleObject - it does not really belong to the saveLoad but it is necessary to remove any doublicate objects
  • added clearAllStores to clear all stored storage.

updated to 2.4

  • added autoload and reset

updated to 2.3

  • now Python 3.0/BGE 2.5x compatible (remains 2.49 compatible)
  • aliases for BGE callable methods for a better overview
  • moved BGE callable methods to the top of the file
  • some refactorings for reduced complexity

updated to 2.2

  • Bugfix: local excluded are now usable when saving. (where ignored before)
  • Parented objects will not become ghosts (as default).
  • Add logic to identify armature objects. Children of this objects will not be reparented. It is not possible to do armature parenting or bone parenting while running the BGE.

Here you can find the old version SaveLoader 1.1

2010-06-25:

  • updated to 2.1 - fixed a bug when loading parented objects.
  • updated the demo file - Ctrl. saveLoad now refers to the script. For simple load save (with <L> and <S>) just append this object to your file.

after heavy work the new version 2.0 of the SaveLoader is ready for release.
There are some changes especially on the API, the file format and the internal storage.

The saved files are still pyhton scripts, but not compatible with 1.1.

The most important improvement is that it is possible to make savepoints and load them from another scene. I will explain how it works in a later post.

The SaveLoader has no dependencies to the controllerLib anymore. So only SaveLoad.py is needed.

You can find the module in the attached Demo:

It is a little portal demo. With the arrow keys the ship can be moved.
When colliding with a white sphere you can collect points.
When driving over the colored squares the scene switches to another scene saving the current scene state.

With <S> the current game state is saved to a file.
With <L> the saved game state is restored. If the current scene is not the current scene at save time, the loaded scene will be started.
With <0> you can reset the game back to start to by pass autoload.

Most important rule:
—> Never, never, never store or restore an object/scene within the same frame more than once <—
It is not possible to determine which one would be first.

The setup is quite simple. See the next posts for a small tutorial or check out this video tutorial. The important part starts at 01:55:

Update:
Added an auto load option (see SaveLoader.autoLoad).
Added a reset to default option (see SaveLoader.loadDefault) with <0>.

Renamed all Saveloader objects.

Required:
SaveLoad.master - carries the Python file and deals with preloaded storage (required)

Portal related:

SaveLoad.storePlayer - stores/restores the player object (previously called ship)
SaveLoad.storeScene - stores/restores the non-player objecs

Saving/Loading:
SaveLoad.manual - complete store/restore with <s>/<l>.
SaveLoad.autoLoad - complete restore of a saved game at startup.
SaveLoad.loadDefault - removes the complete storage and reloads the scene <0>.

SaveLoaderDemo.blend
or go to the last posts of this thread.

Have fun
Monster

A) How to store a scene when switching to another one?
B) How to keep the data of objects while switching to another scene?

In this example the current scene is Level.Red. The ship drives other the blue square (ExitToBlue.Red) and triggers the switch to scene Level.Blue.

  • The blue square sends a message to all objects announcing a scene switch within the next frame. Note: The switch must be in the next frame otherwise the message can’t be processed.
  • Within the next frame the store scene controller (Ctrl.storeScene) receives the switch message the current scene will be stored in the SaveLoader storage “default”. This excludes any objects with data to be transferred to the next scene (ship).This can be done by defining property saveLoad.excludeByProps with “ship”.
  • When the transfer controller (Ctrl.storeShip) receives the switch message the current scene will be stored in the SaveLoader storage under a different storage “ship” and scene “ship”. This includes only the objects with data to be transferred to the next scene. This can be done by defining property saveLoad.includeByProps with “ship”.
  • The blue square switches to scene Level.Blue.

Following will be processed when started Level.Blue scene:

  • The first frame the scene loads as usuall
  • The second frame the store scene controller (Ctrl.storeScene) will restore the scene Level.Blue from SaveLoader storage “default”, removing all objects that are not stored except the excluded objects (ship). This can be triggered with property saveLoad.endMissing set to True.
  • In the same frame the transfer controller (Ctrl.storeShip) will restore the scene “ship” from SaveLoader storage “ship”. This scene contains the transferred object data only.

This is really good, thanks. These save/loading scripts will help a lot of people.

Have you though of converting the scripts to Blender 2.50? Because that would be great!

Ex.

How to save the game status to file?


When the save event happens (<S>) following needs to be done:

  • The saveLoad controller (Ctrl.saveLoad) clears the current scene Level.Blue from storage “default”. This is to make sure that objects that might be removed since last store are not present in the storage.
  • The current scene Level.Blue will be stored in the save loader storage “default” marking this scene as active. This will be done automatically with the last stored scene.
  • Saving the storage “default” with all scenes to file (no excludes).

How to load the game status from file?


The current scene is Level.Blue.

  • Load the file into the storage “default” (the existing storage will be replaced automaticaly.
  • Prepare the loaded active scene for restore.
  • Switch to the loaded active scene Level.Red.

Following will be processed when started Level.Blue scene:

  • The first frame the scene loads as usual.
  • Within the next frame the saveLoad controller (Ctrl.saveLoad) restores the prepared scene.

really helpful thanks

The scene switching objects: ExitToBlue.Red etc.:


No properties
Logic:

  • State “Init”:
    [LIST]
  • At the first frame the switch should be deactivated:
    [LIST]
  • go to state “Near”.

[/LIST]

  • State “Near”:

  • Check for the event to perform the switch:
    [LIST]

  • send a message “switch” to inform all objects that the scene will be switched.

  • go to state “switch”.

[/LIST]

  • State “Switch”:

  • when entering this state:
    [LIST]

  • switch to the defined scene.

[/LIST]
[/LIST]
The store scene controller Ctrl.storeScene:


Properties:

  • saveLoad.endMissing as boolean with True - on rstore objects not stored are removed. This is provide the possibility to remove objects from the scene.

  • saveLoad.excludeByProps as string with a space separated list of properties here “ship”. Objects with one of that properties will be excluded from store.
    Logic:

  • State “init”:
    [LIST]

  • At each frame:
    [LIST]

  • module SaveLoad.hasNoPreparedScene: check that no prepared scene exists (to avoid conflicts with the saveLoad controller). If there is no prepared scene
    [LIST]

  • go to state “restoreScene”

[/LIST]
[/LIST]

  • State “restoreScene”:

  • When entering this state:
    [LIST]

  • module SaveLoad.restoreScene: restore the scene.

  • When receiving the message “switch” (from any switch scene object):

  • module SaveLoad.clearSceneStore: clear the scene storage. Default is storage “default” and scenename is the current scene name.

  • module SaveLoad.storeScene: store the scene.

[/LIST]
[/LIST]The transfer controller Ctrl.shipScene


Properties:

  • saveLoad.storage with the name of the storage “ship”.

  • saveLoad.scene with the name of the scene “ship” within the storage “ship” .

  • saveLoad.includeByProps as string with a space separated list of properties here “ship”. Only objects with one of that properties will be stored.
    The states are equal to the store scene controller:

  • State “init”:
    [LIST]

  • At each frame:
    [LIST]

  • module SaveLoad.hasNoPreparedScene: check that no prepared scene exists (to avoid conflicts with the saveLoad controller).
    [LIST]

  • go to state “restoreScene”

[/LIST]
[/LIST]

  • State “restoreScene”:

  • When entering this state:
    [LIST]

  • module SaveLoad.restoreScene: restore the scene.

  • When receiving the message “switch” (from any switch scene object):

  • module SaveLoad.clearSceneStore: clear the scene storage. Default is storage “default” and scenename is the current scene name.

  • module SaveLoad.storeScene: store the scene.

[/LIST]
[/LIST]The saveLoad controller Ctrl.saveLoad


Properties:

  • saveLoad.filename the name ofthe file to save to/to load from.

  • saveLoad.endMissing as boolean with True - on restore objects that are not stored will be removed. This provides the possibility to remove objects from the scene that were’t present at save time. Be carefull in combination with include and exclude options.
    Logic:

  • State “init”:
    [LIST]

  • module SaveLoad.hasPreparedScene: check that a prepared scene exists:
    [LIST]

  • go to state “restorePreparedScene”

  • module SaveLoad.hasNoPreparedScene: check that no prepared scene exists:

  • go to state “SaveLoadControls”

[/LIST]

  • State “restorePreparedScene”:

  • When entering this state:
    [LIST]

  • module SaveLoad.restorePreparedScene: restore the prepared scene

  • go to state “SaveLoadControls”

[/LIST]State “SaveLoadControls”:

  • If <S> (or any other saveing event):
    [LIST]

  • module SaveLoad.clearSceneStore: clear the current scene’s storage

  • module SaveLoad.storeScene: store the current scene

  • module SaveLoad.saveToFile: save the storage to file

  • If <L> (or any other loading event):

  • module SaveLoad.clearSceneStore: clear the current storage

  • module SaveLoad.loadFromFile: load the storage from file

  • module SaveLoad.restoreScene: restore the current scene

  • module SaveLoad.switchToScene: switch to the prepared scene (needs an set scene actuator!)

[/LIST]
[/LIST]All other Logic is game play related

I hope it helps

Monster

Blender 2.50 would be a bigger chellange as it will be possible to dynamically load new objects.

Wow, what a tutorial, great work, really usefull script

Great Job Monster!!!
Your really great! Not only do you come up with quality stuff, but you always draw really great diagrams. :wink:

Keep it up!!! :smiley:

nice stuff Monster !!

I updated the demo file in the top post. For some reasons some of the objects were not in the initial state. That produced some unwanted effects (e.g. not possible to mode to level red, not possible to load). It should be fine now.

Monster

I don’t understand this at all! How do you use this? It can’t be as simple as connecting the SaveLoad.py module to an “Always” and that’s it. I’m really confused. And will it even work in 2.5? My computer won’t let me play the demo.

Connecting to an always sensor makes sense if you want to load a scene state at scene startup. So yes it is that simple.
It is up to you on what events the script runs.

See post #6 with the Ctrl.saveLoad. It reacts on <S> and <L>. You can change that.
Post #4 shows you how it works.

You can even append this empty to your own file. With <S> and <L> you can save and load from file. The filename is taken from the property (if any).

This script is 2.49b only. A 2.50 might come later. This requires an upgrade to Python 3.

If the example is not working for you please post me the errors from the console. So I can have a look at it. It is not supposed that you fully understand the code. It is supposed to know what it does and how to use it.

Thanks
Monster

(hi this is amazing script. but can it save the properties or the state of ipo animation? in my blend is not working this features…)

i have the switchable torch in my game and i want to remember the state of the torch when i’m saving the game… it will remember the state but after i load the game I’m not able to switch the torch back off or on… i’m using the ipo animation to do this and the “torch” boolean property to get the torch state… it’s working but not after i save or load the game…

Could you provide me a .blend? The best would be with the torch and the save/load logic only.

I think there is a bug in blender with IPO controlled transformation (loc/rot/scale) when the objects are parented. This might be the reason. I haven’t digged into much detail in that as it is difficult to identify.
But it could be something else.

[Edit]
There is an ugly bug in the 2.49 BGE. It is not always reproducale.
Mostly it happens with IPO controlled objects parented to another object.
When reparenting and/or setting a new location, the location is reset to some unexpected offset.

There are two possible fixes:

  1. make sure the IPO starts with position [0,0,0]
  2. exclude the position from beeing save for that object by adding an String Property “saveLoad.exclude” containing “position
    [/Edit]

hi. how can i just add overlay scene to be stored. i’ve tried the “Ctrl.storeScene” property with value “overlay”. and it’s not working. note the “overlay” is the name of my overlay scene…

wow… a very powerfull script. the saveload.py work perfectly fine.
but i have a situation, example:
i have 2 scene, which is basically the same (link) scene. but the camera is diffrent, 1st scene is first person view; 2nd scene is topview.
so my problem is when the user in the 2nd scene, user able to drag and drop the object and save it. after that go to 1st scene, user able to load the object position from the 2nd scene and it still still the first person view.
do you and any advice or solution for this problem? thx in advance.

Brilliant!

I didnt use it yet, but this is very cool thanks monster, you really help us out with your work!
I just want to ask you, you think that the script ll work on burster?