[Music] hello everyone and welcome back to the course in this lesson we will write our very first script using the event graph inside our custom class we'll also learn about the differences between design time and run time and look at some common events included within the graph let's get started you'll recall from the previous lesson that the blueprint editor comes with these two tabs right here the event graph and the construction script every class in unreal engine comes with these two graphs and this is actually where you write your code this is where your source
code lives and to really understand the difference here we're gonna talk about how unreal engine actually runs your game unreal engine has two basic states first one is called design time or editor time and it refers to what happens when you're inside the editor creating your game everything we've done so far is part of design time you can move actors around and customize your level this is where the construction script comes in as the name implies unreal engine runs this script every time it compiles a class this gives us a lot of flexibility because we
can add functionality or randomness inside our construction script to give each instance of a class unique properties we will take a closer look at the construction script in a later lesson the other state is often called run time and it refers to what happens when unreal is actually running your game to enter runtime simply click play on the top bar this is where our event graph comes into play the event graph holds all the source code that will be executed at runtime so if you want to add functionality that happens while the game is running
this is where we will add it so now that we know the difference between design time and runtime we can decide where to place our code based on our needs and if you want to see what runtime actually looks like you can go back to the third person map here and right here at the top press the play button so you can see our player character spawns in the level we can use the wasd keys to move around and we can use the mouse to look around the level to exit run time simply press escape
on your keyboard now let's go back to the event graph so we can write our first script click on the generic actor and right here on the event graph click it if you haven't and here we are on our event graph some navigation tips first you can right click your mouse button and while hold it you can move your mouse to pan around the graph you can use your scroll wheel on the mouse to zoom in and out you can left click and drag to get a selection box where you can select one or more
nodes on the graph and if you simply left click and drag you can simply move the notes on the graph so how do you actually add notes inside the graph you simply right click and a menu will appear where you can scroll down and see all of the available nodes by category alternatively you can simply search by clicking on the search bar and looking for a specific node i'm going to add several nodes for demonstration purposes now that we have the nodes added to the graph we can simply connect them by dragging from one note
to the other like so as you can see this white line is called the execution pin and it basically tells us the order in which the notes will be executed so as we connect one node to the other like so we can see how the nodes will be executed the set actor location node will be executed first then the set actor rotation node and finally the print string node notice that the nodes have several different pins as well for example the set actor location node has a vector 3 pin where you can manually enter the
values of x y c here or you could potentially connect a variable to the pin here to disconnect a node simply press alt on your keyboard and click the connection and that would disconnect the node now let's go ahead and select all of the nodes by left clicking and dragging the selection box select all of the nodes that you've added and press delete on your keyboard we'll do the same thing for the second node called event actor begin overlap we will cover this in a later lesson for this lesson we'll focus on the event begin
play and the event tick the event begin play node executes any code that is connected to its pin once when the game begins in the event tick node executes whatever code is connected to its pin once every frame in other words if your game is running at 60 frames per second your code is running 60 times per second so let's go ahead and test this out we use a node called print string so go ahead and right click on your graph and search for print string go ahead now and connect the begin play node to
your print string node like so and we'll change the in string value to begin play now go ahead and compile and save your code and let's go back to the third person map now before we press play we want to remove two of the instances of our class in the level so if we zoom out here as we had before you'll remember that we have three different instances of our custom class in the level if we press play right now each individual instance of our class will execute our code on beginplay for now though we'll
go ahead and remove the second and third instances from our level we can go ahead and select them right here in the outliner and press delete on our keyboard [Music] now when we press play pay attention to the top left corner of our screen so you can actually see the message [Music] as we just saw on the left hand side our code executed once when the game started and displayed a message for about five seconds we can go ahead and stop runtime and let's go back to our blueprint now let's test tick by doing the
exact same thing however this time let's enter the text ticking and let's disconnect the begin play node here compile and save and let's go back to the level and see what happens as you can see our blueprint is printing the word ticking on every frame now let's go ahead and stop and go back to our blueprint now let's create an integer variable and call it hp [Music] and set its default value to 600. [Music] what we want to do is display this value on the screen so you can simply click and drag the variable to
the graph like so [Music] and you'll notice that we have two messages get hp and set hp the get hp option will create a node that will return the value of hp in this case 600. and the set hp option will create a node that allows us to set the value of hp these are called getters and setters for this example we'll select get hp and now you can see that we have a node with the name of our variable and a pin and this pin if you drag it will return the value of our
variable so in this case all we have to do is simply connect our hp node to the in-stream pin of our print string now and notice that unreal automatically converts our integer into a string so all we have to do now is simply connect event beginplay with our print node and let's go ahead and disconnect this node here compile and save [Music] now let's go back to the third person map and click play [Music] you can see that our hp value displays correctly [Music] let's go ahead and stop and go back to our blueprint now
let's get a bit more creative say that we wanted to simulate that our player is taking damage every frame so how would we do this well we would get the value of hp subtract 1 and then display that value so let's go ahead and do that first let's go ahead and drag our hp value to the graph click get hp and then drag from the node and search for subtract as you can see this node will take whatever comes in this pin and subtract whatever number is here so let's go ahead and enter one now
let's go ahead and set our hp to this new value and to do that we once again drag our hp value to the graph but in this case select set hp let's connect the nodes as so as you can see here on every frame or tick we are grabbing the value of hp we are subtracting one and then we are setting that new value to hp once again now all we have to do is display this new value with our print string so go ahead and drag from set hp and you can drag directly from
this pin into the in string pin right here and as you can see we have our hp subtract one set it back to the the value of hp and display it on the print string here finally let's go ahead and click on the arrow here and change our duration from two seconds we'll go ahead and change it to zero and all this will do is display the value once per frame go ahead and compile and save and let's go back to the third person map and press play you can see that our hp value is
going down as expected now let's go ahead and stop and go back to our blueprint so let's do a quick recap in this lesson we learned about the sign time versus run time we learned how to add nodes to our graph we learned about the common events begin play and tick we learned about variable getters and setters and we'll learn how we can display a value on the screen if you want to practice what we learned search the list of notes and familiarize yourself with the categories also practice adding notes to begin play and tick
and that's it for this lesson thank you so much for watching and i'll see you in the next one [Music] you