Object Id, serialization and deserialization

Godot Version

Godot 4.4.1

Question

I’m making car game. It has some complex data structures that tries to emulate car parts. So we have:

  1. Car(Resource) that is build from many parts (engine, suspension, etc)

  2. Parts(Resources) that are build from many smaller parts (engine contains valves, camshafts, filters, etc).
    To make different operations on those parts easier, they keep references to each other. So car has references to 1st level of parts. Those parts have references to lower level of parts, but also to the car. And of course lower level of parts (valves, filters) keeps references to their “upper” parts. It works quite well until I tried to make save / load. And those circural references are killing me. So I have some questions:

  3. Is it correct approach? Maybe I should store unique ids of those parts? And all the parts in some database. But then I’ll start having problems with synchronization, car dissapears, I need to make sure all of its parts are also removed. Part can be removed from car, then if car dissapears, part shouldn’t, etc. References make it simple, if there is no living reference to the object, it dissapears.

  4. If this is correct approach, how should I approach saving the game. I can store object id instead of reference but according to the documentation if I load object from file, it’s Id will be different.

Any help / advices?

Don’t have references to “upper” parts.

I need to reference it somehow. Not going into the details, but car needs to know which engine is installed inside, engine needs to know which car it is installed in.

I’m quite new in godot. I know how would I do this on other programming languages (like cpp), but godot suprised me many times on how easy some things can be done here. So maybe there is some clever solution for storing circural references.

Let the car tell it after the whole resource tree has been loaded.

Yes, that’s how I would do this in other languages :slight_smile:

But you are right. Ignoring this field during save (or setting it to null) and restoring after fixed the problem.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.