Save and Load script v4.3

Hello,

* New version * (18-5-2018)

4.3 changes

  • Huge bug fix, the GD itself was not restored properly.
  • Every one who is using v4.1 or v4.2 should download v4.3

It saves:

  • position
  • orientation
  • properties
  • Linear velocities
  • Angular velocities
  • color
  • scale
  • states
  • and the whole globalDict

How it works:
Every scene that gets saved needs an object(any name) with a property: spawner. This to define the object spawner in the scene (best is just an empty).

Objects u want to save needs a property called: save.

To load use python->module->name_of_script.load (check menu scene).
To save use python->module->name_of_script.save (check menu scene).

you can change the path and extension to your liking (some directories may need admin rights).

This script is verry easy to use, from a menu or from whatever scene/object you want to use it
or simply keybind it.

And here you find the .blend version 4.3

Thats it, hope you like it,
Cotaks

11 Likes

Hi, nicely done. But maybe you want to check line 28. It should be enough to just convert to a list:

position = list(obj.worldPosition)

Edit: I especially like it because when the globalDictionary is saved, it seems to be encrypted too. I didn’t know that.

Hi, thank you.

You are right about the position, gonna adjust that in the blend and re upload it.

Thank you for your sharp eye :slight_smile:

Thanks for sharing this, I tried many times to make something similar but with no success…

For sure I will try and integrate it into a game…

No problem, glad that people can use it! :slight_smile:
Have fun with it.

Does it have some tweakables to set for different scene names etc?

yes and no, not physically, u need to understand python abit. this save and load saves everything in the main scene.

Edit:
As of save_and_load 2.0 it saves every active scene

Main Scene? And… Do I have to set Main Scene somehow? Or is it possible to apply that script to another scene?

Actually, ould you tell me, please, how to make this save method:

Scene “Settings” object contains 4 properties which must be saved each time player presses “ESC”(exits to main menu). It must also always load theese properties when player enters that scene(properties are: “Volume”(int), “r”(float), “g”(float) and “b”(float)). Scene Level_1 contains object with the same named and the same type properties. It must read the data saved from “Settings” scene and load them(so it is possible to set car color trought settings, and also volume. And… The last thing… In scene “Level_1” there is a property called “points”(int). This property also belongs to car. I need that pproperty to be saved, always loaded and than loaded in another scene which contains scores. But in that scene it must be loaded with name “LV1points”. Could you help me with this by creating that script, than explaining how to add more porperties in that system?

No you don’t have to set a scene, it assumes that your “game scene” is the one that runs first, then you load hud and any other overlays/underlays.

As for your other questions, no i don’t make scripts, you should learn python in order to build it on your own.
i share this for people who can’t write a save/load system. if you can use it, use it, else i am sorry.

(score and hud elements are coming from the main scene right? so give the wearer of the stats the property save)

Edit:
As of save_and_load 2.0 it saves every active scene

@ cotax: I’ve made a blend with a save and load system, based on yours, which will also save scenes. If you appreciate it, I can post it here. It also stores velocities. Let me know.

Ill leave that up to you. If you want you can post it inhere.

EDIT: I’ve rebuild my save and load to fit the demands, it now saves all active scenes.

  • Changed info in the first topic

Have fun with it, Cotax

The mechanics are a little different. It also saves velocities. And it can save as many scenes as you want. I didn’t make it so that it saves all active scenes, but just the active scene. The overlay scene should be an extension of the active scene anyway. So you should program it like so, so that whatever you save in the active scene, it will be updated in the overlay scene. The only condition is to give a property to all inactive objects that will be instanced in the active scene.

Attachments

SaveLoad_00.blend (87.3 KB)

I didn’t make it so that it saves all active scenes, but just the active scene. The overlay scene should be an extension of the active scene anyway

True, but if u do it right the “hud” will be removed when u open the menu(so it is not active anymore), and will be added again when leaving the menu.

Hi, COTAX! I have a question- could you tell me how do I write that dictionary(where you save it(I think it was globalDict)) to the file and than read it so I can also save data when I exit game? It would be very useful…

Actually, it saves a .bgeconfig file when I test your dome, but when I implement it in my game, it works only in session, but doesn’t make a file… How to fix it?


Blender Game Engine Started
Python module can't be imported - object 'Cell.001', controller 'Python':
ImportError: No module named 'Cell'
SAVING (Slot1gameplay)
 - Player (False)
 - cell2 (False)
 - Equipment (False)
 - cell1 (False)
 - Playercam (False)
 - DayNight (False)
 - SkySphere (False)
 - Stick (True)
 - Stick (True)
Error, bge.logic.globalDict could not be marshal'd
Warning: could not create marshal buffer
Python script error - object 'save', controller 'save':
Traceback (most recent call last):
  File "/home/user/Adriana_Faili/Games/FloatedAway/CreationZone/FloatedAway.blend/save.py", line 23, in save
ValueError: unmarshallable object
warning: scene InventoryHUD already exists, not added!
warning: scene HUD does not exist, not removed!
warning: scene InventoryHUD already exists, not added!
warning: scene HUD does not exist, not removed!
warning: scene InventoryHUD already exists, not added!
warning: scene HUD does not exist, not removed!
warning: scene InventoryHUD already exists, not added!
warning: scene HUD does not exist, not removed!
Blender Game Engine Finished


Blender Game Engine Started
Python module can't be imported - object 'Cell.001', controller 'Python':
ImportError: No module named 'Cell'
SAVING (InventoryHUD)LOADING (Slot1gameplay) removing objects
 - removing Stick
 - removing Stick
All OBJECTS ARE REMOVED
LOADING (Slot1gameplay) -> spawn objects
Warning: could not open '/home/user/Adriana_Faili/Games/FloatedAway/CreationZone/FloatedAway.bgeconf'
Python script error - object 'load', controller 'load':
Traceback (most recent call last):
  File "/home/user/Adriana_Faili/Games/FloatedAway/CreationZone/FloatedAway.blend/save.py", line 84, in load
  File "/home/user/Adriana_Faili/Games/FloatedAway/CreationZone/FloatedAway.blend/save.py", line 107, in spawn_object
KeyError: 'save game'
Saved session recovery to '/tmp/quit.blend'
Error: Not freed memory blocks: 59


Blender quit

The console output. Maybe this can help you to figure out my problem…

OK! I am going to post the .blend file so you just tell me why it doesn’t save the .bgeconf file. What’s wrong with it???
http://www.pasteall.org/blend/35046

And, by the way, could you tell me how do I also save linear and angular velocity with this?

Thank you for this script. It works very well. Now with a functioning save/load maybe I’ll move from 2.49 to 2.73. :slight_smile:

One thing I did notice. If you have a Text property parented to the camera in the main scene, (like Socials FPS, Ammo, Mags, Text) it saves the location of the planes it’s mapped to. LOL Then when saved, ends up off the camera. LOL I’ll play with the code later to see if I can stop it from saving objects positions, and save only Text properties.

Thanks again.