Search Unity

Saving/Restoring the state of a gameobject

Discussion in 'Scripting' started by Kev00, Jan 30, 2019.

  1. Kev00

    Kev00

    Joined:
    Dec 6, 2016
    Posts:
    229
    Can anyone suggest a more elegant / preferred way to save and restore the initial state of a game object?

    Code (CSharp):
    1.  public class GameObjectStateSaver
    2.     {
    3.         private GameObject obj;
    4.         private Vector3 position;
    5.         private Quaternion rotation;
    6.         private Vector3 localScale;
    7.         private GameObject parent;
    8.         private bool active;
    9.  
    10.         public GameObjectStateSaver(GameObject obj)
    11.         {
    12.             this.obj = obj;
    13.  
    14.             position = obj.transform.position;
    15.             rotation = obj.transform.rotation;
    16.             localScale = obj.transform.localScale;
    17.             parent = obj.transform.parent.gameObject;
    18.             active = obj.activeSelf;
    19.         }
    20.  
    21.         public void Revert()
    22.         {
    23.             obj.transform.position = position;
    24.             obj.transform.rotation = rotation;
    25.             obj.transform.localScale = localScale;
    26.             obj.transform.SetParent(parent.transform);
    27.             obj.SetActive(active);
    28.         }
    29.     }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    This is not really the approach you want for save game use. Instead, identify the necessary parts you need to save for state-of-a-game, and isolate those parts and only save those parts.

    Keep in mind also you may need to change your scripts so that they can be created blank, then subsequently post-creation initialized back to any arbitrary later-in-game state. Doing this may be trivial, or it may require a complete refactor.

    In any case, the idea behind game saving is to save JUST what you need and nothing more.
     
  3. Kev00

    Kev00

    Joined:
    Dec 6, 2016
    Posts:
    229
    oh sorry I should have been more clear about the usage of this class. It isn't for saving the game. It's for an object pool.

    For example, I have a number of objects that need to get reused (like projectiles) and I want to revert them back to their configured state.

    It would be great if I could take an object (and all its children) and just restore it to its original prefab state.
     
    Last edited: Jan 31, 2019
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    For object pools I just call .SetActive(false) on the root game object and forget about it.

    When it's time to resurrect the object for reuse, you already have to be sure you initialize all fields, so just do that.

    That will take care of it in one place that requires no additional maintenance beyond keeping it up to date for that object, which in the case of necessary initializers, you have to do anyway just to use it.
     
  5. Kev00

    Kev00

    Joined:
    Dec 6, 2016
    Posts:
    229
    Yeah, that's what I'm doing.

    The problem is that child objects could have different positions at the time the object is disabled. Those child objects all need to be reset to their original prefab state.

    My solution is just to iterate over all the child transforms and save / revert their state (using the above class) when they are enabled/disabled.

    It just seems like such a common thing to be forced to do all the time. I wanted to ask if anyone had a better solution.

    If Unity had a Reset function. I'd be happy.
     
    VengadoraVG likes this.
  6. VengadoraVG

    VengadoraVG

    Joined:
    Nov 21, 2015
    Posts:
    13
    I am doing the same thing Kev00 is doing, it'd be awesome to have this feature.

    I'm doing it by hand now, and it's true... it is not "elegant". A feature that just allows us to save a game object state at any point, and then tell the object to restore it's state at any point in the game would help a lot on object pooling.

    It'd have to work, not only for the game object transform and active/unactive state, but also for all of his components. (And children)
     
  7. mbaske

    mbaske

    Joined:
    Dec 31, 2017
    Posts:
    473