UEFN Verse For Beginners Course - Control Flow - Lesson 3

Warforge
8 Apr 202322:05

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

00:00

📝 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.

05:01

🛡 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.

10:03

🎲 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.

15:05

🤺 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.

20:07

🔄 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

Kontrollfluss bezieht sich auf die Reihenfolge, in der Anweisungen in einem Programm ausgeführt werden. In dem Video wird erklärt, wie man in Verse den Kontrollfluss steuert, indem man Bedingungen und Schleifen verwendet. Zum Beispiel wird die Bedingung 'if' verwendet, um zu prüfen, ob die Spieler-Gesundheit kleiner oder gleich null ist.

💡Verse

Verse ist eine Programmiersprache, die in Unreal Engine verwendet wird. Das Video zeigt, wie man eine neue Versedatei erstellt und wie man grundlegende Programmierkonzepte wie Variablen und Kontrollstrukturen in Verse verwendet. Die Datei 'lesson03.verse' wird im Video erstellt und bearbeitet.

💡Unreal Editor

Der Unreal Editor ist eine Entwicklungsumgebung zur Erstellung von Spielen und interaktiven 3D-Inhalten. Das Video beginnt damit, dass der Zuschauer den Unreal Editor öffnet, um die Versedatei zu erstellen und zu bearbeiten. Es wird auch gezeigt, wie man den Editor verwendet, um den erstellten Code zu kompilieren und zu testen.

💡Variablen

Variablen sind Speicherplätze, die Werte speichern, die im Programm verwendet und verändert werden können. Im Video werden Variablen wie 'player_health' und 'enemy_attack_damage' definiert und verwendet, um die Gesundheit des Spielers und den Schaden des Feindes zu speichern und zu manipulieren.

💡Bedingungen

Bedingungen sind logische Aussagen, die entscheiden, ob ein bestimmter Block von Code ausgeführt wird oder nicht. Das Video erklärt, wie man Bedingungen mit 'if', 'else if' und 'else' in Verse verwendet, um verschiedene Spielzustände zu überprüfen, wie zum Beispiel, ob der Spieler gestorben ist.

💡Schleifen

Schleifen sind Konstrukte, die es ermöglichen, Code mehrfach auszuführen, bis eine bestimmte Bedingung erfüllt ist. Im Video wird vorgeschlagen, Schleifen zu verwenden, um den Kampf zwischen Spieler und Feind zu simulieren, indem man den Code wiederholt, bis einer von beiden stirbt. Es wird erwähnt, dass Schleifen später im Detail behandelt werden.

💡Module

Module sind Sammlungen von Funktionen und Variablen, die in einem Programm wiederverwendet werden können. Im Video wird das Modul 'verse.org/random' verwendet, um Zufallszahlen zu generieren. Der Befehl 'using verse.org/random' importiert dieses Modul, sodass die Funktionen darin im Programm verwendet werden können.

💡Funktionen

Funktionen sind benannte Codeblöcke, die eine bestimmte Aufgabe ausführen und bei Bedarf aufgerufen werden können. Das Video zeigt die Verwendung der Funktion 'get_random_int', die eine Zufallszahl zwischen zwei gegebenen Werten zurückgibt. Funktionen helfen dabei, den Code modular und wiederverwendbar zu machen.

💡Indentation

Indentation bezieht sich auf das Einrücken von Codezeilen, um die Struktur und Hierarchie des Codes zu verdeutlichen. In Verse ist die richtige Indentation besonders wichtig, da sie die Blöcke von Code definiert, die zu Bedingungen oder Schleifen gehören. Falsche Indentation kann zu Fehlern führen.

💡Dungeons and Dragons

Dungeons and Dragons ist ein bekanntes Rollenspiel, bei dem Spieler durch das Werfen von Würfeln über den Ausgang von Ereignissen entscheiden. Im Video wird ein einfaches Kampfsystem nach dem Vorbild von Dungeons and Dragons erstellt, bei dem Zufallszahlen verwendet werden, um den Schaden im Kampf zwischen Spieler und Feind zu bestimmen.

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

play00:00

in this exciting video You're Gonna

play00:02

Learn control flow inverse how to make

play00:05

things think do this or that or not the

