UEFN Verse For Beginners Course - Control Flow - Lesson 3
Summary
TLDRIn diesem spannenden Video lernen Sie die Kontrollflussumkehr kennen und erfahren, wie Sie Dinge dazu bringen können, dies oder das zu tun oder nicht zu tun – die Bausteine zur Erstellung von Videospielen und natürlich der Programmierung. Das Video baut auf den vorherigen Lektionen auf und führt Sie durch die Erstellung einer neuen Vers-Datei in Unreal Editor. Sie lernen, wie Sie Kontrollfluss mit if-Anweisungen nutzen, Zufallszahlen generieren und ein einfaches, rundenbasiertes Angriffssystem in einem RPG-Spiel implementieren. Am Ende gibt es eine Übung, um das Gelernte anzuwenden.
Takeaways
- 🎮 Das Video behandelt die Steuerungsflussinversion in der Programmierung und beim Erstellen von Videospielen.
- 🛠️ Es baut auf früheren Lektionen auf, daher wird empfohlen, diese zuerst anzuschauen.
- 📂 Es wird erklärt, wie man eine neue Verse-Datei im Unreal Editor erstellt und eine alte löscht.
- ✏️ Die Bedeutung der Wiederholung beim Programmieren wird hervorgehoben, um die Syntax zu verinnerlichen.
- ⚔️ Es wird ein Beispiel für ein Rollenspiel gegeben, in dem Spieler- und Gegnerwerte definiert und Angriffe simuliert werden.
- 📝 Die Syntax und Logik von Kontrollflussanweisungen wie 'if' und 'else if' werden detailliert erläutert.
- 🎲 Zufällige Zahlen werden mit der Random-Bibliothek erzeugt, um das Würfeln in einem Rollenspiel zu simulieren.
- 🤖 Der Code zeigt, wie man zufällige Angriffe basierend auf Würfelergebnissen implementiert und die Spieler- und Gegnergesundheit anpasst.
- 📝 Die Bedeutung der Einrückung in Verse wird betont, um den Gültigkeitsbereich von Anweisungen klar zu definieren.
- 🔄 Es wird eine Übung vorgeschlagen, um ein rundenbasiertes Angriffssystem zu erstellen, das zufällige Würfelschäden verwendet und den Tod eines Spielers oder Gegners behandelt.
Q & A
Was ist das Hauptthema des Videos?
-Das Hauptthema des Videos ist die Steuerung des Ablaufs in der Programmierung, insbesondere im Zusammenhang mit dem Erstellen von Videospielen, und wie man Entscheidungen in einem RPG-Spielmodell implementiert.
Welche Programmierblöcke sind für die Steuerung des Ablaufs wichtig?
-Die wichtigsten Programmierblöcke für die Steuerung des Ablaufs sind 'if', 'else if' und 'else', die es ermöglichen, Entscheidungen in der Codeausführung zu treffen.
Welche Variablentypen wurden im Video verwendet?
-Im Video wurden Variablentypen 'int' für Ganzzahlen verwendet, um Spieler-Gesundheit und Feind-Angriffsschaden zu definieren.
Wie wird im Video gezeigt, wie man einen Spieler-Angriff durchführt?
-Im Video wird gezeigt, dass man den Spieler-Gesundheitswert um den Feind-Angriffsschaden verringert und überprüft, ob der Spieler stirbt, indem man auf einen Gesundheitswert kleiner oder gleich Null prüft.
Was ist der Zweck des 'get random int'-Aufrufs im Video?
-Der 'get random int'-Aufruf dient dazu, zufällige Zahlen zwischen zwei definierten Werten zu generieren, was für das Simulieren von Würfelwürfen in einem RPG-Spiel verwendet wird.
Wie wird im Video veranschaulicht, dass ein Spieler stirbt?
-Im Video wird gezeigt, dass ein Spieler stirbt, wenn seine Gesundheitswerte kleiner oder gleich Null sind und der Ausdruck 'player died' ausgegeben wird.
Was sind die Bedingungen, unter denen ein Feind den Spieler im Video angreifen kann?
-Ein Feind kann den Spieler angreifen, wenn das Ergebnis des Feind-Würfels größer ist als das des Spieler-Würfels, und wenn der Spieler mehr als 50 Gesundheit hat.
Wie wird im Video die Lesbarkeit von 'if'-Anweisungen verbessert?
-Im Video wird gezeigt, wie man lange 'if'-Anweisungen in Kurzform schreibt, entweder ohne Klammern und mit Bedingungen direkt hinter 'if', oder mit Klammern und ohne Doppelpunkt, um die Lesbarkeit zu verbessern.
Was ist der Vorteil des Imports des 'random'-Moduls im Video?
-Der Vorteil des Imports des 'random'-Moduls ist, dass man nur die benötigten Funktionen für zufällige Zahlen generieren kann, ohne unnötigen Code in das Projekt einzubinden, was die Leistung verbessert und das Projekt kleiner hält.
Was ist die Aufgabe, die am Ende des Videos als Übung zugewiesen wird?
-Die Übung besteht darin, ein einfaches Würfel-basiertes Angriffssystem für ein Spiel zu erstellen, bei dem Spieler und Feind sich gegenseitig angreifen und mit 'if', 'else' und dann zu bestimmen, wer angreifen wird und wann einer stirbt.
Outlines
📝 Einführung in Steuerungsfluss in Videospielen
Dieser Abschnitt des Skripts führt in die Steuerung von Fluss in Videospielen ein, insbesondere im Zusammenhang mit Programmierkonzepten. Der Sprecher betont die Wichtigkeit früher Lektionen und führt dann die Erstellung einer neuen Verse-Datei namens 'lesson zero three' in Unreal Editor vor. Es wird gezeigt, wie man einen bestehenden Code entfernt und den neuen Code kompiliert, um die Funktionsweise zu demonstrieren. Der Fokus liegt auf der Wiederholung von Code, um das Lernen zu festigen, und es werden Variablen wie Spieler-Gesundheit und Feindangriffsschaden definiert.
🛡 Steuerung des Spieler-Todes im Spiel
In diesem Paragraphen erläutert der Sprecher, wie man im Code überprüft, ob der Spieler stirbt, indem man den Spieler-Gesundheitswert mit dem Feindangriffsschaden vergleicht. Es wird ein 'if'-Statement verwendet, um zu bestimmen, ob der Spieler nach einem Angriff weniger oder gleich Null Gesundheit hat. Dabei wird die Bedeutung von Operatoren wie 'less than or equal to' erläutert, um sicherzustellen, dass der Spieler-Gesundheitswert korrekt aktualisiert wird. Der Sprecher betont die Wichtigkeit von Lesbarkeit und Einrückung im Code.
🎲 Einführung von Zufallszahlen in das Spiel
Der Sprecher führt die Verwendung von Zufallszahlen in das Spiel ein, um die Spieler- und Feindwürfelwerte zu generieren, ähnlich wie in einem Rollenspiel. Er importiert das 'random'-Modul und verwendet die 'get random int'-Funktion, um Würfelwürfel zwischen 1 und 20 zu erzeugen. Der Code wird dann so angepasst, dass die Spieler- und Feindwürfelwerte auf dem Bildschirm ausgegeben werden, um den Ablauf des Spiels zu visualisieren.
🤺 Implementierung eines vereinfachten Angriffssystems
In diesem Abschnitt wird ein vereinfachtes Angriffssystem implementiert, das anhand der Würfelwürfe des Spielers und des Feindes bestimmt, wer angreifen kann. Der Sprecher verwendet 'if', 'else if' und 'else'-Anweisungen, um zu entscheiden, ob der Feind den Spieler angreifen kann, basierend auf den Würfelwerten. Es wird eine Logik eingeführt, die den Feind nur dann angreifen lässt, wenn seine Würfelzahl höher ist als die des Spielers, und gibt in einem Unentschieden einen kleinen Schaden für den Spieler.
🔄 Verwendung alternativer Syntax für 'if'-Anweisungen
Der Sprecher zeigt verschiedene Syntaxformen für 'if'-Anweisungen, die man in Code, einschließlich Epic Games-Code, antreffen kann. Er demonstriert die Verwendung von 'if' ohne Klammern und mit einer einzigen Bedingung, gefolgt von 'then'. Außerdem wird gezeigt, wie 'if'-Anweisungen in einer Zeile mit geschweiften Klammern geschrieben werden können, was in anderen Programmiersprachen üblich ist. Schließlich wird ein Übungsauftrag gestellt, bei dem der Zuschauer ein einfaches, zufälliges, wahlbasiertes Angriffssystem für ein Spiel erstellen soll, einschließlich der Behandlung von Todesfällen.
Mindmap
Keywords
💡Kontrollfluss
💡Verse
💡Unreal Editor
💡Variablen
💡Bedingungen
💡Schleifen
💡Module
💡Funktionen
💡Indentation
💡Dungeons and Dragons
Highlights
Introduction to control flow and making decisions in programming.
Importance of watching previous lessons to build foundational knowledge.
Creating a new verse file in Unreal Editor.
Introduction to control flow using conditionals in programming.
Demonstrating a simple RPG game scenario with player health and enemy attack.
Explaining the significance of repetition in learning programming.
Setting up and using variables in Verse for player health and enemy attack damage.
Implementing an attack and checking if the player has died using conditionals.
Introduction to using the 'if' keyword and the importance of indentation in Verse.
Explaining the use of comparison operators like less than or equal to.
Printing messages to the console based on conditional checks.
Demonstrating the use of random number generation with dice rolls for a game.
Importing and using modules in Verse for generating random numbers.
Creating a simple turn-based attack system using random dice damage.
Handling different cases for dice roll outcomes to determine attack success or failure.
Using 'else if' statements to handle additional conditions.
Printing results of dice rolls and game states to the console.
Explanation of functions and methods in programming.
Introduction to loops and how they can be used to repeat code execution.
Alternative syntax for if statements using colons and curly braces.
Exercise: Create a simple turn-based attack system with random dice damage.
Transcripts
in this exciting video You're Gonna
Learn control flow inverse how to make
things think do this or that or not the
building blocks of making video games
and of course of programming this lesson
builds upon lesson two and that built
upon lesson one and so on and so forth
so watch those if you have not but let's
go ahead and get started with this open
up your unreal editor and in our project
or any project if you're starting from
scratch even though I told you to watch
the other videos uh what we're going to
do here is create a new verse file so we
can go to our reverse Explorer verse
verse Explorer and here in the content
folder that I have put yours wherever
you want I'm going to right click create
new verse file
and this one is going to be called
lesson zero three and create
and while I'm here I'm also going to
delete this lesson 2 object over here in
the outliner I'm going to delete that
because we don't want that code to run
anymore
now what I'm going to do is Click verse
buildverse code
and if I go to my content browser I
should now see lesson three verse class
in my creative devices folder and I'm
going to drag that on so our new lesson
compiles okay and because this takes a
long time I'm just going to click push
changes and then we'll just continue
going forward so push changes and Save
back to our code
so previously on 24. uh previously on
our learning path we
worked with
these uh operations
and now I'm going to close out of that
and we're going to go to lesson three
which we're going to talk about control
flow uh making things think and do stuff
we're still going to work with
our RPG type of game okay and so we're
going to say VAR player health
of type int equals 100 and VAR enemy
attack damage of type int equals 51
those are some arbitrary values there in
those variables
and I'm rewriting this every single time
if you've been watching the other videos
for repetition it is so critical to have
repetition and write the code over and
over and over otherwise you don't
memorize it I've been doing this for 15
years and teaching people for like 10
years trust me on this I promise I
promise I promise okay
let's get rid of this boilerplate
nonsense we're not baby coders anymore
we're like toddler coders taking our
first steps now so
okay what do we want to do
you know we want to attack a player we
want the player to die but what happens
if the player dies what how can we check
for that like how do we and how do we
check whose turn it is to attack and
things like that uh so let's have some
fun all right so let's do an enemy
attack right so we're gonna say set
player health I'm going to close this
left hand side here say set player
Health minus equals enemy attack damage
you've seen this before we're gonna take
the Current player's health and subtract
the enemy damage from it and set it back
into the variable okay we've done that
before
now what if the player died
like lost all their life like how do we
check for that
well
we can say if
player health is less than or equal to
zero
I'm gonna put a colon here at the end
and by the way I press Escape whenever
these dumb pop-ups come up here Escape
and then I'm gonna say print
player died
sad face
like so
okay
new stuff new syntax oh no okay
we work with conditionals by specifying
the
if keyword the control flow where is our
program going to flow if and then we use
parentheses
and we're going to say player health
is less than or equal to okay so this is
the less than or equals to operator uh
the reason why we don't typically uh
check for just health
for the amount exactly is because what
happens if the enemy attacks you you
know for like 50 more than your life
actually has well then this would go
false so we want to say less than or
equal to if it if it's equal to zero or
less than we don't want to just say less
than because then if it actually brought
you down to zero uh it wouldn't fire
either okay so less than or equal to in
parentheses at the end of those
parentheses we put a colon okay a colon
here that says hey the next area down is
a block of code notice how it's indented
okay the indentation is important in
verse okay it's yelling at us right it
doesn't like that indentation is
important I'm pressing tab all right
player died so if the player health is
less than or equal to zero player died
so how much is the player's health So
currently it started off at 100 uh an
enemy attack damage is 51 so that
dropped it down to what like 49. Okay so
let's do another attack how about that
set player Health minus equal enemy
attack damage uh you know maybe what
we'll do is uh you know if they get
attacked again we're gonna say uh
uh we'll say print player flinched
enemy
attacks again okay
so then we're going to do it again
and then what we can do is we can grab
this code copy it
and paste it down here
so we attack once
okay uh in fact just to be to make this
more like a game print enemy attacks
player okay enemy attacks player the
attack is made jaw check the player's
health
check the player's Health let's see what
happens okay
um let's go ahead and go back to our
unreal editor
verse buildverse code
push verse changes
escape and start game alright tab to
look at our log here if you're not in
the log you can just click the log all
right enemy attacks player player
flinched enemy attacks again
player died okay so our code worked it
worked as expected so let's review it
again
you have to memorize some of this stuff
you may it may sound confusing but it
just takes lots of time and repetition
memorize this syntax okay if if then if
then just think that if then right like
that's just how it works you know if I
tell my wife how she should fix her
problems then I'm going to get in very
big trouble else if
I listen and Nod and show appreciation
even though I totally don't understand
anything that's going on in her head
then I get a hug and life is great
uh okay so
that's how it works control flow it's
just like life every everything in life
has you know we make decisions that's
what we're doing we're making decisions
here okay
so
let's make this a little bit more
interesting right because we know you
know how much damage the enemy has and
we're kind of forcing the player to die
let's actually make this like a real
game what if we made it like Dungeons
and Dragons where we roll the dice and
then uh we get random numbers how cool
would that be let's do that so what I'm
going to do is up here at the top we're
going to import a new module we're gonna
say using
verse dot org slash random we're going
to use some code for generating random
numbers that verse was kind enough to
make for us uh and the reason why we
import saying using is so we don't
include a bunch of code into our project
that we're not using and make you know
our game smaller make things run faster
it's we're just grabbing only the things
that we need we're being polite little
citizens here in the world of
programming
so let's go ahead and have some fun now
if you can see random tabs like this
where my cursor is in the wrong spot
make sure you always go back to the
right spot okay don't start in the wrong
spot start where you're supposed to go
because
okay if I type in here all right we are
still inside of our if statement okay
the scope here belongs to our if
statement however if I go like this
all right uh guess what we are now
outside of the if statement and this
code will run no matter what indentation
is very important in verse
all right so go to the right spot and
I'm going to say VAR player dice result
of type int equals get random int and
we're going to do parentheses between 1
and 20. like a 20-sided die now get
random int is a function if I control
click it it's going to take me to verse
dot digest and it's going to tell me
what it does returns a random int
between low and high inclusive inclusive
meaning it's going to include uh those
numbers okay
so the one and the 20. all right if it
was exclusive uh it would mean that it's
going to only get the numbers 2 through
19. so a player dice result okay let's
do another one lar enemy dice result
colon int equals get random int
between 1 and 20. so imagine that the
player rolls their dice the enemy rolls
their Dice and we're gonna see who goes
head to head and who attacks who and
does what and all kinds of things and
because our player is dead right now
let's let's resurrect our player and
let's say set player health
equals 100
and what do we want to do well
first off let's just print the values of
our dice so let's well first let's say
print uh player and enemy World dice
okay that's what happened uh that way
when we're playing the game we can
actually see the order of things that
are happening and now we're going to
print the player dice
type in player dice result I'm pressing
enter when it does the autocomplete
and we're going to print
the enemy dice
we're going to say enemy dice result
like so
and
let's print those on the screen and see
what happens so we've used this random
get random in it's a function or rather
it's a let's go into here and look at it
again so this is a module our random
module that we included using the using
keyword so we included this module which
means it's going to make these available
and these are functions now you might
hear me say the word method and the word
function and those are kind of the same
thing really but in this case they are
functions because they're publicly
available
methods belong to part of a class you'll
learn about that later but they're the
same thing so we're calling a function
okay that's pre-built for us and we use
the parentheses to actually call the
function and then the function is asking
us for two numbers a low number and a
high number it's asking for integers
here okay
uh between 1 and 20. that's all that's
happened save
editor verse buildverse code push versus
changes
it's up to date start game
okay
so player and enemy World dice player
dice 13 enemy dice 14. excellent it
worked it gave us a random number pretty
cool now
every time I run the game it's going to
give us a new number because on begin is
called whenever we start the fortnite
game so that number will change every
single time we run the game which is
pretty awesome
now let's uh let's pretend that the uh
enemy is attempting to attack the player
we're gonna do a super simplified
Dungeons and Dragons but basically you
know if if an enemy goes to attack a
player
you know the player has to roll the
device and they've got you know the
player defense and enemy attack and if
one dice is higher than the other it
determines if you know the attack worked
or not more or less so that's what we're
gonna do okay we're gonna code some
Dungeons and Dragons uh all right I'm
gonna say if enemy dice result is
greater than player dice result okay so
if the enemy's dice is a bigger number
than the player's number what do we want
to do
well we're going to make an attack okay
only if it's bigger I didn't say greater
than or equal to if it's a tie I'm still
not going to make an attack I'm going to
give the benefit to the player okay so
that's just my logic choice so say set
player health
to minus equal
enemy attack damage so only attack the
player
if the enemy rolled a higher die
die die a higher number
now what what else we want to do we can
we can take this further okay how about
we say else if ooh a new one okay else
if else if
enemy dice result equals player dice
result what do we want to do so now
actually we're handling I remember now
we're handling the the case where it's
it's the same attack right the same same
number in this case here we're just
going to take one life away from the
player that's what I meant to say
earlier minus equal one so a minor
penalty for getting a tie okay
else what else colon what print the
enemy mist
okay so that's pretty cool
um we're gonna take away the player Dam
or player Health in fact let's print
this to see if these even work so we're
going to say print uh attack enemy
attack
successful
player HP is now we'll just do all the
one line to keep it fast player health
like so put our quotes in here and then
down here what we'll do is we'll say
enemy
attack
uh we'll just say
roll was a tie
player lost one HP I'm gonna print that
there otherwise the enemy missed okay
let's go ahead and compile our code
verse buildverse code
push verse changes and load the game to
see this in action well Let's Escape and
start game
and press tab so it looks like the enemy
Miss player dice was 19 enemy dice was
13 the enemy missed excellent press
Escape okay excellent so our code is
doing some things uh and it's working
just fine and I'm not going to print
through all the states but you can
relaunch your game and test that these
work if you want to be extra creative
you know you could you could create a
loop if you're really adventurous you
could create a loop
for these right here and loop through
actually starting probably for all of
these put all of those inside of a loop
um and then you could print it over and
over and over again we're not covering
Loops just yet to not confuse you but
just know if you wanted to have this
test like a hundred times put it in a
loop for 100 times and it'll give you
different results every single time
which is pretty cool so we just did some
basic logic here which is awesome now I
do want to show you uh one more thing
that we can do with our if else
statements here so I put parentheses in
here there's another format a style to
do this and the reason why I want to
show this to you is because you're going
to see it in other people's code you're
going to see it in Epic Games code all
right so we're going to say if colon
notice not parentheses this time colon
okay
we're going to say enemy dice result is
greater than player dice result
all right
player health
is greater than 50.
bear with me for a moment here
and then what we're going to do is we're
going to say then colon
enter
set layer health
minus
equals enemy attack damage
else
colon
print the enemy Mist okay
what's happening here so instead of
doing parentheses what we do is we say
if colon
and the conditions that we're testing
are actually inside of here okay we are
testing various conditions we could put
as many as we wanted here test them all
sometimes that's nice because if you're
inside of an if statement sometimes they
get really long you know you say uh you
know enemy Dives result is greater than
player dies result and if you know
player dice result
result is you know greater less than or
equal to 10 and if enemy dice result you
know uh equals five and see how this is
getting really really really long
well if that was our logic instead of
just using all of the you know using all
of that crazy stuff you know you can
just kind of move those uh
into here
just to make it more readable
okay so instead of
yeah you just put all your conditions in
that one spot all right I'm gonna get
rid of those two though
then do something so in our case here if
enemy dice result is greater than player
dice result and if player health is
greater than 50. so in this case here
we're saying
only let enemy attack player if player
has more than 50 Health maybe it's a
maybe it's a cut scene area in your game
and you only want them to get hit up to
a certain amount of damage and then like
you know the hero is going to come in on
a spaceship and you know save the
Paladin who's attacking ogres with bows
and arrows uh anyway that's our logic
then okay so if then all right uh that's
what's happening here so here's our then
up here if then in our case here it's if
and then now I like this format a lot
because some of my ifs get pretty long
as I'm working with uefn and so this
becomes very nice in those cases else
print the enemy Mist okay so that's a
different way of working with if
statements you can choose whatever one
you like and there's nothing wrong with
doing this at all do whatever works for
you and keep it simple don't stress out
about things one more thing I want to
show you just for fun because you're
going to see this a lot as well too is
I'll copy this over there
and I'm going to again always make sure
you go to the right indentation I'm
going to paste it here so another if
statement what you'll see sometimes is
if statements on the same line okay so
you'll take off the colon like so
and we'll do curly braces almost kind of
like other programming languages all
right and you'll see this on the same
line often and I do these as well too
and these are things that you especially
use when you start learning about
failure and failure contacts you know we
can print uh enemy is attacking you'll
see this as well too enemy is attack
uh attacking all your base are belong to
us okay so if enemy dice result is
greater than players result notice we
don't have the colon here on this one
and we use curly braces instead that is
another type of if statement that you
will see in code all right pretty cool
now
that is it for now I'm going to give you
some work here is your exercise create a
simple turned based attack system where
the player attacks the enemy and then
the enemy attacks the player using
random dice damage like above use if
else and then to determine who is being
attacked and then print that be sure to
handle when one or the other dies so
turn this into a real game with a print
statements so enemy has life and you
know attack damage and each player okay
they randomly attack each other back and
forth back and forth using the dice
until one of them dies how cool would
that be and then you print that up here
and if you want to get really crazy and
be adventurous okay let's say extra
credit
use a loop so you don't have to copy and
paste code if you're not feeling
adventurous don't worry we will get
there so that's it for now hope you're
learning some great things here make
some video games yourself and there's
more to come if you didn't know my name
is Mark walbeck and you can watch the
next video in this series right here and
don't forget to subscribe and click the
Bell so you can know when the next
videos are released
Посмотреть больше похожих видео
Crashkurs für Anfänger | Canva Tutorial Deutsch
Wie 365 Tage Ohne Social Media Mich Verändert Haben
Qu’est-ce qu’un conte merveilleux ?
Wie du dir alles merkst, was du liest (5 Schritte für bessere Merkfähigkeit)
How to Get PPC Clients | 3 Ways To Get Google Ads Clients
Was ist eine SPS? - SPS Programmierung einfach erklärt - Anfänger TIA Portal Tutorial
5.0 / 5 (0 votes)