How to Program in Unity: Command Pattern Explained

Learn the fundamentals of the Command Pattern in this new video break down! We'll explain how to use each component of the pattern using c# programming in unity!
This tutorial explains important concepts behind the Command pattern and how to use programming! Today we will walk through an example project to showcase the benefits of using state, the state pattern and state machines in Unity!
This is a new episode in a growing series on this channel: Programming for Production. In this series, we take relatively complex software development topics and try to explain them in easy-to-understand ways. Let's take our programming skills to the next level!
SUPPORT THE CHANNEL:
💛 / iheartgamedev
iHeartGameDev Merch:
💛 www.iheartgamedev.com
WANT MORE?
Interested in learning more about animating characters in Unity? Check out my growing series of tutorials:
✅ • How to Animate Charact...
✦ Like the vid? Please consider Subscribing!
bit.ly/2YdIb6j
✦ Missed out on the last episode?
• How to Move Characters...
SOCIAL:
✦ Discord
/ discord
✦ Twitter
/ iheartgamedev
LINKS FROM THE VIDEO:
🤖 Download Jammo for YOUR game:
assetstore.unity.com/packages...
⭐ And Check Out @Mix and Jam !
/ mixandjam
GEAR:
Lumix G7 Camera: amzn.to/3Kks1gZ
Epomaker GK68 Keyboard: amzn.to/3Ke87EB
Samson C01U Pro Mic: amzn.to/33kDdtG
LG 27" Monitor: amzn.to/3qAHFwS
Lepow 15.6" Portable Monitor: amzn.to/3287OtL
Logitech G604 Mouse: amzn.to/3A6jNV4
Audio-Technica ATH-M50X Headphones: amzn.to/3tBaeMT
X-Pen Artist 15.6 Pro Drawing Tablet: amzn.to/3FE5HLI
Apple 15in MacBook Pro Laptop: amzn.to/3AbKdFc
►TIMESTAMPS:
Intro: 0:00
Classes Deep Dive: 0:45
The Focus of Today: 1:30
Program LifeCyle: 1:45
The Question of the Day: 2:10
The Command Pattern Definition: 2:50
Classes Explained: 3:08
The Concept of the Command Pattern: 3:40
The Structure of the Command Pattern: 4:10
Meta Example: 5:34
CODE STARTS HERE: 6:30
Project Without Command Pattern Overview: 6:40
Refactoring with the Command Pattern: 7:18
Abstract Command Explained: 7:46
Concrete Command Explained: 10:00
Using just the Concrete and Abstract: 11:40
Invoker Explained: 12:12
Important point about the Command Pattern: 13:28
Lightswitch Invoker: 13:58
Decoupling: 15:00
TurnOff Refactor: 15:18
What this Invoker is missing: 16:27
Phillips Hue Example Setup: 16:47
A Better Invoker - LightApp Invoker: 17:22
Undo Functionality: 19:18
Whats might come next: 21:50

