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

Download "Match Statements Pt 1 | Godot GDScript Tutorial | Ep 09"

input logo icon
Video tags
|

Video tags

Match
Statement
Godot
GDScript
Tutorial
Learn
Programming
Pattern
Value
Fundamental
Series
Beginner
Subtitles
|

Subtitles

subtitles menu arrow
  • ruRussian
Download
00:00:01
welcome to another episode in the GD
00:00:04
script fundamental tutorial series in
00:00:06
this episode we will be talking about
00:00:07
the match statement so a match statement
00:00:10
is a type of selection control mechanism
00:00:13
used to allow the value of a variable or
00:00:16
expression to change the control flow of
00:00:18
a program execution via search in map
00:00:21
basically you use a variable to match a
00:00:23
pattern and execute the code inside the
00:00:26
match block statement one thing to keep
00:00:28
in mind is that patterns are matched in
00:00:30
chronological order if a pattern matches
00:00:33
the match block will be executed after
00:00:35
the block of code has run the match
00:00:37
statement will exit the chain so you'll
00:00:40
find yourself using two things quite a
00:00:42
lot when using match statement one is
00:00:44
the underscore you can use the
00:00:46
underscore symbol at the bottom of a
00:00:48
match statement to denote a default
00:00:50
block statement to run the second thing
00:00:52
you may find yourself using is the
00:00:53
continue keyword what that basically
00:00:56
does is if you would like to continue
00:00:57
looking through match patterns you use
00:01:00
the continue keyword let's go ahead and
00:01:02
take a look at an example of a match
00:01:04
statement without the continue keyword
00:01:05
as you see here you have the keyword
00:01:07
match followed by a value that value can
00:01:11
be anything a variable dictionary array
00:01:14
it could even be a hard literal but no
00:01:17
matter the value what you're gonna have
00:01:18
our patterns underneath it in this case
00:01:21
we have one and an underscore what that
00:01:24
basically means is that our value has to
00:01:26
match one of these patterns in order to
00:01:28
execute the code inside as we've
00:01:30
discussed previously the underscore is
00:01:33
considered like a default block so here
00:01:35
you can see the match keyword is
00:01:37
followed by a value after the value
00:01:39
you'll have yourself patterns that
00:01:41
you'll use to see if the value matches
00:01:43
any of the patterns so let's go ahead
00:01:45
and take a look at a basic match
00:01:47
statement flowchart as you can see when
00:01:49
we've started the match statement we
00:01:52
take the value and we look at the
00:01:54
patterns in this case we look at case 1
00:01:56
we check the pattern if the pattern
00:01:58
matches we enter the block statement and
00:02:00
we exit the code however if the pattern
00:02:03
doesn't match we move on to the next
00:02:05
pattern we check if they match and if
00:02:07
they match we enter the block statement
00:02:09
and exit the code however if that's
00:02:10
false you'll either exit the code or if
00:02:13
you have an
00:02:14
score which is considered to be the
00:02:16
default block you'll enter the default
00:02:18
block you'll enter the block statement
00:02:20
and then you'll exit the code let's go
00:02:22
ahead and take a look at an example with
00:02:24
the continue keyword very simple all you
00:02:26
have to do is just type continue at the
00:02:29
bottom of your match statement block
00:02:31
what this basically means is that even
00:02:32
if your value matches a pattern continue
00:02:35
and check other patterns after it what
00:02:38
this looks like is basically the same
00:02:40
thing as the first match statement
00:02:42
however we're using the continue keyword
00:02:44
we take a value we check if our value
00:02:47
matches a pattern and if it matches we
00:02:49
entered the block statement execute all
00:02:52
the code and then we move on to the next
00:02:54
pattern we do the same thing we check if
00:02:56
that pattern matches our value and if it
00:02:58
does we enter the statement block
00:03:00
execute the code and we move on to the
00:03:02
next now in this case we have a default
00:03:04
if there was no default you would just
00:03:06
exit your match statement chain however
00:03:08
if you do have the default which is
00:03:10
again the underscore you'll enter that
00:03:12
because it's a wildcard you'll enter
00:03:13
that statement and you'll exit very
00:03:15
simple stuff once you get the hang of it
00:03:17
of course so there are seven different
00:03:19
types of match patterns you may find
00:03:22
yourself using in GT script you have the
00:03:25
constant pattern the variable pattern
00:03:27
the wildcard pattern the binding pattern
00:03:29
the array pattern the dictionary pattern
00:03:32
and the multiple pattern let's go ahead
00:03:34
and take a look at the constant pattern
00:03:36
a constant pattern is very simple it's
00:03:38
basically just primitive data types you
00:03:41
know your boolean z' your strings
00:03:42
integers etc let's go ahead and take a
00:03:45
look at an example of a constant pattern
00:03:47
so you can see here we're just using
00:03:48
literal values in this case an integer
00:03:51
value and a string value we're basically
00:03:53
saying if X is either one or the string
00:03:56
hello keep in mind that strings have to
00:03:58
be case sensitive very simple very
00:04:01
straight for it let's move on next you
00:04:02
have the variable pattern just as the
00:04:04
name implies you're using a variable or
00:04:06
enum to represent your match pattern if
00:04:09
you look at the print statement that's
00:04:11
basically how we're trying to match our
00:04:13
pattern if X is equivalent to Health and
00:04:16
for the name if X is equivalent to name
00:04:18
again very straightforward let's move on
00:04:20
now we've talked briefly about the
00:04:23
underscore symbol being the default but
00:04:25
in reality the underscore symbol
00:04:27
is a wildcard pattern basically a
00:04:29
wildcard pattern is used to match
00:04:32
everything again sometimes this pattern
00:04:35
is used as a default block meaning it
00:04:38
runs when all other tests fail however
00:04:40
you don't have to use it as a default
00:04:42
block since match statements run in
00:04:44
chronological order it's best to use the
00:04:46
wildcard pattern as your last pattern in
00:04:48
the match statement chain let's take a
00:04:50
look at an example of the wildcard
00:04:52
pattern it's very simple it's just an
00:04:54
underscore and underscore will match
00:04:56
anything in everything moving on we have
00:04:59
the binding pattern the binding pattern
00:05:01
catches everything just like the wild
00:05:03
card statement however the binding
00:05:06
pattern assigns the match value into a
00:05:09
variable let's go ahead and take a look
00:05:11
at that as you can see here we have
00:05:14
declared a variable in our match pattern
00:05:17
var and a variable name very important
00:05:20
you have the keyword var followed by a
00:05:23
name for your variable basically what
00:05:25
this means is that whatever is in your
00:05:26
match value that value will get passed
00:05:29
on to the variable that you just named
00:05:31
in your pattern and then you can use
00:05:33
that name in your block statement so as
00:05:36
you can see here we have the print
00:05:37
statement print value of x and we're
00:05:42
using our newly created variable keep in
00:05:44
mind that these variables are in local
00:05:47
scoped which means you can only use the
00:05:49
variable name in the block statement
00:05:51
that belongs to the pattern okay moving
00:05:53
on to an array pattern an array pattern
00:05:56
is basically that every single element
00:05:58
in the array is a pattern in itself and
00:06:01
arrays can be nested in your patterns a
00:06:04
very important thing to keep in mind is
00:06:06
that you can use sub patterns inside
00:06:09
your array so when you are testing your
00:06:11
value with an array pattern it is tested
00:06:14
in the following order first the length
00:06:17
of the array is tested if that match
00:06:19
fails or rather if the length of the
00:06:22
value and the pattern are different then
00:06:25
the match fails however if it passes
00:06:27
then the next thing it tests for is if
00:06:30
the value and the pattern elements
00:06:32
inside the array are the same if the
00:06:35
array of the value is different than the
00:06:37
pattern array then your test will fail
00:06:41
however if they are the same then it
00:06:42
passes and it executes everything inside
00:06:45
the block statement belonging to that or
00:06:48
a pattern and let's go ahead and take a
00:06:49
look in this case we have a value X and
00:06:53
we're matching three different things so
00:06:55
the first thing we're testing is an
00:06:56
empty array if your X has a value of an
00:06:59
empty array then it's gonna go ahead and
00:07:01
match our array pattern the first array
00:07:04
pattern and it's gonna prints out empty
00:07:05
array however if that fails X is gonna
00:07:08
move on and test itself in the second
00:07:10
pattern X needs to have exactly a one
00:07:13
two three as the first three elements of
00:07:15
its array followed by anything because
00:07:17
the pattern and the second pattern array
00:07:20
is a wild-card so that means X is fourth
00:07:23
element can be any value and lastly the
00:07:26
fifth element of X can be any value
00:07:29
however it is going to be passed into a
00:07:32
binding pattern in this case we called
00:07:34
it last element you can use last element
00:07:37
inside the block statement belonging to
00:07:39
the second array pattern so yes you can
00:07:42
use different types of patterns inside
00:07:45
your array a pattern now the last
00:07:46
pattern in this example as you notice at
00:07:49
the very end or the third element we're
00:07:53
using two periods what this means is
00:07:56
that our array is open-ended basically
00:07:58
if X has an array with the first two
00:08:01
elements being the value of one and two
00:08:03
it can be anything and any size after
00:08:06
that and it will match the third element
00:08:09
again this is called an open-ended array
00:08:12
array patterns are very powerful you can
00:08:14
do quite a lot with it so moving on to
00:08:17
the last example we have the dictionary
00:08:20
pattern the dictionary pattern works
00:08:22
just like the array pattern except
00:08:24
instead of arrays you'll be using
00:08:26
dictionaries keep in mind that all keys
00:08:29
have to be constants when using them in
00:08:32
your patterns however the values you use
00:08:34
in your key value pair for your pattern
00:08:38
dictionaries can be anything and by
00:08:40
anything I mean sub patterns so sub
00:08:43
patterns can be used on the dictionary
00:08:45
values of the dictionary pattern okay
00:08:48
moving on
00:08:49
so basically dictionary patterns follow
00:08:51
the same test format that array patterns
00:08:54
use so let's go ahead and take a look at
00:08:56
that so first the length of the
00:08:58
dictionary value is tested against the
00:09:00
dictionary pattern if the match fails
00:09:03
then we move on to the next pattern
00:09:05
unless of course you're using the double
00:09:07
period sub pattern which makes your
00:09:09
pattern and open-ended dictionary now if
00:09:12
the test pass what the value and what
00:09:15
the pattern look for is if they hold the
00:09:18
same key and value pair if the key and
00:09:21
value pairs are different then your test
00:09:24
will fail however if they match then
00:09:27
your test will pass let's go ahead and
00:09:29
take a look at that so our first example
00:09:30
is an empty dictionary if X is an empty
00:09:33
dictionary we meet or rather we match
00:09:36
the first pattern and we go ahead and
00:09:38
print out empty dictionary now the
00:09:40
second one behaves just like our array
00:09:42
example however keep in mind that our
00:09:45
keys and values have to be exactly the
00:09:48
same what this means is X has to have
00:09:50
key and value and key too and any value
00:09:54
in order to pass for the second pattern
00:09:57
now the last example is an open-ended
00:09:59
dictionary just like the array you use
00:10:01
two periods at the end and basically
00:10:05
your value can be any dictionary of
00:10:08
length except its first key value pair
00:10:11
has to be key followed by value the last
00:10:13
pattern is the multiple pattern you can
00:10:16
specify multiple patterns as long as
00:10:19
they are separated by commas multiple
00:10:21
patterns cannot have bindings to them
00:10:23
basically you can have a range of values
00:10:26
that the value can match against doesn't
00:10:29
have to match against just a single
00:10:31
value it can match against multiple
00:10:33
values let's go ahead and take a look at
00:10:34
an example as you can see here we have a
00:10:37
pattern 1 2 3 4 5 everything separated
00:10:40
by commas keep that in mind and
00:10:42
basically what this means is that as
00:10:43
long as X has a value of 1 2 3 4 or 5 we
00:10:49
will match the pattern and we will
00:10:50
execute everything in the block
00:10:52
statement very simple very
00:10:54
straightforward easy to use well that's
00:10:57
it for this episode I hope to see you in
00:11:00
the next