play00:09

building blocks of making video games

play00:11

and of course of programming this lesson

play00:13

builds upon lesson two and that built

play00:16

upon lesson one and so on and so forth

play00:18

so watch those if you have not but let's

play00:20

go ahead and get started with this open

play00:23

up your unreal editor and in our project

play00:25

or any project if you're starting from

play00:27

scratch even though I told you to watch

play00:28

the other videos uh what we're going to

play00:30

do here is create a new verse file so we

play00:34

can go to our reverse Explorer verse

play00:36

verse Explorer and here in the content

play00:40

folder that I have put yours wherever

play00:41

you want I'm going to right click create

play00:43

new verse file

play00:45

and this one is going to be called

play00:47

lesson zero three and create

play00:53

and while I'm here I'm also going to

play00:55

delete this lesson 2 object over here in

play00:57

the outliner I'm going to delete that

play00:59

because we don't want that code to run

play01:01

anymore

play01:02

now what I'm going to do is Click verse

play01:04

buildverse code

play01:06

and if I go to my content browser I

play01:08

should now see lesson three verse class

play01:11

in my creative devices folder and I'm

play01:13

going to drag that on so our new lesson

play01:15

compiles okay and because this takes a

play01:19

long time I'm just going to click push

play01:21

changes and then we'll just continue

play01:22

going forward so push changes and Save

play01:26

back to our code

play01:28

so previously on 24. uh previously on

play01:32

our learning path we

play01:34

worked with

play01:36

these uh operations

play01:39

and now I'm going to close out of that

play01:42

and we're going to go to lesson three

play01:43

which we're going to talk about control

play01:45

flow uh making things think and do stuff

play01:48

we're still going to work with

play01:51

our RPG type of game okay and so we're

play01:54

going to say VAR player health

play01:58

of type int equals 100 and VAR enemy

play02:02

attack damage of type int equals 51

play02:06

those are some arbitrary values there in

play02:09

those variables

play02:10

and I'm rewriting this every single time

play02:12

if you've been watching the other videos

play02:13

for repetition it is so critical to have

play02:16

repetition and write the code over and

play02:19

over and over otherwise you don't

play02:21

memorize it I've been doing this for 15

play02:22

years and teaching people for like 10

play02:24

years trust me on this I promise I

play02:26

promise I promise okay

play02:29

let's get rid of this boilerplate

play02:31

nonsense we're not baby coders anymore

play02:32

we're like toddler coders taking our

play02:36

first steps now so

play02:39

okay what do we want to do

play02:41

you know we want to attack a player we

play02:43

want the player to die but what happens

play02:45

if the player dies what how can we check

play02:47

for that like how do we and how do we

play02:49

check whose turn it is to attack and

play02:51

things like that uh so let's have some

play02:54

fun all right so let's do an enemy

play02:55

attack right so we're gonna say set

play02:57

player health I'm going to close this

play03:00

left hand side here say set player

play03:03

Health minus equals enemy attack damage

play03:07

you've seen this before we're gonna take

play03:09

the Current player's health and subtract

play03:11

the enemy damage from it and set it back

play03:14

into the variable okay we've done that

play03:16

before

play03:17

now what if the player died

play03:20

like lost all their life like how do we

play03:22

check for that

play03:24

well

play03:25

we can say if

play03:28

player health is less than or equal to

play03:32

zero

play03:34

I'm gonna put a colon here at the end

play03:37

and by the way I press Escape whenever

play03:38

these dumb pop-ups come up here Escape

play03:41

and then I'm gonna say print

play03:44

player died

play03:47

sad face

play03:50

like so

play03:52

okay

play03:54

new stuff new syntax oh no okay

play03:58

we work with conditionals by specifying

play04:00

the

play04:02

if keyword the control flow where is our

play04:05

program going to flow if and then we use

play04:08

parentheses

play04:09

and we're going to say player health

play04:11

is less than or equal to okay so this is

play04:15

the less than or equals to operator uh

play04:19

the reason why we don't typically uh

play04:22

check for just health

play04:24

for the amount exactly is because what

play04:27

happens if the enemy attacks you you

play04:29

know for like 50 more than your life

play04:31

actually has well then this would go

play04:33

