If i set the subdata of a data object with another data object, is that supposed to be saved and loaded properly?

I have the following code:


public DataObject SaveGame()
{
DataObject data = new DataObject();

persistentObjects.ForEach((k, v) =>
{
DataObject sub = new DataObject();
foreach (var o in v.Value)
{
sub.Set(o.name, o);
}

data.Set(v.Key, sub);
});

return data;
}


Which seems to be correctly passing my data from the method, however when LoadGame is called, the subobject DataObject has all null values in it.


public void LoadGame(DataObject data)
{
if (data != null)
{
Dictionary<string, DataObject> objects = data.GetData<DataObject>(typeof(DataObject));

objects.ForEach((k, v) =>
{
Dictionary<string, GameObject> gObjects = v.Value.GetData<GameObject>(typeof(GameObject));
//gObjects is always null here
persistentObjects.Add(v.Key, gObjects.Values.ToArray());
});
}
}
  • edited May 15
    Generally yes - using DataObject as data is possible (even using arrays of data objects), they can also be retrieved via the GetFile and GetFileArray functions. This is used by ORK to do deep serialization (i.e. serializing fields that represent classes).

    The issue you're facing is the following: you can't store game objects (spawned or prefabs, or any other kind of asset like audio clips, textures, etc.) in save games.
    The data serialization of settings can store references to assets, but not save games.

    You'd need to have some custom serialization to do that, e.g. using the custom component save data to store the needed information of the game object to be able to recreate it on load game.
    Post edited by gamingislove on
    If you're enjoying my products, updates and support, please consider supporting me on patreon.com!
  • I guess that would make sense... So this necessarily needs to be a bit more complicated than I had hoped. Do you know offhand of any tools that can serialize and then load a resource from a gameobject instance?

    I mean the easy answer is just store something that identifies the prefab it comes from, position, rotation, scale, and recreate from that. At the same time this seems likely to become a giant rabbit hole, so to speak.
  • edited May 15
    There was a free save tool available some time back that could save spawned prefabs (if set up correctly), but I don't remember the name ... generally, it should be possible to use on of these save tools that are available in the asset store (or elsewhere) and incoproprate them in ORK's save game system.
    Post edited by gamingislove on
    If you're enjoying my products, updates and support, please consider supporting me on patreon.com!
  • OK cool, thanks for the help on this!

    One other thing, would it be possible to set an object's name when spawning a prefab from an event? i want to be able to control the hierarchy name so that it is easier to find later on.
  • That's possible using a Change Fields node:
    - component name: Transform
    - add a field
    - field name: name
    - is property: enabled
    - field type: String
    - string value: whatever you want to name it

    And don't forget to select the object (actor, prefab, etc) you want to change.
    If you're enjoying my products, updates and support, please consider supporting me on patreon.com!
  • Yea fair, I just didnt want to have to add an extra node each time :P
Sign In or Register to comment.