background top icon
background center wave icon
background filled rhombus icon
background two lines icon
background stroke rhombus icon

Download "How to use the Command Pattern (Skill Combos Example)"

input logo icon
Video tags
|

Video tags

unity tutorial
unity arcade game
unity game tutorial
unity
game development
game dev
game development unity
programming
c#
software development
learn to code
learn programming
unity tutorials
unity devlog
indiedev
gamedev
unity3d
unity command pattern
unity skill combo
Subtitles
|

Subtitles

subtitles menu arrow
  • ruRussian
Download
00:00:00
command is a powerful pattern to know
00:00:02
because essentially you're wrapping a
00:00:04
method into an object this is perfect
00:00:07
for strategy or turn-based games or in
00:00:09
the example today allowing our player to
00:00:12
organize combos for their character now
00:00:14
once your method is wrapped up in an
00:00:16
object it can have properties you can
00:00:18
store it in a variable and you can
00:00:20
execute it right away or in 5 minutes
00:00:22
you can pass it around to other objects
00:00:24
or you can serialize it and even send it
00:00:26
over the network shout out to anas whose
00:00:29
comment inspired today's example let's
00:00:31
get into
00:00:34
[Music]
00:00:36
it let's start by defining a class for
00:00:39
our hero I want all of my commands to
00:00:41
execute animations so I've written a
00:00:43
small animation manager class already
00:00:45
I'm going to make it lazy load using the
00:00:46
null coalescing assignment operator here
00:00:49
if the assignment manager is not null
00:00:51
use it otherwise assign the animation
00:00:53
manager to the variable and use that
00:00:55
next we need some public methods that we
00:00:57
can wrap in our commands I'm going to
00:00:59
expose three methods attack Spin and
00:01:01
jump and I really want to execute all of
00:01:04
these skills for all of the creatures in
00:01:06
my game using commands so I'm going to
00:01:08
extract an interface here called I
00:01:10
entity that we can use to enforce that
00:01:13
contract I'll move that interface into
00:01:15
its own file and then we can get to the
00:01:17
command now all commands will require
00:01:20
one method which is execute classes that
00:01:23
use a command object don't need to know
00:01:25
anything about the method or methods
00:01:27
being called inside the execute method
00:01:30
they only need to know that they have a
00:01:31
command and it can be executed I'm going
00:01:34
to make it return a task as well this
00:01:37
isn't a requirement of the command
00:01:38
pattern but I know that I want each
00:01:41
command to run for a minimum amount of
00:01:43
time because they all have animations if
00:01:46
I had a command that didn't have an
00:01:47
animation in this scenario I could just
00:01:49
make it wait for zero I'm going to make
00:01:51
a base abstract class for all of the
00:01:53
commands related to my hero so the hero
00:01:56
is going to have to store a reference to
00:01:58
the I entity which is my hero in this
00:02:01
case and we can take that in through the
00:02:04
Constructor here we'll make it protected
00:02:06
so that it can be called by the classes
00:02:08
that are going to extend this now here
00:02:11
I'll just have an abstract task execute
00:02:13
the concrete versions of this class can
00:02:15
fill that out I also want to have a
00:02:17
static Factory method here to create
00:02:19
commands so anytime I want to create a
00:02:21
hero command I can call the static
00:02:23
create method here and passing the type
00:02:25
of command I want as type t for those of
00:02:28
you who haven't seen this before
00:02:29
activator create instance is a static
00:02:32
method of the system activator class and
00:02:34
has numerous overloads one common use
00:02:37
for it is to dynamically create an
00:02:39
instance of a type it's especially
00:02:41
useful when the type of the object is
00:02:43
not known at compile time let's make our
00:02:46
first concrete command the attack
00:02:48
command will take in a reference to our
00:02:50
hero in the Constructor now the execute
00:02:52
method for this command is going to run
00:02:54
the attack method on the hero then call
00:02:56
the animations. attack method which
00:02:59
returns its duration Now using the new
00:03:01
awaitable class in unity 2023 we can
00:03:04
wait for that duration before running
00:03:06
the final method which is to switch back
00:03:08
to our idle animation now if you aren't
00:03:11
using Unity 2023 you could use task
00:03:14
delay or enforce a weight with other
00:03:17
methods maybe a CO routine now before we
00:03:19
go too much further I just want to take
00:03:21
a quick peek at the animation manager
00:03:23
that I wrote now some of you that have
00:03:25
been watching the channel for a while
00:03:26
know that I don't really like dealing
00:03:28
with the mechany system and so what I
00:03:30
prefer to do is write a little
00:03:32
management class if there's not a lot of
00:03:34
Animation so here you can see I've got
00:03:36
public methods for attack spin jump and
00:03:38
idle all of those methods called the
00:03:40
private method play animation which just
00:03:43
does a cross fade and then Returns the
00:03:46
duration now if we look into the data
00:03:48
I've got here stashed away in this
00:03:50
region I've got all my attack hashes
00:03:53
being calculated using the animator
00:03:55
string to Hash method I've defined my
00:03:57
Crossfade duration and then I've set up
00:03:59
a a little dictionary based on those
00:04:01
hashes and how long I want all of these
00:04:03
animations to run now this works really
00:04:06
well if you just have a small amount of
00:04:08
animations it's easy to set this up no
00:04:10
problem but let's go back into unity and
00:04:13
have a look at how many animations there
00:04:14
actually are for this character you can
00:04:16
see there's about 20 of them let's blow
00:04:19
this up a little bit with shift space so
00:04:22
you can see by the time you add all your
00:04:23
Transitions and all your parameters this
00:04:26
little window here is going to start to
00:04:28
look like animation hell and I'm just
00:04:31
going to superimpose an image on here
00:04:33
that you know trying to deal with
00:04:36
something like this is just almost
00:04:37
impossible and it drives me crazy uh so
00:04:41
uh before we move on I just want to
00:04:42
point out one tool that I like to use
00:04:45
when it when I get a lot of animations
00:04:47
or I have to start dealing with
00:04:48
animation layers and blend trees and
00:04:50
that's animancer Pro So animancer Pro
00:04:53
does basically a souped up version of
00:04:55
what I just did in code it lets you
00:04:58
programmatically control all of your
00:05:00
animations highly recommended let's get
00:05:03
back into code here and write up our
00:05:06
last two commands so we'll have a spin
00:05:07
command and we need a jump command as
00:05:10
well just before I add the last command
00:05:12
I'm going to move all of these hero
00:05:14
commands into their own file so yeah
00:05:16
let's add one jump command here co-pilot
00:05:18
will fill it out cuz it's basically the
00:05:20
same as all the others so the final
00:05:22
thing we need is a class that can run
00:05:24
our commands and manage them all so I'm
00:05:26
going to call that the command manager
00:05:29
now it can live on our player in this
00:05:31
case because I only have one character
00:05:32
in my game at the moment or you can have
00:05:34
this component be sitting on a game
00:05:36
manager for example and you might allow
00:05:39
your player to select units in your game
00:05:42
and then issue commands for each of them
00:05:44
I want to point out here that I'm going
00:05:45
to use a serialized mono behavior for
00:05:47
this class because for the demo I want
00:05:50
to be able to show all of these
00:05:51
interfaces in unity now the ability to
00:05:54
serialize interfaces is built into the
00:05:56
Odin inspector and this came up in a
00:05:58
comment on a video last week now I tend
00:06:01
to take it for granted because I've used
00:06:03
Odin in every single project for years
00:06:05
now Black Friday is coming up in a
00:06:07
couple weeks and this tool is always on
00:06:09
sale sometimes for 70% off so if you
00:06:12
don't have this yet I highly encourage
00:06:15
you to pick it up when you can now
00:06:16
Beyond just serializing interfaces it
00:06:19
has plenty of helpful attributes you can
00:06:21
set up awesome enums in the inspector
00:06:24
you've got the required attribute uh you
00:06:27
should pick up the validator too if
00:06:28
that's on sale because it fixes so many
00:06:30
problems in your project it's
00:06:33
unbelievable using Odin I'm going to
00:06:35
expose I entity so we can see it in the
00:06:37
inspector a single command so we can
00:06:40
just try running a command with one and
00:06:41
then we're going to make combos so for
00:06:43
that I'm going to use a list of I
00:06:45
command next I'm going to separate out
00:06:47
the execution of tasks in particular
00:06:49
because I want them to be awaitable so
00:06:51
I'm going to put that into a command
00:06:53
invoker class that will just take in a
00:06:55
list of I command iterate over them and
00:06:58
execute them all waiting for each one to
00:07:00
complete each time so with that out of
00:07:02
the way let's come back to the command
00:07:04
manager and get a reference to that we
00:07:06
can just create a new one here now at
00:07:08
the start I know that I'm going to put
00:07:10
the command manager on my hero so we can
00:07:12
just get the I entity component from
00:07:14
this game object next let's define our
00:07:17
single command and our list of commands
00:07:20
just by calling our hero command create
00:07:23
generic method and now just as I'm
00:07:25
writing this I'm realizing that I can go
00:07:27
back up to the definition of my list of
00:07:30
commands I don't need to declare this as
00:07:31
new because the start method is always
00:07:33
going to create some initial commands
00:07:35
for me here finally let's have a private
00:07:37
method here execute command that accepts
00:07:40
the list of commands and awaits the
00:07:42
command invoker to run them all right
00:07:46
last piece of the puzzle we need some
00:07:47
way to test this thing so in the update
00:07:49
method let's check to see if the user
00:07:51
has pressed the one key so when the user
00:07:55
presses one let's execute command and
00:07:57
we'll just create a new list here and
00:07:59
send in the single command and then if
00:08:01
the user presses the two key we'll
00:08:03
execute our list of commands as a combo
00:08:06
okay nothing left to do but go test it
00:08:09
out so if I select my hero in the
00:08:12
hierarchy here you can see I've already
00:08:13
got the animation manager and the hero
00:08:15
components on here I just need to add
00:08:17
the new component our Command manager so
00:08:20
let's get that on there now the first
00:08:22
thing you're going to notice is that all
00:08:24
of the interfaces are exposed here and
00:08:26
you can drag and drop things into this
00:08:27
field if you like but in this case
00:08:29
they're all already being created in the
00:08:31
start method by making calls to our hero
00:08:33
commands static create method so if we
00:08:36
just press play here you see all the
00:08:38
magic at work so it's populated our hero
00:08:41
into the entity field we've got our
00:08:43
single command which is an attack and
00:08:44
then we've got a list of commands that
00:08:46
we can order any way that we want so
00:08:48
let's press number one here you can see
00:08:50
I've got an attack so that's no problem
00:08:52
attacks goes right back into idle now
00:08:55
the combo there we go attacking spinning
00:08:58
and jumping just executes them all in
00:09:00
order and now we can just rearrange them
00:09:03
however we want so let's flip them
00:09:05
around here so that the jump is first
00:09:07
then spin then attack now I'm sure you
00:09:10
can picture where you go from here you
00:09:12
just build a little gooey element where
00:09:14
your player can rearrange their combo
00:09:17
any way they want and now you can just
00:09:20
send this list around to anything you
00:09:21
want you could pass this list of
00:09:24
commands into a state of your state
00:09:26
machine for example if you're going into
00:09:28
an attack state or if you have multiple
00:09:31
units and you're just trying to set up
00:09:33
actions for each one of their units
00:09:35
before you run your play say you're uh
00:09:37
in your big
00:09:38
RTS then you just cue up all these
00:09:41
commands one by one keep selecting a
00:09:43
different entity set up the commands
00:09:45
that you want to execute for them set up
00:09:48
combos if necessary all those things get
00:09:51
handled by your command manager and when
00:09:53
it's time to run them all just send them
00:09:55
all into the command invoker one by one
00:09:57
they're all going to fire off now I'm
00:09:59
just going to jump into code here
00:10:00
quickly because in this case I'm just
00:10:03
creating the commands and executing them
00:10:06
as soon as the buttons get pressed so I
00:10:08
don't really want them to continually
00:10:10
being sent into the invoker if
00:10:12
something's already running let's just
00:10:14
create a little guard Clause that'll
00:10:15
prevent that so we can have a Boolean is
00:10:17
command executing and so if it is we'll
00:10:21
turn the flag on if it's not we turn it
00:10:23
off and then in the update method if
00:10:25
there's a command already running let's
00:10:27
just bail out
00:10:30
okay that's as far as we're going to go
00:10:31
with the command pattern today there's
00:10:33
so many uses for command and to be
00:10:35
honest it's probably the third most
00:10:37
often used pattern in my daily work
00:10:40
after Factory and Builder remember the
00:10:43
easiest way to think about a command is
00:10:45
that it's a method or several methods
00:10:47
wrapped in an object and once it's
00:10:49
wrapped you can treat it like data store
00:10:52
it organize it and pass it around hit
00:10:55
that like button if that helped you out
00:10:56
and subscribe to the channel if you like
00:10:58
new video on software engineering
00:11:00
applied to games every week if you want
00:11:02
to see more content like this click on
00:11:04
one of the boxes on your screen and I'll
00:11:06
see you in another one