Пікірлер: 149

  • @Tarodev
    @Tarodev2 жыл бұрын

    I loved that 'like' example. I was fiddling with some lego, which I had to snap together and put down before moving to the mouse to like the video. Perfectly executed!

  • @iHeartGameDev

    @iHeartGameDev

    2 жыл бұрын

    Rad! Love to hear that the Meta example was able to click 😊 cheers Taro! 🍻

  • @HassaanALal

    @HassaanALal

    Жыл бұрын

    oo, one master of another master's video. This looks like the kingdom of masters :D

  • @obiekt19

    @obiekt19

    8 ай бұрын

    Wonder if Taro noticed that he was one of the house instance owners in 3:34

  • @daffertube
    @daffertube Жыл бұрын

    This is actually the most useful style of editing a video about programming. WAYYYYY easier to understand than listening to a professor talk for an hour with a piece of chalk.

  • @iHeartGameDev

    @iHeartGameDev

    Жыл бұрын

    Thanks very much Dustin!! Glad you liked it!

  • @iHeartGameDev
    @iHeartGameDev2 жыл бұрын

    Over a month of research, recording and editing later... I can’t wait to finally share this tutorial! See you all Sunday! Cheers! 🍻

  • @christopherrapczynski204

    @christopherrapczynski204

    2 жыл бұрын

    I really hope the patreon supporters vote for the combat system, I've been waiting so long for that haha

  • @chethanvenkataramaiah6940

    @chethanvenkataramaiah6940

    Жыл бұрын

    Really appreciate the amount of effort u have put in the video. I know how much of work it takes to pull of these things. Really appreciate that!!!

  • @anthonytrepanier1877
    @anthonytrepanier18772 жыл бұрын

    Hey! I just wanted to say that your tutorials are the most clear and detailed one that we can find on KZread. It may not seem like it but I know that editing this kind of video takes a lot of time. Also, these topics (Design Pattern) are so important in game development. It allows us to complete a game without any frustration. Big congrats and thank you!

  • @iHeartGameDev

    @iHeartGameDev

    2 жыл бұрын

    Thank you so much for the kindness, Anthony!

  • @M3g4t0n
    @M3g4t0n2 жыл бұрын

    Despite the difficulty of me trying to grasp this concept, your explanation is rock solid! I feel that if I watch this once or twice more, I'll actually be able to implement this in my games!

  • @zeeshansattar4531
    @zeeshansattar45312 жыл бұрын

    Please also cover SOLID principles exactly like the way you did state pattern. And obviously this video was helpful like always :)

  • @pewpew518
    @pewpew5182 жыл бұрын

    Give this man a rider license.

  • @marularch
    @marularch5 ай бұрын

    I watched a lot of tutorials to be able to understand everything about the command pattern and adapt it to my needs; your tutorial has the clearest explanation. (I still suggest watching other tutorials too if anyone is having problems)

  • @signalised9540
    @signalised95402 жыл бұрын

    This is honestly one of the best tutorials made on this topic Thanks Nicky!

  • @IPlumpton
    @IPlumpton2 жыл бұрын

    Genuinely one of the best game dev channels going. In a world of racing to the shortest video, it's great to see in depth tutorials like this. Thanks and keep it up! 🙂

  • @jonathandunn9302
    @jonathandunn93022 жыл бұрын

    This is one of the clearest and most concise videos on a subject like this I have ever seen! Kudos!

  • @zaprodaju1661
    @zaprodaju1661 Жыл бұрын

    This is the next level of tutorials. Not only explained in the "command pattern for dummies" kinda way, but it's also animated, well-structured, and super concise.

  • @workflowinmind
    @workflowinmind2 жыл бұрын

    Really love your charisma man, nice to see more advanced topics! Keep up the good work ❤️

  • @alexmilanu8652
    @alexmilanu86522 жыл бұрын

    I love your tutorials and explanations :) Especially for this advanced stuff. You're helping me a lot. Thank you for those videos!

  • @iHeartGameDev

    @iHeartGameDev

    2 жыл бұрын

    Your kindness means the world. Thank you 🙏

  • @ochukomeoghenetega3737
    @ochukomeoghenetega37372 жыл бұрын

    Love this. Nice one man. Always wanted to try this out. Good to have the basic knowledge

  • @lucasafonsolombardimoreira6539
    @lucasafonsolombardimoreira65392 жыл бұрын

    Great video!! just a small correction. At 13:44 STACK is LIFO (Last in - First Out). Love your videos!!!

  • @bitgalaxis1147
    @bitgalaxis1147 Жыл бұрын

    Excellent. I can't say I've ever seen a more complex group of programming topics condensed so clearly in 22 minutes. Excellent concept breakdown, excellent presentation, excellent explanations. For those of you who are somewhat new to programming and struggle to understand concepts like interfaces, abstract classes, or their purposes, watch this video. It will make it clearer. If you already understand these things pretty well, you'll wonder why nobody ever explained it this way to begin with.

  • @mateuszpatua3016
    @mateuszpatua301610 ай бұрын

    the one tutorial I've been looking for! it just clicked! THANK YOU!

  • @basavarajguled848
    @basavarajguled8482 жыл бұрын

    compared to infallible code the way you explain the concept is really effective, not so fast not too slow. keep doing videos and please cover some more paaters and SOLID patter as well

  • @Damnesia2
    @Damnesia2 Жыл бұрын

    I clicked this video out of interest since I've learnt design patterns and have been programming for a while but have only recently heard of this pattern. I ended up realising that despite not knowing this pattern, I've already used it in a personal project for the purposes of compilation of a scripting language I created for a text based action game engine I made back in University. At least now I have a name for this pattern. This just shows how useful this pattern is that despite not knowing of its existence, I ended up using it because it achieved the goal in a clean and efficient way.

  • @mosleydev9973
    @mosleydev99732 жыл бұрын

    Thats what i've been searching for

  • @thelordcthulhu3684
    @thelordcthulhu3684 Жыл бұрын

    Another amazing video. I cant wait to see more use cases of this pattern in the future!

  • @JazzInATinCan
    @JazzInATinCan Жыл бұрын

    This was very informative, well structured, good examples, easy to follow with the refactoring etc. Very high quality, thanks :)

  • @zekiozdemir420
    @zekiozdemir4202 жыл бұрын

    Wow! might be the best tutorial I have seen about this. Thanks

  • @coltraingames1740
    @coltraingames17402 жыл бұрын

    great video, i'd love to see a follow-up showcasing a variety of different applications for this

  • @sorokan6036
    @sorokan6036 Жыл бұрын

    The quality of your videos is amazing! Keep up the good work :)

  • @DylanBurke
    @DylanBurke2 жыл бұрын

    Hey, solid intro on command pattern; thank you for that. One tiny nitpick (mostly for those watching to think about) at 17:05, SetRandomLightColor() should really just pick a random color, then call SetLightColor() feeding in the random color instead of repeating the same logic.

  • @GameDevNerd
    @GameDevNerd2 жыл бұрын

    A better design is to create a CommandProcessor class with a Queue collection that stores commands and processes them. Stack stores the history. But for anything other than trivial commands that finish instantly, you have a lot of challenges to overcome to have accurate undo-ing of commands. The CommandProcessor can be added to any class you want to execute commands, and will be nice and generic so it doesn't care what type of concrete commands it's running. It only cares if it's a simple ICommand or an IUpdatedCommand that needs updates and takes a while to finish. Without this, you can't implement more interesting things like moving a character along 5 waypoints because all of the commands execute instantly and the character will just go straight to the last waypoint instead of following them in sequence and moving on when the active one is complete. It was kind of a challenge for me to implement this sort of system properly, but once I ironed out all of the problems in it I ended up with a reusable system I can apply to literally anything and just create new commands for it to run literally any logic. Some things may to inherit CommandProcessor and override the way it processes commands, so mark its methods virtual. An example is if you're making an RTS game and you want a unit to attack an enemy but if it's given a an order to move or retreat it will cancel the attack command and do as the player commands. Otherwise, it will be stuck attacking until it dies or kills the enemy before it can respond to the next commands enqueued, and it can't do that if it's dead, can it? So the command processor for the units will override the ProcessCommands() method of the CommandProcessor base class and deal with this accordingly, by canceling commands that are followed by a higher priority one like an immediate move, stop, retreat, etc ...

  • @Hiren24
    @Hiren242 жыл бұрын

    Thanks for such a great explanation 😍😍Loved it

  • @radiantgames55
    @radiantgames554 ай бұрын

    I love how you take things from ground up and then build upon that. One of the best videos of command pattern I have ever watched!! ❤❤ Please make more videos of this type.

  • @iHeartGameDev

    @iHeartGameDev

    4 ай бұрын

    Hey thank you for the kind words! I try to make things understandable to myself so I guess it just happens that a lot of people think similar to me 😅 happy to hear you do too!

  • @kizz06
    @kizz062 жыл бұрын

    i watched tons of tutorial videos and you are one of the best. Really appreciate your effort of making animations and stuff to make it really easy to understand.

  • @iHeartGameDev

    @iHeartGameDev

    2 жыл бұрын

    Thank you so much Kizz06! I’m glad you like the videos and appreciate the effort!

  • @tarekchentouf4860
    @tarekchentouf4860 Жыл бұрын

    You are AMAZING! This is a premium quality tutorial. Thank you so much!!!

  • @HarshYadav0069
    @HarshYadav00692 жыл бұрын

    One thing i have to say is, i have seen lots of tutorials of unity and programming about many topics, and there are lots, but all of them are not that much relevant some are and one of them is yours one thing that i love about you and your channel is, your only talks about relevant things means point to point no other useless stuff and this makes your video more interesting. that's why i love your channel. keep uploading this cool stuffs. love you from india...

  • @iHeartGameDev

    @iHeartGameDev

    2 жыл бұрын

    Thank you so much for such a kind comment! 😊 I am happy that these videos offer more and new ways to explain these concepts.

  • @HarshYadav0069

    @HarshYadav0069

    2 жыл бұрын

    @@iHeartGameDev thanku to you bro!! Keep working 🤗

  • @thev01d12
    @thev01d122 жыл бұрын

    Awesome video. Glad someone is covering topics that are necessary to be used in production. Have you thought about making a video on inventory system?

  • @GameplaysJogosPC
    @GameplaysJogosPC2 жыл бұрын

    Excellent video. It is very difficult to find paid content with such quality as this video!

  • @BbVortexMortinghan
    @BbVortexMortinghan2 жыл бұрын

    Good to see you back at it Nicky, I love the videos.

  • @PhantomWolf93
    @PhantomWolf932 жыл бұрын

    Really good tutorial, very interesting, easy and visual. Good work man, i want more videos, i learn a lot with your channel!

  • @jean-michel.houbre
    @jean-michel.houbre2 жыл бұрын

    Very well explained! Thank you !

  • @nikowolfwood
    @nikowolfwood Жыл бұрын

    This video is pure gold. Thanks!!

  • @saqibbro5297
    @saqibbro5297 Жыл бұрын

    Thank you so much for this brother. This means alot!!

  • @chris.davidoff
    @chris.davidoff2 жыл бұрын

    I recently used the command pattern at work, it would have been great to see your video first :P Implementations can be done so differently which is always great to see

  • @julienaudisso3409
    @julienaudisso3409 Жыл бұрын

    It's very very well explained. Thank you and good job !

  • @ajmckennan1663
    @ajmckennan16632 жыл бұрын

    Some of the best content on youtube!

  • @sangareshwarannadar_official
    @sangareshwarannadar_official2 жыл бұрын

    The video Is good worth waiting

  • @kepcukhusamettin9400
    @kepcukhusamettin9400 Жыл бұрын

    Very high quality tutorial, thanks!

  • @Ironlionm4n
    @Ironlionm4n Жыл бұрын

    Awesome video, cant wait for the Observer Pattern video later today

  • @Dk-Ang
    @Dk-Ang2 жыл бұрын

    Nice tutorial!!

  • @raingame_
    @raingame_ Жыл бұрын

    So good tutorial! applause!

  • @KennethBoneth
    @KennethBoneth2 жыл бұрын

    Really well done.

  • @orpheuscreativeco9236
    @orpheuscreativeco9236 Жыл бұрын

    I was wondering _how_ to work the "Banana Command Class" into my code, thank you so much! ✌️

  • @grumpysalmon4153
    @grumpysalmon4153 Жыл бұрын

    Super helpful! Love your videos

  • @tekmontekmon8098
    @tekmontekmon8098 Жыл бұрын

    Amazing video ,thanks for sharing

  • @babarrehman7967
    @babarrehman7967 Жыл бұрын

    Bro you should make a series of coding it is really difficult and your way of explaining is owesome

  • @iHeartGameDev

    @iHeartGameDev

    Жыл бұрын

    My next video will be on another pattern!

  • @Introboy13
    @Introboy132 жыл бұрын

    Broooo! This is sooo detailed AF! Thank you! 🎮

  • @iHeartGameDev

    @iHeartGameDev

    2 жыл бұрын

    Cheers mate! Thank you for the love!

  • @vrfxrealtime
    @vrfxrealtime Жыл бұрын

    This was great! Please More :)

  • @TheKr0ckeR
    @TheKr0ckeR Жыл бұрын

    This video-editing is very high level.

  • @ahmadelmir7660
    @ahmadelmir76602 жыл бұрын

    Thank you for all your tutorials about Unity. They are so helpful. Could you make some Tutorials about the Unity particle system? How to use this tool and how to use it with animation. :)

  • @bobaman8957
    @bobaman89572 жыл бұрын

    ❤️❤️❤️ More design patterns ❤️❤️

  • @user-ij6el8gw5y
    @user-ij6el8gw5y2 ай бұрын

    I really liked the analogy with the like this video command, smart!

  • @iHeartGameDev

    @iHeartGameDev

    2 ай бұрын

    Thank you :)

  • @Churagawa
    @Churagawa Жыл бұрын

    Awesome tutorials dude. Really. Would love to see some command queue handling :3, also callbacks from command

  • @SigmaZer0
    @SigmaZer0 Жыл бұрын

    Excellent!

  • @losfouad2014
    @losfouad20142 жыл бұрын

    Thank you for this awesome tutorial 💙

  • @iHeartGameDev

    @iHeartGameDev

    2 жыл бұрын

    Thank you for watching!

  • @nguyenhuuphu8371
    @nguyenhuuphu83712 жыл бұрын

    I wish people would know your channel more and more

  • @dibaterman
    @dibaterman2 жыл бұрын

    So I've decided that my first game dev related income I'll put $50 towards patreon, if anything your videos keep me going. Today I am going to be working with a asset I got off Unity Store to do some level design. I have an idea for the way the game will look, it's general loop, and the scenes needed to facilitate this for the purpose of making a demo. Up until now I had been doing prototypes where I test out ideas and mess with things to get familiar with coding and developing in Unity. Each one of these prototypes I figured I'd be using in some way on my final project. I realized it's a lot easier to be inspired to develop my final project if I can see a level instead of working with primitives. So I'll be working on 2 combat maps and one Town map. I still haven't really landed on what the world map will look like. After that I will have a separate project for developing the UI visuals. Right now I think I'm at a point where I've taken in all of this stuff and I can use it but without your videos I'd forget I know this stuff. This means to me I'm in need to some way to note down patterns and possibly I should refer to my old projects more. Anyway thank you for being you.

  • @kenji0q
    @kenji0q2 жыл бұрын

    좋은 영상 감사해요 객체지향으로 코드를 구성하는데 도움이 될거같아요 개발자는 언제나 공부를 해야하네요!

  • @renatusdev
    @renatusdev2 жыл бұрын

    This is the most unique way someone got me to press the like button. I literally had to press it just to understand the concept of an invoker hahahaha

  • @iHeartGameDev

    @iHeartGameDev

    2 жыл бұрын

    Lol happy you understand it now though!

  • @goehlergamedev
    @goehlergamedev Жыл бұрын

    Really great videos! 😁 👍

  • @vima9046
    @vima9046 Жыл бұрын

    Good tutorial!

  • @Landon_Hughes
    @Landon_Hughes9 ай бұрын

    Not sure how I found this video, but thanks! I’m not a C# dev but I can see where this would be useful in Swift 😁

  • @Wobling
    @Wobling2 жыл бұрын

    Great video

  • @V1113sh
    @V1113sh Жыл бұрын

    Thank you

  • @SkaiCloud562
    @SkaiCloud562 Жыл бұрын

    Took me a whole year. My brain finally invoke the subscribe button.

  • @brianwilson7497
    @brianwilson7497 Жыл бұрын

    dude, fantastic, thank you

  • @iHeartGameDev

    @iHeartGameDev

    Жыл бұрын

    Thanks for watching Brian!

  • @Javiwido
    @Javiwido2 жыл бұрын

    You got your like, bc that was a great meta example. Besides, my hand was also busy with coffee.

  • @Nomedeusuariodoluiz
    @Nomedeusuariodoluiz2 жыл бұрын

    Awesome!

  • @Natural_Mindset
    @Natural_Mindset Жыл бұрын

    Меня ещё никогда так изящно не сподвигали лайкнуть видео))

  • @ponchitosalcedo2588
    @ponchitosalcedo2588 Жыл бұрын

    nice ! runs great on my laptop on medium settings :)

  • @oaksBFL
    @oaksBFL Жыл бұрын

    thx and subscribed

  • @tugan_aa
    @tugan_aa10 ай бұрын

    Hello IHeartGameDev, as a programmer I want to say that you have explained the basics of OOP very well and thank you for that but I wouldn't call it delaying the execution. And since you have started with the OOP concept you could take a look at abstraction, encapsulation, inheritance and polymorphism. I am not sure if you guys are using these concepts on Game Programming but I would love to help you out if you ever need.

  • @royvictorgagarin55
    @royvictorgagarin55 Жыл бұрын

    Awesome!

  • @romanblack1420
    @romanblack14202 жыл бұрын

    good tutorial

  • @Brovider
    @Brovider2 жыл бұрын

    I love you man you're the best

  • @iHeartGameDev

    @iHeartGameDev

    2 жыл бұрын

    Thanks so much for the kindness!

  • @DanielS-yf4me
    @DanielS-yf4me Жыл бұрын

    Its been a great help, I just wish the example wasnt so contrived. Additionally the one of the things you mentioned was that commands allow for delayed actions, but that was completely ignored in the example because the input immediately caused action.

  • @Ligerff346
    @Ligerff3462 жыл бұрын

    perfect also when u will be making practical use of it my req is that also show use of conservation with it

  • @Overdrink7
    @Overdrink77 ай бұрын

    Even if I know this pattern, I wouldn't have explained it better :D

  • @iHeartGameDev

    @iHeartGameDev

    7 ай бұрын

    Thank you!

  • @WeirdGoat
    @WeirdGoat Жыл бұрын

    Great video! Just 1 question, what's the difference between Command and Observer pattern?

  • @zeggxr1539
    @zeggxr1539 Жыл бұрын

    Would like to see some systems like : inventory system, grid system, merge system, stat system

  • @muhammetuymaz2961
    @muhammetuymaz2961 Жыл бұрын

    13:44 Stack should be last in first out

  • @neelanshpandey455
    @neelanshpandey4552 жыл бұрын

    Hey, Can you create tutorials on how to setup character animation with multidirectional animation for twin stick shooter...thanks for your help, your great...and sorry for my bad English

  • @iHeartGameDev

    @iHeartGameDev

    2 жыл бұрын

    Hey! That sounds fun to do one day!!

  • @noodlekopf2443
    @noodlekopf2443 Жыл бұрын

    Would this be useful to damage an enemy in a multiplayer game by different players or is it fine to just subtract it directly? Can someone give a example for a not turn based game?

  • @AlexBlackfrost
    @AlexBlackfrost2 жыл бұрын

    5:50 my invoker (Brain.cs) had already liked the video on the Awake() function 😎. Great video, man!

  • @ugurkarabulut8598
    @ugurkarabulut8598 Жыл бұрын

    Perfect tutorial can u make exactly same for factory and abscract factory pattern :D

  • @mohsenmousavi2313
    @mohsenmousavi23132 жыл бұрын

    💚💚💚

  • @MsDeniz2001
    @MsDeniz20012 жыл бұрын

    Hey can you demonstrate multiplayer game development ?

  • @muhammadjamal347
    @muhammadjamal347Ай бұрын

    Why did you execute newCommand in lightApp invoker in addCommand method? arent they supposed to be different? newCommand.execute() in execute() part of invoker and pushing command to stack in addCommand() ?

  • @soonhongng7037
    @soonhongng703711 ай бұрын

    Damn, the way you suggested subscription is damn sick 😂

  • @iHeartGameDev

    @iHeartGameDev

    11 ай бұрын

    Thank you! I thought it made sense at the time!

  • @Norbyscook
    @Norbyscook8 ай бұрын

    woaaaah,

  • @SakanaKuKuRu
    @SakanaKuKuRu10 ай бұрын

    I keep thinking about the lazy lambda functions when delay is mentioned

  • @ultramelon3960
    @ultramelon3960 Жыл бұрын

    Why not also pull the command from the stack on undo?

  • @weckar
    @weckar2 жыл бұрын

    Wait a minute.... If you add multiple change Colors into the queue, won't they all believe they have the same 'previous color'? For a proper undo, shouldn't state be saved at execution?

  • @iHeartGameDev

    @iHeartGameDev

    2 жыл бұрын

    Ah I think I see what you are saying. In this example, we execute immediately after instantiation which is why it works without a problem. In the case that we didn't want to execute immediately, that would be necessary. Good point!

  • @weckar

    @weckar

    2 жыл бұрын

    @@iHeartGameDev Delayed execution being one of the main advantages of the pattern :)

  • @iHeartGameDev

    @iHeartGameDev

    2 жыл бұрын

    @@weckar total oversight on my part. Thank you for pointing it out, Weckar. Much appreciated!