Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Displaying Serialized fields in one script as elements of another.

Discussion in 'Code Editors & IDEs' started by jnbbender, Oct 10, 2023.

  1. jnbbender

    jnbbender

    Joined:
    May 25, 2017
    Posts:
    483
    I am attempting to create a save system but want as little intrusion as possible. I would like to do what EasySave does and that is, when I add a SaveGameObject.cs script to an object in the hierarchy, it will load all serialized fields from all other scripts attached to the object and add them as select-able fields to the SaveGameObject.cs.

    I am terrible with custom inspector stuff but if I can just figure out how to get a list of SerializedFields from the other scripts, that would be a great start.

    Thanks,
    J
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,994
    You're looking at reflection. Find all public non-static fields and all non-public, non-static fields flagged with [SerializeField] attribute and oh wait don't forget about skipping fields with [NonSerialized] attribute and don't forget about looking in base classes, too.

    This will quickly get pretty involved, and just saving all serialized fields just isn't going to cut it for a savegame system. You also have to consider that at some point you're going to make an update to your game and users will want to be able to load their old savegames despite the numerous changes you've made. But with this approach at that point you are in hell, because it's either working weeks on the near unsolvable or face frustrated players (probably both because loading old saves is likely going to cause issues no matter how much work you put into that).

    Meaning: you have to save data purposefully and with versioning. Non-intrusive savegame solutions, as in "plug and play" like EasySave, are effectively becoming broken solutions after you've released your game. They're okay for quick & dirty or for development, but no more.

    For further reading see Kurt's post here:
    https://forum.unity.com/threads/how-to-save-scriptableobject-data-in-runtime.1504319/#post-9400829
     
  3. jnbbender

    jnbbender

    Joined:
    May 25, 2017
    Posts:
    483
    That's awesome. Thanks for the input. Never thought of all those issues.