Description:

Unity C# Architecture: Learn the powerful Command programming pattern by example, building a Skill Combo system for our Hero! 🔔 Subscribe for more Unity Tutorials https://www.youtube.com/@git-amend ▬ Contents of this video ▬▬▬▬▬▬▬▬▬▬ 0:00 Command 5:30 Command Manager Check out: https://www.artstation.com/katgrabowska *Assets Shown In This Video* (Affiliate Links) Odin: https://assetstore.unity.com/publishers/3727?aid=1101lw3sv Animancer PRO: https://assetstore.unity.com/packages/tools/animation/animancer-pro-116514?aid=1101lw3sv Animancer Lite (FREE): https://assetstore.unity.com/packages/tools/animation/animancer-lite-116516?aid=1101lw3sv Dungeon Mason: https://assetstore.unity.com/publishers/23554?aid=1101lw3sv Chromisu: Handpainted Forest MEGA Pack https://assetstore.unity.com/packages/3d/vegetation/handpainted-forest-mega-pack-248421?aid=1101lw3sv SineVFX: Better Crystals https://assetstore.unity.com/packages/vfx/shaders/better-crystals-235002?aid=1101lw3sv *Follow me!* https://linktr.ee/gitamend

Preparing download options

popular icon
Popular
hd icon
HD video
audio icon
Only sound
total icon
All
* — If the video is playing in a new tab, go to it, then right-click on the video and select "Save video as..."
** — Link intended for online playback in specialized players

