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.?
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.
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.