false so we want to say less than or

play04:36

equal to if it if it's equal to zero or

play04:38

less than we don't want to just say less

play04:39

than because then if it actually brought

play04:41

you down to zero uh it wouldn't fire

play04:44

either okay so less than or equal to in

play04:48

parentheses at the end of those

play04:51

parentheses we put a colon okay a colon

play04:55

here that says hey the next area down is

play04:58

a block of code notice how it's indented

play05:01

okay the indentation is important in

play05:04

verse okay it's yelling at us right it

play05:06

doesn't like that indentation is

play05:09

important I'm pressing tab all right

play05:11

player died so if the player health is

play05:14

less than or equal to zero player died

play05:16

so how much is the player's health So

play05:18

currently it started off at 100 uh an

play05:21

enemy attack damage is 51 so that

play05:22

dropped it down to what like 49. Okay so

play05:27

let's do another attack how about that

play05:29

set player Health minus equal enemy

play05:33

attack damage uh you know maybe what

play05:36

we'll do is uh you know if they get

play05:38

attacked again we're gonna say uh

play05:41

uh we'll say print player flinched

play05:45

enemy

play05:46

attacks again okay

play05:50

so then we're going to do it again

play05:52

and then what we can do is we can grab

play05:54

this code copy it

play05:57

and paste it down here

play05:59

so we attack once

play06:01

okay uh in fact just to be to make this

play06:05

more like a game print enemy attacks

play06:09

player okay enemy attacks player the

play06:13

attack is made jaw check the player's

play06:16

health

play06:18

check the player's Health let's see what

play06:20

happens okay

play06:22

um let's go ahead and go back to our

play06:24

unreal editor

play06:26

verse buildverse code

play06:29

push verse changes

play06:32

escape and start game alright tab to

play06:36

look at our log here if you're not in

play06:38

the log you can just click the log all

play06:41

right enemy attacks player player

play06:43

flinched enemy attacks again

play06:45

player died okay so our code worked it

play06:49

worked as expected so let's review it

play06:52

again

play06:53

you have to memorize some of this stuff

play06:55

you may it may sound confusing but it

play06:57

just takes lots of time and repetition

play06:58

memorize this syntax okay if if then if

play07:02

then just think that if then right like

play07:05

that's just how it works you know if I

play07:08

tell my wife how she should fix her

play07:11

problems then I'm going to get in very

play07:14

big trouble else if

play07:16

I listen and Nod and show appreciation

play07:19

even though I totally don't understand

play07:21

anything that's going on in her head

play07:22

then I get a hug and life is great

play07:26

uh okay so

play07:28

that's how it works control flow it's

play07:31

just like life every everything in life

play07:33

has you know we make decisions that's

play07:35

what we're doing we're making decisions

play07:36

here okay

play07:38

so

play07:39

let's make this a little bit more

play07:41

interesting right because we know you

play07:44

know how much damage the enemy has and

play07:46

we're kind of forcing the player to die

play07:47

let's actually make this like a real

play07:49

game what if we made it like Dungeons

play07:51

and Dragons where we roll the dice and

play07:54

then uh we get random numbers how cool

play07:56

would that be let's do that so what I'm

play07:58

going to do is up here at the top we're

play08:00

going to import a new module we're gonna

play08:03

say using

play08:06

verse dot org slash random we're going

play08:09

to use some code for generating random

play08:12

numbers that verse was kind enough to

play08:14

make for us uh and the reason why we

play08:16

import saying using is so we don't

play08:19

include a bunch of code into our project

play08:20

that we're not using and make you know

play08:22

our game smaller make things run faster

play08:24

it's we're just grabbing only the things

play08:26

that we need we're being polite little

play08:28

citizens here in the world of

play08:29

programming

play08:31

so let's go ahead and have some fun now

play08:33

if you can see random tabs like this

play08:35

where my cursor is in the wrong spot

play08:37

make sure you always go back to the

play08:39

right spot okay don't start in the wrong

play08:41

spot start where you're supposed to go

play08:43

because

play08:45

okay if I type in here all right we are

play08:47

still inside of our if statement okay

play08:50

the scope here belongs to our if

play08:53

statement however if I go like this

play08:55

