Command Pattern in Unity, Part 2: Implementation

preview_player
Показать описание
Implementing the command pattern for use in a Unity project, so that each action taken is encapsulated into a command object.

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:

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

Love the videos, would like to see more design patterns. The observer pattern handling high level game state and also per instance game objects would be great!

mana
Автор

Easily my favorite tutorial channel. Keep up the great work, man!

MrHandsy
Автор

Great stuff, subscribed and liked! Video's about the State & Observer pattern would be amazing too. Looking forward to more of your video's!

tehuster
Автор

Extremely immense. Thanks loads for this.

quadtychgort
Автор

Awesome, lookin forward to the next video!

CCV
Автор

Nice work man. I'm coming from a UE4 forum looking to implement Command Pattern there.
I like your approach and explaination. Maybe it can be improved somehow based on a usecaase

meow
Автор

In the command invoker script, if there are multiple commands in the buffer, the script will only execute one per frame. Shouldn't we loop using "while count>0" to execute all commands in the buffer one by one instead of using "if" so that all commands that are added in the same frame are also executed in the same frame?

MayurGarg
Автор

I don't know if you will answer this but in any case I will try. What if our execute function takes time to complete and we need to wait for running command's complation before invoking the next in line, how would we implement it?

trmechanic
Автор

Hey what if you have for example a bunch of commands that do level building another set of commands for UI and another set to move a character. What is a proper way to activate/desactivate a set of commands and keep track which commands you want to use?

Brian-fpkp
Автор

Is there a specific reason you didn't instantiate the queue inline instead of awake? Is there a difference between the two methods?

mana
Автор

Hi, I'm sort of getting it but I'm a little bit confused on how things are working. I'm a bit confused on the type ICommand because ICommand is mostly empty. Why are we using it so much? We call ICommand.Execute() but Execute() is empty on the ICommand. If we store a Command in this variable, doesn't it become an ICommand? I know I'm confusing interface with "base class" here, but I don't understand what the interface does. When we say this variable can store an ICommand, are we saying that it can store any type of scripts that inherits from ICommand regardless of its actual class/type?

If class Dog inherits from an interace IAnimal which has commands Sleep() and Eat(), can I place an object of type Dog in a variable meant to hold ICommands? If I do, is it still a Dog? Judging from what you say here, it seems like I can access an IAnimal variable like Sleep() and it will execute the Sleep() command not on the IAnimal script, but on the Dog script. Could I also use this technique to access other functions on the Dog such as Bark()?

If I have:
IAnimal animal;
Dog fluffy;
animal = fluffy;
Does animal now reference fluffy, or it some kind of copy of another object altogether?

cetiah
Автор

Sorry if I missed this, but what is the benefit of using this pattern in a game? What example mechanics would benefit from this? Performance? (I did see you use thing in your Camera Controller series)

EDIT: rewatched the first video and still did not quite get it, but I'm not too technical when it comes to the theory. I just do it and see what happens lol
.

KeiNovak
Автор

When using Command pattern would it be preferred to keep all command classes in a single file, add them to the bottom of relevant files or keep them each in a separate file like you seem to be building up to here?

NickMarcha
Автор

Hey man! I have a question. How would you handle it if some of your commands had a state. So instead of simply immediately dequeueing and running it you had to wait for some sort of secondary input like on mouse up or a second click. Lets say I have to click and drag to stretch something and stretching is a command how would I use this queue system?

Haunchfluff
Автор

what does the "physics.raycast()" function? thx :)

FBAzekFB
Автор

Why are you using an Interface instead of a base class?

hidsgi-games
Автор

why are you using such a weird abstract convoluted example. Just instantiate a cube to remove confusion, seriously that weird ray stuff changing colors really took away from the important stuff. Really distracting.

BobrLovr
join shbcf.ru