Search Unity

Question Call all functions with a specific name

Discussion in 'Scripting' started by urumizawa, Mar 26, 2021.

  1. urumizawa

    urumizawa

    Joined:
    Dec 30, 2020
    Posts:
    32
    Hello,

    Currently trying to make a saving system, needless to say that I am new, tackling the issue one step at a time starting with calling a function without knowing what script it is attached to or what gameobject it is on.

    to put it into perspective, let's say I have a QuestTracker script where I keep all the information about the status of the quests, EnemyStats on each enemy with all the info on that particular enemy and a PlayerStats script with all the stats. I want to be able to call a save/load on all of them without having to reference every single one of them or even knowing what gameobject it is. Each one will save/load according to the save/load script it contains.

    I hope that made sense, I couldn't find much info on the internet besides that using an interface is an option which was expected. so, I implement an ISaveable interface on them and then what?

    thanks a lot.
     
  2. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    646
    So, if it is a "Computer/XBox/PS" Game, i think even nitendo etc... this approach works like a charm:


    - Make an Interface "ISaveable",

    implement 2 functions: void Load(Object state), Object Save();

    - give that Interface to each class you want to save,
    to seperate your entities you will need to have a system (common one is to use unique IDs),
    so based on that information you can make a "foreach" loop in your SavingSystem, find all ISaveables,
    interate trough and save, for loading the same approach
     
    urumizawa likes this.