all right uh guess what we are now

play08:59

outside of the if statement and this

play09:01

code will run no matter what indentation

play09:03

is very important in verse

play09:06

all right so go to the right spot and

play09:09

I'm going to say VAR player dice result

play09:12

of type int equals get random int and

play09:18

we're going to do parentheses between 1

play09:20

and 20. like a 20-sided die now get

play09:24

random int is a function if I control

play09:26

click it it's going to take me to verse

play09:28

dot digest and it's going to tell me

play09:30

what it does returns a random int

play09:32

between low and high inclusive inclusive

play09:36

meaning it's going to include uh those

play09:40

numbers okay

play09:41

so the one and the 20. all right if it

play09:44

was exclusive uh it would mean that it's

play09:47

going to only get the numbers 2 through

play09:50

19. so a player dice result okay let's

play09:54

do another one lar enemy dice result

play09:58

colon int equals get random int

play10:03

between 1 and 20. so imagine that the

play10:06

player rolls their dice the enemy rolls

play10:08

their Dice and we're gonna see who goes

play10:10

head to head and who attacks who and

play10:11

does what and all kinds of things and

play10:13

because our player is dead right now

play10:15

let's let's resurrect our player and

play10:17

let's say set player health

play10:20

equals 100

play10:22

and what do we want to do well

play10:25

first off let's just print the values of

play10:28

our dice so let's well first let's say

play10:31

print uh player and enemy World dice

play10:35

okay that's what happened uh that way

play10:38

when we're playing the game we can

play10:39

actually see the order of things that

play10:41

are happening and now we're going to

play10:42

print the player dice

play10:46

type in player dice result I'm pressing

play10:50

enter when it does the autocomplete

play10:52

and we're going to print

play10:54

the enemy dice

play10:56

we're going to say enemy dice result

play11:00

like so

play11:01

and

play11:03

let's print those on the screen and see

play11:04

what happens so we've used this random

play11:07

get random in it's a function or rather

play11:09

it's a let's go into here and look at it

play11:12

again so this is a module our random

play11:14

module that we included using the using

play11:17

keyword so we included this module which

play11:20

means it's going to make these available

play11:21

and these are functions now you might

play11:25

hear me say the word method and the word

play11:29

function and those are kind of the same

play11:31

thing really but in this case they are

play11:32

functions because they're publicly

play11:34

available

play11:35

methods belong to part of a class you'll

play11:37

learn about that later but they're the

play11:38

same thing so we're calling a function

play11:40

okay that's pre-built for us and we use

play11:43

the parentheses to actually call the

play11:44

function and then the function is asking

play11:47

us for two numbers a low number and a

play11:49

high number it's asking for integers

play11:50

here okay

play11:52

uh between 1 and 20. that's all that's

play11:54

happened save

play11:56

editor verse buildverse code push versus

play12:01

changes

play12:03

it's up to date start game

play12:07

okay

play12:08

so player and enemy World dice player

play12:11

dice 13 enemy dice 14. excellent it

play12:16

worked it gave us a random number pretty

play12:18

cool now

play12:21

every time I run the game it's going to

play12:22

give us a new number because on begin is

play12:24

called whenever we start the fortnite

play12:25

game so that number will change every

play12:27

single time we run the game which is

play12:29

pretty awesome

play12:30

now let's uh let's pretend that the uh

play12:35

enemy is attempting to attack the player

play12:38

we're gonna do a super simplified

play12:40

Dungeons and Dragons but basically you

play12:41

know if if an enemy goes to attack a

play12:44

player

play12:45

you know the player has to roll the

play12:46

device and they've got you know the

play12:48

player defense and enemy attack and if

play12:51

one dice is higher than the other it

play12:52

determines if you know the attack worked

play12:54

or not more or less so that's what we're

play12:56

gonna do okay we're gonna code some

play12:59

Dungeons and Dragons uh all right I'm

play13:01

gonna say if enemy dice result is

play13:05

greater than player dice result okay so

play13:09

if the enemy's dice is a bigger number

play13:11

than the player's number what do we want

play13:13

to do

play13:14

well we're going to make an attack okay

play13:16

only if it's bigger I didn't say greater

play13:18

