Search Unity

Command Pattern - how to resurrect what has been killed?

Discussion in 'Scripting' started by c-Row, May 22, 2022.

  1. c-Row

    c-Row

    Joined:
    Nov 10, 2009
    Posts:
    853
    I have been looking into the Command Pattern to add some additional functionality into my game, mostly the ability for the player to undo their actions. The game involves moving agents across the game map, and following the description from Robert Nystrom's "Game Programming Patterns" the best way to handle this seems to be to keep track of both the agent's old and new position to easily move them back to their original place.

    However, something that has not been covered in any of the tutorials I have been looking at is - how can an agent that has been destroyed be brought back to life if the respective GameObject does not exist anymore to rebuild it from including all its previous attributes, modifiers etc.?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,741
    I'm not sure there's a general case way that would work in 100% of game contexts and perfectly restore the state of a given object.

    You're also assuming that the object's destruction doesn't have side effects in other global systems, such as giving score points, or tallying overall progress, team size, tripping other conditions ("time for more enemies!"), etc.

    All of those would need to be rewound as well.
     
  3. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    the action that Destroyed it would also be a Command, and that commands "Do" implementation would record the state of the object, and its "Undo" implementation would describe how to recreate the instance and restore its old state.
     
    c-Row likes this.