How to save and load games?

Maybe question is too general, but I am asking if such mechanism exists in Babylon or someone is ready to share some approaches.
And before trying any obvious suggestions I would like to share what I am looking for.

What to save?

  1. Static objects (objects that will not change during all level life time: building meshes, landscape, some lights).

  2. Dynamic objects (objects that might be moved, resized, altered, created or destroyed).

  3. Physics state (if some box was thrown, it should keep it impulse after saving and loading).

  4. Timings (grenade delays, timer before door will start to close. Notice: I never use setTimeout for such purposes I always attach callbacks to main loop via OnBeforeRenderObservable and put frame counters there, then remove callbacks if it’s not needed anymore. I think that this approach is more safe and helps to keep code execution more predictable).

  5. Actors stats (positions, health, weapons, ammo for player, NPCs and monsters).

  6. AI cycles (if NPCs or monsters logic has some steps, it should be saved also. For now it might be skipped, I didn’t implement any AI for now).

When to save?

  1. Checkpoints (much easier, we can put them only when nothing is happening. So we’ll need only to save static objects and some player’s stats. Other things from the list above might be evilly truncated. :smiling_imp:)
  2. Anytime (like the Doom did. We’ll need to save everything. Actually I am looking for such ‘anytime’ approach).

How to save (what to put into file)?

  1. Full dump (the entire level will be loaded from the file. Usually it creates bigger save-files).
  2. Everything excluding static objects (for each Level-data file we can assume two sections loadStaticPart and loadDynamicPart. First part will always be loaded from Level-data file, the second part might be loaded from save-file if provided or from Level-data file instead. It should create smaller save-files. To be honest I am not 100% sure which approach is better.)

I am not sure what BABYLON.SceneSerializer may do for me from the lists above. I am pretty sure that I will need something more and beyond of it. That’s why I am asking how to start this incredible adventure. :smiley:

1 Like

4 important question you most answer before your question
why i need save
what i most save
where i need keep ( local for single player or multi user)
when i do it

after that you have structure just you think about

but the best view for save and load in web is JSON and that is open struct and you can freely save it on localstroge or indexedDB
or you can save structure in server side

you just need look separated i am sure you know that and you have all your question answers in your content

don’t flow standards in custom games i am sure you can define your own standard

but if i wanna save something
i try make it smaller for dynamic parts
and try keep large data for static parts

but all depend for 4 first question


I’ve been using dexiejs.

When initializing your database you shouldn’t define all fields - just id and index fields.

1 Like

I use this :



Apple? :wink:

Apple II
yes (didn’t own one, but dreamed about it)

Basically I am planning to force download file with saved data. But my question is not about it.

I am asking how I can save and restore listed data in BabylonJS? Is there any methods for this?

  1. Static objects - OK, looks like BABYLON.SceneSerializer could do this.
  2. Dynamic objects - well, if only simple properties were altered (position, rotation, scaling, materials), I think that BABYLON.SceneSerializer could help too.
  3. Physics state - ? Completely don’t know what to do. Is there any way too save and restore impostors with all current impulses and energy. And force fields as well.
  4. Timings - ? Even harder. How I can save current execution step of OnBeforeRenderObservable callback? Looks like I have to serialize every possible callback data and turn it to the same state (frame number) while loading. Sounds hard :smiley:
  5. Actors stats - Well it’s pretty easy. JSON works here.
  6. AI cycles - Don’t care for now.

So, the most unclear things are physics state and timings. I am wondering which data should be saved and how use it to bring the scene to the state before saving.

I’ve been working on this a bunch over the last month or so. For a bit, I was serializing and saving the scene. But ran into the problem that while serializing allowed me to save a state and re-open to (almost) exactly the same state, it didn’t help at all with re-initializing my various state tracking objects.

So, at the end of the day, i’ve got a saveState() function that goes through every object that I care about and saves out its current state to a Json file. This includes static and dynamic objects - and any modifications that have been made to them throughout the course of the game. Then onLoad, I go through and recreate / reset each object and property.

It’s a bit laborious. But I don’t think there’s any other good way to do it. Happy to hear otherwise from someone on forum though!

This doesn’t answer your question about physics/timings, of course. I’m not sure how you’d do that. Seems like you’d want to have the game resolve to a neutral state before saving it.

Hi @splash27,

Yes. Babylon.js Documentation

Helpful to use search in Docs and PG: Babylon.js Documentation

:eagle: : )