than or equal to if it's a tie I'm still

play13:21

not going to make an attack I'm going to

play13:22

give the benefit to the player okay so

play13:24

that's just my logic choice so say set

play13:28

player health

play13:30

to minus equal

play13:32

enemy attack damage so only attack the

play13:35

player

play13:36

if the enemy rolled a higher die

play13:40

die die a higher number

play13:43

now what what else we want to do we can

play13:45

we can take this further okay how about

play13:47

we say else if ooh a new one okay else

play13:51

if else if

play13:54

enemy dice result equals player dice

play13:58

result what do we want to do so now

play14:01

actually we're handling I remember now

play14:02

we're handling the the case where it's

play14:05

it's the same attack right the same same

play14:08

number in this case here we're just

play14:09

going to take one life away from the

play14:11

player that's what I meant to say

play14:12

earlier minus equal one so a minor

play14:15

penalty for getting a tie okay

play14:18

else what else colon what print the

play14:23

enemy mist

play14:26

okay so that's pretty cool

play14:29

um we're gonna take away the player Dam

play14:31

or player Health in fact let's print

play14:33

this to see if these even work so we're

play14:34

going to say print uh attack enemy

play14:38

attack

play14:40

successful

play14:42

player HP is now we'll just do all the

play14:46

one line to keep it fast player health

play14:49

like so put our quotes in here and then

play14:51

down here what we'll do is we'll say

play14:54

print

play14:55

enemy

play14:57

attack

play15:00

uh we'll just say

play15:02

roll was a tie

play15:05

player lost one HP I'm gonna print that

play15:08

there otherwise the enemy missed okay

play15:12

let's go ahead and compile our code

play15:14

verse buildverse code

play15:17

push verse changes and load the game to

play15:20

see this in action well Let's Escape and

play15:22

start game

play15:23

and press tab so it looks like the enemy

play15:27

Miss player dice was 19 enemy dice was

play15:29

13 the enemy missed excellent press

play15:31

Escape okay excellent so our code is

play15:34

doing some things uh and it's working

play15:36

just fine and I'm not going to print

play15:38

through all the states but you can

play15:39

relaunch your game and test that these

play15:42

work if you want to be extra creative

play15:44

you know you could you could create a

play15:47

loop if you're really adventurous you

play15:49

could create a loop

play15:51

for these right here and loop through

play15:53

actually starting probably for all of

play15:55

these put all of those inside of a loop

play15:58

um and then you could print it over and

play16:00

over and over again we're not covering

play16:02

Loops just yet to not confuse you but

play16:04

just know if you wanted to have this

play16:05

test like a hundred times put it in a

play16:08

loop for 100 times and it'll give you

play16:09

different results every single time

play16:11

which is pretty cool so we just did some

play16:14

basic logic here which is awesome now I

play16:17

do want to show you uh one more thing

play16:20

that we can do with our if else

play16:23

statements here so I put parentheses in

play16:27

here there's another format a style to

play16:29

do this and the reason why I want to

play16:31

show this to you is because you're going

play16:32

to see it in other people's code you're

play16:34

going to see it in Epic Games code all

play16:37

right so we're going to say if colon

play16:40

notice not parentheses this time colon

play16:42

okay

play16:44

we're going to say enemy dice result is

play16:48

greater than player dice result

play16:52

all right

play16:55

player health

play16:57

is greater than 50.

play16:59

bear with me for a moment here

play17:02

and then what we're going to do is we're

play17:03

going to say then colon

play17:06

enter

play17:07

set layer health

play17:11

minus

play17:12

equals enemy attack damage

play17:16

else

play17:18

colon

play17:20

print the enemy Mist okay

play17:24

what's happening here so instead of

play17:27

doing parentheses what we do is we say

play17:29

if colon

play17:30

and the conditions that we're testing

play17:32

are actually inside of here okay we are

play17:35

testing various conditions we could put

play17:37

as many as we wanted here test them all

play17:39

sometimes that's nice because if you're

play17:42

inside of an if statement sometimes they

play17:43

get really long you know you say uh you

play17:45

know enemy Dives result is greater than

play17:47

player dies result and if you know

play17:49

player dice result

play17:53

result is you know greater less than or

play17:56

