I'm developing a Command Pattern with Undo/Redo support. The environment is Unity C# but should be mostly language-agnostic.
My initial commands comprise of adding and removing objects to and from a space, but I'm encountering commands that lose their reference to their object. Removing an object means destroying it, so if I:
add an object
remove an object
undo
then the first command that adds the object will have its own internal reference to the object nullified since the Remove command destroys the original and creates a new one. I've done some digging and have found this described in this blog post but no solutions were posted.
I have some ideas on how to solve this but would like some input.
- Set up some callbacks so that commands register themselves with an object, and can be notified if the reference changes. This might be overengineering.
- Don't destroy the object on removal, just deactivate/hide them. This seems to be an easier way to achieve it, but I'm worried about memory usage in the app since the objects will have meshes attached to them.
- Set up a registry of some sort that, given the correct ID, the commands can query for the correct object that they should be referencing. This definitely seems overkill.
I probably shouldn't optimise early, but the domain for the application is pretty strict. Are these good ways of solving the problem? What else can I try?