Hi all, over the holiday period I decided to take some time out to try an experiment: attempt to add undo functionality to the game. Specifically, implement a command history, to which a command would be added for each tool action (e.g. select, move, or attach parts, apply material or paint, etc.) and then allow the player to undo or redo the commands in that history.
The game’s tools were not originally built with this in mind, but the code is reasonably well structured so I thought it might be possible. The initial prototype turned out to be promising, and showed the potential improvement to usability this would bring.
Well, I couldn’t resist seeing this through to completion, and so that’s what I’ve been working on for the past few weeks. It required substantial refactoring of some of the tool code, and took a while, but the results are well worth it I think.
Here it is in action, I’ve exposed the command history to Lua scripting, and made a script mod that shows the current list of commands:-
Rather than store an absolute snapshot every time something changes, the commands in the history store relative deltas. A command keeps a record of the change that was made by a tool action. This means implementation was kind of an “all or nothing” prospect: all tool actions must be accounted for the command history (in the right order!) for everything to work properly.
Here are the commands I ended up with, these are added to history when…
Select tool: …a tool is selected (some actions are only valid when the
correct tool is active, so this must go in the history).
Select parts: …any frozen parts are selected or deselected.
Select pivot: …a pivot point is selected, which detaches all the selected part(s).
Move / resize selection: …the selected part(s) are translated, rotated, or resized via manipulators.
Replace attachment: …an attachment’s type is changed.
Lock attachment: …an attachment is locked or unlocked.
Delete attachments: …attachments are deleted (whether directly, due to the
selection being detached, or due to a part being destroyed).
Spawn / destroy construction: …a part / construction is spawned or destroyed.
Freeze construction: …a construction is frozen or unfrozen.
Swap material: …a material is applied to a part.
Add / remove links: …link(s) between link nodes are added or removed.
Apply paint: …paint colour is applied to a part.
The command history has a buffer containing references to the added commands, in the order they were added. This allows for the commands to be undone in reverse order, and redone in forward order.
The history maintains an index of the last undone command, and if a new command is added, it will go into the buffer at this point. Any commands that came after the one most recently undone will be lost.
The buffer is a finite size (currently 256), if a command is added to history and it’s full, the oldest command is removed.
So, the game now has full undo / redo capability (using Ctrl + Z / Ctrl + Y). This is a feature that this genre of game really should have, and I think it was worth making the effort to get it done.
There are still a couple of issues left to deal with, but it’s now in a usable state, so I’ve just updated the demo to include it. A lot of code had to change, hopefully I haven’t introduced any glaring bugs, it seems stable so far. Give it a try and let me know how you get on with it!