Suppose i have two entities with these components:
//some tag for an entity that contains meshes and materials etc
public struct Prefab : IComponentData {}
//a floor plan that references an entity to get the meshes/materials from
public struct FloorPlan : IComponentData{
Entity Prefab;
}
How do you save this to a save file, lets say JSON although the format isn’t important here… the key point is how to then load it back and keep the reference to the Prefab entity?
I can’t find much info at all from Unity docs on how we should go about this. And please don’t pitch me your save/load asset from the asset store i want to learn to do my own save/load system.
First of all I’m not sure what you mean by prefab in this case. Maybe you have a repository with references to different prefabs, and you choose one at runtime depending on some criteria?
Anywho, what you’d usually do is save some serializable type as JSON if we’re going with JSON, an enum would be the typical choice. And then you’d have some dictionary that takes the enum as the key and outputs the prefab or whatever other reference you want as the value.
The code above isn’t my actual code… The point was more that i have entities that reference other entities… take another example a way point graph… edges reference two nodes and nodes have references to edges to signify how its all connected so they have a field of Entity in them.
If i want to save this data how do i save the fact they reference other entities since i can’t just serialize the ID of the entity because they won’t have the same ID when i load and create them again.
There are multiple methods, it all depends on how these entities are created and how your game is structured. If there’s only a few of them, you could make an enum with a specific value for each of them, and use a dictionary like I explained above.
In my current project for example, I use a mix of world chunks and the hierarchy itself to generate IDs for the objects I want to persist, since when a scene loads, the objects will always have the same hierarchy position and therefore the same sibling indexes. This is not ideal if you know the hierarchy positions may change in the future of course.
If your entities are objects that are generated on the fly on the other hand, you could save all their configurable data instead, and then generate new identical versions of the fly using the saved data when the game loads.
If the objects are runtime created, what I think would be best is, like I explained in the last paragraph, save all the variables that were used to create the object, and then recreate a new object using the saved data on game load. How does your entity system work exactly so I can get a better idea of the requirements?
You keep a database of your prefabs with an ID (usually an int) and use this on your component. You then use this ID to get the prefab from your database.
I provide no examples or support. It’s just here if you want a reference.
I will likely answer questions if you ask on discord though if you have them.
Hi, Celt. Let’s say you have a bunch of entity A and entity B. Each A has a component
public struct MyReferenceB: IComponentData {public Enitty b;}
that refers to B. When you save the game, you can
Store all data about B in an array such as NativeArray<DataB> databaseB with each DataB representing one entity B.
Make a NativeHashMap<Entity, int> that maps the Entity of each B to the index in databaseB.
Save the data of A. Use the index of B in the databaseB to represent the data of MyReferenceB.
When you load the game
Create all B with the `DataB’ from the save file.
Store the entity of B you created in a NativeArray<Entity> arrayB. The order should be the same as the databaseB in your saving process. If you are using Entity Command Buffer, store them in Dynamic Buffer instead.
Create all A. When setting the MyReferenceB, you can get the Entity from the arrayB using the index you saved.