equal to 10 and if enemy dice result you

play18:03

know uh equals five and see how this is

play18:07

getting really really really long

play18:10

well if that was our logic instead of

play18:12

just using all of the you know using all

play18:15

of that crazy stuff you know you can

play18:17

just kind of move those uh

play18:20

into here

play18:22

just to make it more readable

play18:25

okay so instead of

play18:28

yeah you just put all your conditions in

play18:29

that one spot all right I'm gonna get

play18:31

rid of those two though

play18:34

then do something so in our case here if

play18:37

enemy dice result is greater than player

play18:39

dice result and if player health is

play18:42

greater than 50. so in this case here

play18:44

we're saying

play18:47

only let enemy attack player if player

play18:52

has more than 50 Health maybe it's a

play18:55

maybe it's a cut scene area in your game

play18:57

and you only want them to get hit up to

play18:59

a certain amount of damage and then like

play19:01

you know the hero is going to come in on

play19:03

a spaceship and you know save the

play19:06

Paladin who's attacking ogres with bows

play19:09

and arrows uh anyway that's our logic

play19:12

then okay so if then all right uh that's

play19:17

what's happening here so here's our then

play19:19

up here if then in our case here it's if

play19:22

and then now I like this format a lot

play19:25

because some of my ifs get pretty long

play19:27

as I'm working with uefn and so this

play19:30

becomes very nice in those cases else

play19:33

print the enemy Mist okay so that's a

play19:36

different way of working with if

play19:38

statements you can choose whatever one

play19:40

you like and there's nothing wrong with

play19:41

doing this at all do whatever works for

play19:43

you and keep it simple don't stress out

play19:45

about things one more thing I want to

play19:47

show you just for fun because you're

play19:49

going to see this a lot as well too is

play19:51

I'll copy this over there

play19:54

and I'm going to again always make sure

play19:56

you go to the right indentation I'm

play19:58

going to paste it here so another if

play20:00

statement what you'll see sometimes is

play20:02

if statements on the same line okay so

play20:06

you'll take off the colon like so

play20:09

and we'll do curly braces almost kind of

play20:11

like other programming languages all

play20:13

right and you'll see this on the same

play20:16

line often and I do these as well too

play20:18

and these are things that you especially

play20:20

use when you start learning about

play20:21

failure and failure contacts you know we

play20:24

can print uh enemy is attacking you'll

play20:28

see this as well too enemy is attack

play20:31

uh attacking all your base are belong to

play20:34

us okay so if enemy dice result is

play20:37

greater than players result notice we

play20:38

don't have the colon here on this one

play20:40

and we use curly braces instead that is

play20:42

another type of if statement that you

play20:44

will see in code all right pretty cool

play20:48

now

play20:50

that is it for now I'm going to give you

play20:53

some work here is your exercise create a

play20:57

simple turned based attack system where

play21:00

the player attacks the enemy and then

play21:02

the enemy attacks the player using

play21:03

random dice damage like above use if

play21:06

else and then to determine who is being

play21:08

attacked and then print that be sure to

play21:10

handle when one or the other dies so

play21:12

turn this into a real game with a print

play21:14

statements so enemy has life and you

play21:17

know attack damage and each player okay

play21:19

they randomly attack each other back and

play21:21

forth back and forth using the dice

play21:23

until one of them dies how cool would

play21:26

that be and then you print that up here

play21:28

and if you want to get really crazy and

play21:30

be adventurous okay let's say extra

play21:33

credit

play21:34

use a loop so you don't have to copy and

play21:38

paste code if you're not feeling

play21:40

adventurous don't worry we will get

play21:43

there so that's it for now hope you're

play21:46

learning some great things here make

play21:47

some video games yourself and there's

play21:49

more to come if you didn't know my name

play21:52

is Mark walbeck and you can watch the

play21:54

next video in this series right here and

play21:57

don't forget to subscribe and click the

play22:00

Bell so you can know when the next

play22:02

videos are released

Rate This

5.0 / 5 (0 votes)

関連タグ
VideospieleSteuerungsflussZufallszahlenRPGProgrammierenEinzelspielerEntwicklungCodeUnreal EngineMark Walbeck
英語で要約が必要ですか?