Command Pattern in Unity, Part 3: Undo/Redo Functionality

preview_player
Показать описание
Using the Command Pattern to enable players to undo and redo actions through the use of a command history.

Complete code will be released at the end of the series on GitHub, or become a Patreon supporter for instant access.

Support Board to Bits on Patreon:

Рекомендации по теме
Комментарии
Автор

First of all - i like the way Command is presented, thank you for this video.
My note is, like someone also noticed, about that part where in code the created cubes are identified by their position/color. I would also propose to use Resources.Load to load the prefab in CubePlacer.PlaceCube - in that case PlaceCube should only know the prefab name (string) to load from resources
RemoveCube then becomes just a 2 lines
cubes.Remove(cube.transform);
GameObject.DestroyImmediate(cube, true);

PlaceCubeCommand.Undo also needs to be changed to


that way its no longer needed to identify the cubes to delete by their pos/color

venelinvasilev
Автор

2 things:
1. This video and part 4 is are swapped in the playlist.
2. 2:04 Wouldn't this potentially get the wrong command if you place 2 cubes in the exact same position and color? For your example, it works ok since it would be very rare to place two cubes in the same spot and get the exact some color since you're randomizing color but on a grid based game with discrete colors, you wouldn't be so lucky. I changed this to simply destroy the gameobject at the end of the cubes list using - 1].gameObject); and then remove it from the list with cubes.RemoveAt(cubes.Count - 1); No need to use a for loop.
I'm fairly new to OOP so I might be completely wrong.
I'd appreciate your input

RobertShane
Автор

These are some of the best tutorials I've come across and are really helping broaden my knowledge of C# and Unity. Very appreciated. I just finished your Procedural Mesh tutorials from a long while ago and was wondering if you ever planned to expand them any further? Worthwhile tutorials on this subject feel very hard to find.

danzilthard.
Автор

I like the way you simplify code for beginners, unlike some tuts

humadi
Автор

Generaly using a Stack<ICommand> instead of List<ICommand> is more suitable for implementing "undo" functionality.

behnamrasooli
Автор

Thanks for the tutorial! I have noticed tutorial will work for an object staying still. How can you track a movable object and have record it's movement, but at the same time, be able to undo and redo?

Halfscreen
Автор

Is it posible to program the redo-undo funcionallity with the serializedObject property?

Cynbell
Автор

Seeing as I had a Rigidbody on the cubes in my test scene, I decided to store a reference to the cube.transform.InstanceID in the PlaceCubeCommand.cs (making the CubePlacer.PlaceCube return the instanciated ID and CubePlacer.RemoveCube use it). Seemed to work fine. Wouldn't this be better practice in most circumstances?

NickMarcha
Автор

Thank! this was great and the right level of simplicity to understand conceptually. I was wondering if you are still planning to put this on github or will keep it for Patreon only? I seem to have made a mistake and am not destroying objects and would like to cross check. thankyou again for the very understandable piece of work

jamesh
welcome to shbcf.ru