UE4 With Casey - Mastering Blueprints - Part 1 - Events

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey guys in this video I will be going over part one of what I believe are the three most important concepts for a beginner to master in blueprints and the reason why I'm making these videos is that I've gotten a few friends and even a family member into making video games and they've gotten into unreal they've started using blueprints and while they've gotten a few of the basics down what they want to go and make something new or when they want to do work usually the first thing they do is they go to YouTube and they look up a tutorial and while tutorials can be okay I think they can be destructive if you are just copying them cuz what they end up doing is they watch these videos they copy exactly what the person is doing in the video but they don't understand what they're doing or why and then usually they'll run into some type of roadblock somewhere they come to me and while I'm looking through their code I'll ask them why they did something a certain way and they will just say I don't know that's just what the tutorial did and I really think that's not useful I don't think that's helpful and I really don't feel like you are learning so the purpose of these videos is that I want to try and give you a good foundation of how to think about your code how to think ahead and plan so that hopefully when you do get to a part where you want to do something new you can think it through a little bit you can get a general idea of what you want to do and how you want to do it but because you're a beginner you're not gonna know all the tools that you have available to you and that's where tutorials come in and they help fill that logic gap but with the good foundation while you watch that tutorial you can start learning and you can start thinking critically about why they're doing what they're doing and that's really what I want to try and help you guys with so in this first video we're gonna go over the first concept that I think is important and that is events but before we get into that this project is simply just the default standard first person template you can see the text right there I have not touched this project at all so if you want to follow along you are welcome to so the first thing that let's work through is let's just say conceptually I want to take one of these boxes and I want to make them spin inside of the like I want to make out an actor and I want to spin it so where I would start as I know my end product is I want to take a box and I want to rotate it so to add some functionality let's actually make an actor and if I click inside of an open space down here or if I even do add new I can create a new actor so let's create a blueprint blueprint class and here we're gonna select actor now as you can see an actor is just something that exists inside of the world it's something that can be placed or spawned in the world it's just basically the most common type of type of blueprint that you will use or type of actor that you will use is just the base actor class and here let's just call it Q so if we open up our blueprint now what we can do in the top left is that we can add a cube to it so now we have our cube and we have our blueprint and now with the blueprint we have some code so if I knew I want to spin this cube in the world I want to have it constantly inside of the world just rotating we have a few events available to us and I guess if I take one step back what I believe this first important concept is is understanding these red events that we see here the basis of all code or the start of all code is an event you can't run code without having some type of starting point to have some type of event to start it so when I think about I want something to happen constantly I want this to constantly be spinning in the world I know my end result I know my end result rather is that I want to set the rotation of this actor but when how often do I want to do it whether what is my event well I don't want it like event big employee that happens once at the start of the actor I know I'm not just setting its rotation once I want it to happen constantly so logically what I would say is event tick is your die here you could use something like a timeline or some some other type of events but really the main one that we're gonna be talking about is even tick tick if we highlight it it says it's called every frame I like to just think about tick as being 60 times a second I believe it can be more it can be less but how I view it is this happens a lot per second the exact amount I'm not quite sure some of the times but here it's gonna be happening a lot and it's going to be happening constantly so when we hook this up while I work through this project and then try and go through my thought process I wouldn't worry too much about the math the calculation part of this well I would be more concerned with them but I really want you guys to learn is learning kind of a thought process so personally I just know that I want to get the current rotation of my actor and I want to break it and that what I want to do is I just want to be working with Z here that is the yah I know I just want to constantly add to it and that that will spin the actor in the world and if I make a rotator now I can just hook it up I don't want to touch the X of the Y I'm not trying to change its picture its role I just want to spin it along its yaw so here what's really gonna happen inside of the world with our cube is that ontic we're gonna spin or cube so so let's try that out so if we drag our cube into the world and we hit play you can see our cube spins and why that is is that ontic which this is however many times per second we are running this code we're taking it Z and you can see we're adding 1 to it and then we're setting the rotation pretty simple but the key here is that it's on tick if we put this on begin play if we put this on begin overlap any of these other events it wouldn't behave the same exact way that we have it here so it's very important to realize tik can be very powerful because you're doing it at a lot but at the same time tik can be very destructive because if you're doing something expensive off of tik it's gonna be running a ton and your performance will probably start dying down there are a very good uses for tic and there are very bad uses and as you get more familiar you will learn how to stay away from the bad but here we just want to spin it constantly tik is our guy so let's move to the next type of event let's use event begin play and how event begin play works is if we highlight it it says event when this when play begins for this actor how I like to think of it is I like to think when would the first event tick B of of this actor here it would be when the level starts since we have it dragged how since we have it dragged into the level right when this level starts on the first tick even begin play would run but let's say this cube didn't exist in the world let's say we spawned this cube one hour into our game well the first begin play for that cube would be that one hour in that mark that we first spun it so it's good to realize that event begin play is not one game starts and it might not even be when your level starts it's when the actor has its first tick whichever actor it is it's when it exists that first tick that it exists so let's actually delete our cube out of the world and let's use our level blueprint so let's use the event begin play on our level blueprint and we can either type event begin play and we could search for it or if you right-click you'll see that here that we have a deven in our drop-down and if we open it up we can see that there's actually a ton of events we can keep opening these boxes and that's not all the events that we have available to us when you start using more actors and different actors you're gonna get a ton of events exposed to you so we're only gonna focus on the main ones but you but it's good to understand that events are the basis of our code and they all have usefulness there's a there's always a time for one of these events to be used and as you start to get more familiar you'll start to be thinking in these types of ways that when you approach new code you think well what event am I going to use and based on some other factors like how often do we want to call the code how are we calling the code that will lead you to use different events so here what I want to do is at the beginning of the game when our level first starts up on the first take of the game let's spawn that cube in the world and I know event begin play that's gonna be right when our level starts because we're in the level blueprint so right when our level starts we're going to launch this code so let's spawn actor from class and let's select our cube or a cube actor so if we type in cube we see it at the bottom here and we can do our cube and the one important thing that we want to worry about here is our transform so a transform if we do a make transform you can see it's a location and rotation in the scale I'm not too worried about the scale what we really care about is the location so if we click on our actor here if we click on our first person actor you can see its location on the right here it's kind of weird this isn't like we're at zero zero zero our route of our game is a bit offset and it's a little high so what I'm gonna do to get a location is I'm actually gonna cheat I'm actually gonna take this text actor and I'm just gonna kind of drag it up in the world a little bit and I'm just gonna take its location so what I'm gonna do actually is that if I have this text actor selected so I physically have it if I go back in the level blueprint by right-click I can get a reference to that text actor and with that off of it I can do get after location and here we go we have a location and now when i hook this up when we launch this code we can read it as at the beginning of the at of the level we're gonna spawn our actor at the location of that text actor that's how we can read through this logic so now if we hit play you can see at the start of the game we spawned our cube act the location of that text and you can see it's spinning because ontic if we go and look at our code we can see we are constantly rotating it so that's event begin play and that is event tick begin play is often used for say if you're making like a first-person shooter usually what you'll do is on begin play you'll attach your gun to the socket of your hands or something of that sort event the gameplay is really useful for maybe spawning a HUD which actually let's do that right now let's let's create a simple HUD so we can go in and we create a user interface and we can do a a widget blueprint all I'm doing here is I'm right-clicking in this open space user interface widget blueprint let's call this our user HUD and now what we can do is with this user HUD let's just make it really simple let's just add text to it and we can say this is a HUD so let's say at the start of our game when our player spawns we want to create this HUD in front of them what we can do is that we can go inside of our player we can go inside of the code the the blueprint of our player we can do that by selecting him opening his blueprint and then if we find our event you can play we can search for it in enter we can see it already exists this code is just for use of a I believe a VR headset we don't really care about it so we're gonna unhook it and what we can do is that on begin play cuz right at the start of our actor existing when our player starts we want to create this HUD we can do create widget and once again we're using begin plays we want this happen right at the start and actually that's a little confusing let's actually rename that so it's a little close to like the default name let's just call this hug and now what we can do is that we can do select class and instead of to having user hiding user widget will just call it HUD and we can take and then add to viewport add to viewport is just how you um how you put things to the screen how you put widgets to the screen if we just want it to be like over top so now at the beginning of the game for our player we're gonna create the HUD and add it to our viewport so let's try it and here you can see at the top of our screen this is a high and that's what begin play is really useful for it's for when we know at the start of our game at the start of our actor it can get more complex than that we want things to happen here we're just popping up the HUD or we're spawning the cube in the level it's pretty simple so let's unhook that so it's not in our way and let's look at the next event we'll use and next one I think is good to learn is we will use overlaps so there's a few different ways to use overlap so you can use them in your level if we go to all classes I believe it's called a box trigger so we can drag this into the level and we can use a box trigger but let's actually attach it to our cube so if we take our cube we can in this top left in our components if you go to our viewport we can see our cube we can add let's add it to our cube and let's add a if we go into a collision we can see we have some collisions let's add a box collision over top of it I don't believe we'll be able to see it because it's very tiny but if we up its extents we can make it 100 100 100 you can see we have a decent size around our cube and what we can do with this with this box collision is that if we have it selected and we right-click we can actually do an add event for our box and what we can do is we can go under collision and we can do begin overlap so add on component begin overlap so here if we highlight this it says event called when something starts to overlap this component sounds useful so what we could use this for is that when our player walks into this overlap box surrounding the cube we can run some code so let's add a little bit of complexity to our code and let's actually make it so that we can stop this box from rotating when we want it to or the cube from rotating it rotating from when we want to so we can create a branch and if we drag off of this pin we can do promote to variable or if we wanted to on this bottom left we can do add variable we want it to be a boolean so we want it to be red and we can call this spin question mark let's just axe ask ourself a question do we want to spin when I compile what we get down here is we can change the default value let's set the default value to be true so at the start of a game we will be spinning and that what happens is that on ticks so 60 times a second we will be hitting this branch and we'll be asking ourselves a true/false question should we be spinning if yes let's keep rotating if not let's just not do anything and then what we can do is down on our overlap box so once again when our player walks into this box what we can do is that we can take this spin variable and let's just set it to be false so let's try this out what we can actually do is that we can actually make this not be hidden so that we can visually see our overlap box so now at the start of the game on begin play we spawn our queue and oh let's actually do one more thing actually I didn't want to add this complexity but we're gonna have to what's happening is that at the start of the game this is actually overlapping with the text actor so with the overlap box if we check its collision this is gonna be a little bit more complex than I wanted to get into but very briefly if we check the collision we can see that it's gonna be interacting with a lot of different things here it's interacting with some world static dynamic pawn vehicles basically whatever is entering this box this box fires our issue is that that text actor in the world we're responding the box is overlapping so what we're gonna actually do is we're gonna take this actor reference that we have here this other actor that we're overlapping and let's actually cast it by casting we're asking hey when you overlap this actor that were overlapped with is it something like we can specify is this and we can ask hey is this our firstpersoncharacter and what we can do is we can go to first person character and what we're basically asking this overlap box is hey when when this event fires when something overlaps with our trigger box is that after is the first person character if it is a first person character we will run our code but you can see we have a cast failed pin and basically we're saying if that's not our first person character don't do anything over that box just just check to see if it is we're not gonna do anything if we don't want it to be so now when we hit play you can see it's spinning because what happened is it asked hey that text actor are you our first person character no then don't do anything but now if we walk inside the overlap you see we collided with itched a little bit it stopped rotating that's because it overlapped its we said hey are you our firstpersoncharacter it said yes we expect that we set the spool spin boolean to be false on tik if we actually do a debug for a cube we can see the orange saying this is what's happening right now and then tik it's happening a lot and we're asking are we spinning we set it to be false no code that's happening so that's how we can use overlap events I think overlap events sometimes are the easiest for beginners to conceptualize because it's really simple right we have this area we can physically see it like if I hit play we can physically see that overlap box and we can say hey if something enters it let's run some code it's really obvious it's really simple I would say beginners tend to overuse or black boxes in an end game you actually for me I try to avoid using overlap boxes a bit because there are some performance concerns with them but for beginner purposes I think they're great and they're very they're very good with feedback it's really obvious between having like a yes or no is something inside of it I think the one hiccup that people tend to have is this overlap will run whenever anything enters it based on the collision settings and we can see here inner collision settings it's overlapping with everything what we did is we did one more step to it we said hey whether are we overlapping with that other actor we're just making sure it's our firstpersoncharacter before we run our code and that's kind of the check that you often need to do with overlap boxes the next really obvious events we're actually gonna unhook that no we'll leave it the next thing the next type of event we're going to talk about is user input events the most obvious user input event for example this first person game that we have we can click we can shoot so every time I live click we're shooting it and that's also fairly obvious what that is actually called is that's called an input action or there's also input access but we're just gonna focus on the input action here you see we have input action fire there's a few different ways to navigate to where these inputs are if you go to settings project settings or you can do file or is it under edit edit project settings there's a bunch of different ways to navigate to this but inside the project settings if we go over under input you'll see up here we have bindings we have our action mappings and our access mappings I wouldn't worry too much about the axis mappings for now but for the action mappings these are for single button presses so we can see here we have the fire event and if we check underneath it you see that by default we have the left mouse button or a gamepad right trigger and a motion controller so if we do any of these inputs on our peripherals so on our left Mouse if we are our mouse if we hit our left mouse button we're saying that we want this event called fire to happen so if we go back to our code you'll see we have input action fire and simply how you get to that event is if you just type in the name of the event so if you type in fire you can see we get our action events and fire and what you do is you get two pins off of user inputs you get a pressed and a released so when we left click the pressed will fire and when we release the release will fire here if we simply look at the default code we're playing a montage on our gun that's really just for like recoil we're spawning a projectile which is that ball we're shooting and then we're playing a sound fairly simple right so that really allows the player that we can check for inputs we can say hey when the player wants the left click and shoot we want to shoot something it's fairly straightforward I think this is also really easy for beginners to grasp because it's a lot like the overlap it's it's very intuitive how they work there's no like hidden magic behind it so yeah those are input events and I think those fairly straightforward when we want to receive input from the player to drive something that's what we want to use like we don't use like event begin play right we don't want to shoot a bullet at the first event of our actor we don't want to use event tick we're not trying to shoot every single tick of the game so it's really intuitive that we want to user inputs so let's set up a user input event there's two more events that I want to go over and may our custom events and functions so if you go back into our cube custom events in functions behave very similarly there's a few differences they're hard to really understand or it's more when you get into complexity it's some more complexity that you start to understand the differences but we'll go over them very briefly but if we search custom event you can see here that get ad custom event or if we do our drop-down box on ad even you can see it's right here but you can notice how there's a different symbol on the left here it's not open it's not like blank within that area arrow there's some extra stuff there and the reason being is that a custom event is a bit different from normal events so it's an event with customisable name and parameters so it's pretty cool that we can add a custom event and we can name this let's call it the spin of it and what we can actually do with this custom event is if we want to move our code off of event tick to spin we can set this on to our custom event and then what we can do is we can then call that custom event so really if you want to think about this like begin overlap we don't really have like a manual control over win something overlaps with that with our overlap box right that's up to like the course of the game whether or not we walk into that box however with the custom event it's up to us when we want to call it here we're calling that on the event tick and all you have to do is search for the name of what you named the event we named it spin event so if we search spin of n we can see we have call function and our spin event and really this is gonna behave just like the code did before cuz all we did is we moved the code off of the event tip to our custom event and then we're calling our custom event on tick so really nothing's gonna change inside of our game it is still gonna spin exactly the same and if we look at our code and drop down our debug you can see the orange on tick it's constantly calling our spin event and then here you can see it's constantly calling the spin event spin event down below so custom events are really just events when we want them to be called this is a lot like how functions work so on the left side here you can see we have a area called functions if we hit the plus button we can have a new function and then we can call it if we want spin function but you can notice that we're no longer in our event graph when we work in a function you can see how it puts us kind of in our own space it's separated a good way to kind of maybe conceptualize functions is that they are separated they're kind of like segregated away from the rest of your code and there's some good reasons for that so what we're actually gonna do is let's copy paste our spin code and let's put it in a spin function and then what we're gonna do is afterwards let's delete this custom event that we're calling off of tick and let's call our spin function you can see because we call that function just like we do with our custom event or if we wanted to we can take our spin function from the left side here and we can drag it in and just hook it up that way just like the custom event this will behave almost exactly the same we can open up our code we can check our debug we can see our orange is calling the function if we double click the function and open it you can see it's behaving almost exactly the same as the custom event so that kind of brings into question how do they differ well with the event graph you're able to work with timing and what I mean by that is that we're able to use delays we're able to use stuff like timelines all of these things they affect timing like we could delay our spin events that we could delay it by two seconds before spinning if we open up our spin function and tried to call delay we don't get anything if we tried to do a timeline we don't have the the the the node for a timeline here that's because functions cannot be based on time if you don't quite understand what that means I wouldn't worry about it too much you'll learn it over time just on a very basic level understand you can't use delays or timelines inside the functions you can use delays or font or timelines inside of your event graph which is where you have custom events so that's one very important difference the other main important difference I would say is that if we open up our function on the bottom left you see we have this area called local variables so if we make a local variable you will see that this is seperated from our actual variables you can call this just for purposes we can say local float we can promote this to a float and now with this float we could get it we can set it we could change its default value we can hook this up to our math and do stuff with it um but if you look at our event graph you'll see that area disappeared we no longer have local floats and if we tried to like go in an open space and say a local float nothing pops up the reason being is that inside the functions we're able to make something called local variables these are not global these are not accessible everywhere these are only accessible within our functions so why would we want to use them the main reason to me is for performance and that is is that at the end of our function when it does its last piece of logic and it is done and the function ceases it just stops all of our local variables will get cleared from memory and they will cease to exist so that means that if we like zoomed out we had like a bunch of math and a bunch of calculations we were using a bunch of variables it could be very good to put that inside of a function and use local variables because then at the end we can clear them out and that they don't exist and we clear them out from memory some things though if we did go into our map we're gonna go over what I'm about to use here in the next video and that is actor references but if we went to our level blueprint off the right side of our spawn we get an actor reference to our cube if we wanted to we could set spinning so that means that from our level blueprint with our actor reference we can manipulate our cube once again we're gonna go much deeper with this in the next video but if we did that and we tried to access that local float you can see how once again we don't get access to it so it's an interesting thing to note that if we want to be manipulating these variables if we want to be working with them and doing some heavy stuff maybe externally that might be a reason not to use local variables whereas with spin if we want to change it somewhere else that's why we would want to use a normal variable and not a local variable so some interesting distinctions there but I believe these are the most important events to at least understand and if we really abstract this we can really chunk code maybe into three pieces you have a beginning a middle and an end at the beginning it's what event are we gonna use how often do we need to use this event do we need to be doing this on tick are we trying to do it constantly if we go to our level blueprint is this just something we want to happen once at the beginning of the game these are some considerations and some things to think about next you have your middle of the code I like to just consider this like the calculation like here on the spawn I just know that this is like the calculation I know at the beginning I want this to happen in the middle I know I'm gonna be doing some type of math and at the end kind of the ending of our code we have the beginning the middle end at the end it's gonna be our end node here we're just trying to spawn an actor or our end group announced some times you want to achieve multiple things but when you approach new code that's what how I would really advise looking at it what is the beginning what events do I think I'm gonna use how often where do I want to do it how am I going to call it what event do I use and then kind of see I would probably talk to the end what is the end result that I'm looking for here I just wanted to spawn our cube and then in the middle we have our math and it's just trying to figure out where exactly am I spawning that transform at so that's how I visualize code and what we just went over were events I hope that was useful and then in the next video we're gonna go over what I believe is the second most important thing and that is what we briefly did in this level blueprint with this actor reference we're gonna go over how do blueprints communicate how can I manipulate that cube inside of our level how can I manipulate it inside of our player how can we change things inside of our HUD at runtime and that's actor references or object references and that will be the next video I believe all of these things are hugely important and I really believe that this will help you understand tutorials so I hope you stick around and I can see you guys back in the next video
Info
Channel: UE4WithCasey
Views: 45,574
Rating: undefined out of 5
Keywords: ue4, unreal engine, tutorial, blueprints, events, master
Id: S9KforwzAAU
Channel Id: undefined
Length: 27min 57sec (1677 seconds)
Published: Sun Jul 08 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.