How to save and load game in BGE?

Even though i searched for some videos, they didn’t help me to get this situation. Can anybody tell me how can i save and load my game in blender game engine?

this topic is quite a bit more complex than a video can handle.

since the bge has no data optimization, saving and loading is something you need to build from scratch, suited to the needs of the game.

the json python module is great for saving and loading a dictionary with ease. but the problem comes with what to put in the dictionary, and how to put it back.

at its simplest, a loop through the scene to save and restore all properties is a good start, but its best to be selective, and save only the most necessary things.

1 Like

Write a script that gets the positions of objects you want to save, their properties and all that, and write that to a file. json format will be easy for this.

1 Like

hello,
you can use a dictionary where all infos should be saved

with actuator Game=> load.bge.logic.globaldict
=> save.bge.logic.globaldict

you can import a dictionary or more from extern .py file
with : from file_name import dictionary_name
or just: import file_name
the file contains your dictionary of course
the dict is now in memory, call it and fill it in the code with his reference

you save the dictionary on the file like this

    with open("file_name.py", "w") as f_write:
        f_write.write("dict_name={}".format( your_dict ))

also read and save infos from .csv file
with the python csv module and “with open()”

if you have just one object s position to deal with, a .txt file is enough
just overwrite with “with open” and “w” option and read the file with “with open()”

1 Like

what informations do you have to save n load?

1 Like

What is json format? I don’t know anything about coding.

I will try and feedback to you. Thank you!

My main character (as fps mode):
Starts from point A.
He walks and save game in point B.
When he arrives point C,
he will die and restart from point B.

I made all death mechanics and animations (including some kind of game save station) but when it comes to coding, i don’t know anything about it. (I made everything with logic bricks) Your previous reply seems helpful but first i need to understand what they means.

Wow! it seems very helpful. I will try this. Thank you!

Thank you for your script but that is not the solution of my problem. In your script my player spawns well but after all those dangers passed, doors opened, sensors worked i will need to reset them one by one and it is really exhausting for me. Can you recommend something with “restart”? Because i need save/load system with restart mode.

My script ain’t the problem, how you have build your game is the problem.

How does any sensor know it’s reloaded? Or how does a script know if a door is open?
You simply need to let the mechanics work based on a property. For example:

  • Build a door
  • add a property
  • when player clicks button, change the property to open or close
  • let the door play it’s animation based on that property, so when property is open play open animation, when closed play closed animation.
  • Don’t let the door play the animation directly. This counts for anything really.

Then the only thing the script need is another property on any object you want to save, so the script knows that it needs save that object. Then when reloading, the property open/close is restored to what was saved and animations play to open or close it after loading.

Or instead of a property you can use states, script will save that as well, so both options work.

1 Like

Ok i will try again. Thank you.

One more question whether i understand or not because i’m newbie:

If i want to restart (for example a door) animation, i need to add that door a property called “save”.
If there will be another doors, i will have to add another “save” 's right?

Despite i do exact same thing; while it is working on your example file, it is not working on my file except only one object (which is my main character object) and i don’t understand why!

I have:
1-Built thing (like door),
2-added property on that door
3-i made animations for that door controlling by properties (for example, if door proerty = 0, door closed, if property =1, door is opening)

And still not working.

*I added spawner empty objects

*Even i dont make animation property, even i only add “save” property it works on your file but in mine, not.

*My properties based “integer”. Does this make problem?

Does the door thing have a property save as well?
Any object that you want to save need a property called save.

Any format int,str,float or boolean.

Yes. Exactly. They have “save” property.

I don’t know. I will try again to be sure.

I couldn’t handle but i do other way that i cannot even explain how to. It seems working for now. But thanks for your replies.