Questions about downloading video

mobile menu iconHow can I download "How to use the Command Pattern (Skill Combos Example)" video?mobile menu icon

  • http://unidownloader.com/ website is the best way to download a video or a separate audio track if you want to do without installing programs and extensions.

  • The UDL Helper extension is a convenient button that is seamlessly integrated into YouTube, Instagram and OK.ru sites for fast content download.

  • UDL Client program (for Windows) is the most powerful solution that supports more than 900 websites, social networks and video hosting sites, as well as any video quality that is available in the source.

  • UDL Lite is a really convenient way to access a website from your mobile device. With its help, you can easily download videos directly to your smartphone.

mobile menu iconWhich format of "How to use the Command Pattern (Skill Combos Example)" video should I choose?mobile menu icon

  • The best quality formats are FullHD (1080p), 2K (1440p), 4K (2160p) and 8K (4320p). The higher the resolution of your screen, the higher the video quality should be. However, there are other factors to consider: download speed, amount of free space, and device performance during playback.

mobile menu iconWhy does my computer freeze when loading a "How to use the Command Pattern (Skill Combos Example)" video?mobile menu icon

  • The browser/computer should not freeze completely! If this happens, please report it with a link to the video. Sometimes videos cannot be downloaded directly in a suitable format, so we have added the ability to convert the file to the desired format. In some cases, this process may actively use computer resources.

mobile menu iconHow can I download "How to use the Command Pattern (Skill Combos Example)" video to my phone?mobile menu icon

  • You can download a video to your smartphone using the website or the PWA application UDL Lite. It is also possible to send a download link via QR code using the UDL Helper extension.

mobile menu iconHow can I download an audio track (music) to MP3 "How to use the Command Pattern (Skill Combos Example)"?mobile menu icon

  • The most convenient way is to use the UDL Client program, which supports converting video to MP3 format. In some cases, MP3 can also be downloaded through the UDL Helper extension.

mobile menu iconHow can I save a frame from a video "How to use the Command Pattern (Skill Combos Example)"?mobile menu icon

  • This feature is available in the UDL Helper extension. Make sure that "Show the video snapshot button" is checked in the settings. A camera icon should appear in the lower right corner of the player to the left of the "Settings" icon. When you click on it, the current frame from the video will be saved to your computer in JPEG format.

mobile menu iconWhat's the price of all this stuff?mobile menu icon

  • It costs nothing. Our services are absolutely free for all users. There are no PRO subscriptions, no restrictions on the number or maximum length of downloaded videos.