Description:

In this episode, I look into the basics of creating match statements in Godot GDScript. ✅ Github Project File: https://github.com/Godot-Tutorials/Match-Statement Godot Tutorials ---------- 🌸 Website: https://godottutorials.com/ 🐦 Twitter: https://twitter.com/godottutorials Resources ---------- 🎮 Godot Game Engine Tutorial Series: https://www.youtube.com/playlist?list=PLJ690cxlZTgIsmdEhFufnB7O6KWoMS8M6 💻 GDScript Fundamental Tutorial Series: https://www.youtube.com/playlist?list=PLJ690cxlZTgL4i3sjTPRQTyrJ5TTkYJ2_ 🐥 Trello Page with other Godot Channels: https://trello.com/b/TAHIZJ9D/godot-game-tutorials In this episode, I cover the following topics: - Match Statement Definition - Match Statement Flow Charts - 7 Pattern Types This is a reupload of a previously uploaded episode that had a mistake with the dictionary pattern. Special thanks to Ron Tarrant for pointing that out. This is part of a beginner's programming series called the GDScript Fundamentals Tutorial Series.

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 "Match Statements Pt 1 | Godot GDScript Tutorial | Ep 09" 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 "Match Statements Pt 1 | Godot GDScript Tutorial | Ep 09" 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 "Match Statements Pt 1 | Godot GDScript Tutorial | Ep 09" 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 "Match Statements Pt 1 | Godot GDScript Tutorial | Ep 09" 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 "Match Statements Pt 1 | Godot GDScript Tutorial | Ep 09"?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 "Match Statements Pt 1 | Godot GDScript Tutorial | Ep 09"?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.