Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

[Solved] Saving a reference

Discussion in 'Scripting' started by joshcamas, Mar 11, 2018.

  1. joshcamas

    joshcamas

    Joined:
    Jun 16, 2017
    Posts:
    1,242
    Hello friends :)

    I'm assuming this is impossible, but basically I have a save file that references scriptable objects. Since these scriptable objects are unchanged from when they were created, I would like to save a reference to them, instead of saving them. This is to cut down on save space and also allow for changes to the data (ie changing the scriptable object in editor) and being able to apply this info to the save file.

    One idea is to somehow save the path on scriptable objects, and perhaps if they were in the resource folder I could use that as a reference?

    Another idea is to save the type of scriptable object as a string, then converting it to a Type and using Resources.FindObjectOfType whenever I want to reload it.

    Any thoughts?

    Josh
     
    Last edited: Mar 11, 2018
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,131
    I'm assuming you mean when you serialize data for a save file.

    In which case... you have to create a serialized version of a reference link.

    For instance in the unity serializer for scenes and what not. This is done by linked the object guid (that's what the meta files are for, they hold information like this guid).

    This won't work at runtime though. So instead you'll have to serialize it as some sort of generated id of your own. This id should be able to match to some lookup table of some sort. So like if the object you want to link is a resource, you can use the resource path as the id, since you can look it up by that path. If it's a scene object... well that can get more complicated since you need that scene loaded and there's no guarantee that the object exists.
     
    joshcamas likes this.
  3. joshcamas

    joshcamas

    Joined:
    Jun 16, 2017
    Posts:
    1,242
    I was thinking about that. It just seems sorta clunky though... But honestly anything involving serialization in unity is guaranteed to be clunky.

    It's sorta sad how id's aren't saved during runtime, it would solve maaaany of my problems
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,131
    If you mean the guid's, well that's probably because it would increase the size of the project. And increase memory usage as they'd have to build a look-up table of the guid's so that they were useful for anything.

    And it doesn't get around the fact that at runtime, just because you know the guid. Doesn't mean the object in question is loaded and available.

    And regardless... saving that guid would be saving an 'id' of some sort. Serializing a reference isn't really a thing... you have to save a token of that reference (rather than the token of that object). And that token is usually in the form of an id. You just need to define an id and facilitate the look-up table of that id (therefore only using up the memory/resources required for exactly that what you need savable... rather than bloating up the system with creating this for every possible object).

    An implementation of this can be clunky. But that's a clunky implementation, the idea of serializing an id is not clunky inherently.
     
    eses and joshcamas like this.
  5. joshcamas

    joshcamas

    Joined:
    Jun 16, 2017
    Posts:
    1,242
    Hmmm okay. I was thinking having an "Asset Library" that would hold a lot of data would be useful, and I guess I will implement this for the sake of having a lookup table :)
    Not too complex either, since whenever I want to save the reference all I need to do is check that table to see it's GUID, and of course on load all I need to do is plug that GUID in to get the value. Awesome :D

    I'm surprised a GUID packed into every scriptable object would add that much bloat. I guess that explains why Unity doesn't do it though.

    Thanks!
     
  6. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,131
    It's not the guid in every object necessarily. It's what's the point of that guid if a look-up table doesn't exist. And that look-up table adds bloat.
     
    joshcamas likes this.
  7. joshcamas

    joshcamas

    Joined:
    Jun 16, 2017
    Posts:
    1,242
    Ahhh that makes a lot more sense for sure!
     
  8. joshcamas

    joshcamas

    Joined:
    Jun 16, 2017
    Posts:
    1,242
    Alright, the lookup table thing worked! I even added a little custom editor for all scriptable objects that lets you add and remove it from a subtable (just a group of objects with an id)! It's not pretty yet, I plan on adding some sort of catcher (editor tool that scans for assets that are certain types, and then tells me about it). I find it's really easy to miss adding an item to the table, which can be annoying.