Intro to Variables - Unreal Engine basics - What, How, Why and When (Includes teleport example)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to another 3D Ross tutorial in today's video we're going to be going over variables what they are what kind of variables you might want to use how you can use them as well as some use case examples also stick around to the end of the video where I'll show you how to set up a basic teleportation system first things first what exactly is a variable well you can think of a variable as a container that stores information this information could be anything from a number to a string of text or even something more complex like an object mesh or another blueprint actor so in Unreal Engine it's quite straightforward to create variables especially with blueprints for this example I'm just going to use the basic third person character controller so I'm just using the third person example template you can use first person or whatever project and set up but I've just loaded a real engine and I've just loaded the third person example and I'm just going to go to third person Blueprints and the third person character here and we open this up I'm just going to pull this tab here so we can full screen it and this is our blueprint view here so you don't need to pay attention to this at the moment um so don't worry about that but simple interfac is scroll in and out with the mouse wheel and you use right Mouse button and drag to pan and left Mouse button to select or hold it down and drag select to drag select so to start off with our variables are found here in this left corner down here if you don't see this you can go to Windows and make sure my blueprint tab has turned on and just dock it wherever you would like it so to add a variable we just can press the plus icon here and you can name it whatever you would like so I just call this first variable so now let's talk about the different types of variables you can use if we open this drop down list here we can see loads of different ones we got Boolean bite integer integer 64 float name String text vector rotation transform and then we have others under interfaces structures Etc but the main ones that you'd want to know like the basic ones for starting off with and nor booleans which is true or false and then we have integer which is a whole number like 1 2 3 4 - 5 - 3 20 then we have a float which is a decimal number so 3.14 minus 0.25 that kind of thing and then we have a string with which is just text so string is different from text because as it says here text can be localized so if I write name in our project settings we can translate that name um the word name to the correct word for whatever language we're localizing for whereas the string is just a string of characters just like that and you just want to know transform which is the location rotation and scale of an object to use a variable in Unreal Engine you can just left click and drag into this event graph here and let go and then we can get our variable so we're getting the information or we can set it we can set it to something different so if you get then we can check what this value is here so I'm just going to delete that for now and we shall move on to some examples so for our first example I'm just going to create a Boolean which is going to call it rore pressed and you see why I'm calling get that in a moment so our value for our Boolean will show up here but we need to compile the graph first to see that so I'm just going to hit compile and save and now you can see we have a tick box so this represent our Boolean if it's true true or false so no tick in the Box means it's false left click adds a tick to the box which means it's true so I'm just going to keep the default value off for now so unless we set it to anything different it's going to be false just going compile and save that to make sure that's done so now what we could do to test these things is I'm going to do a key press so right click on our event graph and I'm going to type in r key press just type in R key and scroll down until we see the r key so we've got all the different Keys you can press here you can add what you want but I'm just going to start off with the r key so I found it here so I'm just going to left click that and it's give us this node here so nodes are just blocks of code to talk to each other and you string them together to create your final code in blue prints so what this node does is when we press the r key so press it will do the code from this and when we release the r key it'll do the code connected to this so what I'm going to do is when we press the r key we're going to set this Boolean to true so I'm just going to hold left click and drag this into the graph and I'm going to let go and I'm going to set and I'm going to left click drag this press page to the set pin here and when we press R I'm going to set our R press to true so I'm going to tick that I'm just going to compile and save that it's always good practice to compile and save your work now I'm going to press the T key so I'm just going to copy this R key so I'm going to contrl Z with it selected and crl + V to paste it and then under here we can select whatever key we want so we can type in we can find it or if we just press the keyboard icon here when it goes yellow I press the T key it now changes that to T so when we press T I'm going to check if we've already pressed the r key so this might seem like a strange example but if you're in the middle of reloading so when you press R we set reloading to True which means that if you try and shoot so if you try and press left click it won't let you shoot because we're currently reloading and then when we finish reloading we set the is reloading to false so that's kind of the real world example but I'm just trying to show an easy use case scenario that won't require further explanation so when we press T key I'm going to do a branch so I'm going to hold down B and press left click or you can right click and type in branch and click on that so a br branch is basically we've got this red pin here which represents a Boolean and if that Boolean is true we're going to do this code if the booing is false we're going to do this code so as we can see it's set to true you can uncheck it or check it here but what we actually want to do which is more useful is hold left click and drag in our R press and I'm going to get and I'm going to hold left click and drag these pins together so now if our R pressed variable is true we're going to do this code if is false we're going to do this code so I'm just going to hook the T key pressed to the branch and then that's going to check if our R key is pressed so if our R key is pressed for this example I'm just going to print a string so it's going to say something uh normally you'd want to set extra code to do whatever but I don't want to make it too complex so if you right click and type in print string and select print string Under development what this will do is it will print this text in the top left corner of your game view so this is normally made for debugging just to make sure that your code's working and you can check that it's working in certain places so if I plug this in here and this never happens I know that this is not turning true and that's where the issue is lying but for this I'm just going to show show you example of variables working in process so if our key press is true we can set it to say something like R is pressed already and then I'm going to do another print string on false and just as an example I'm going to create a variable and I'm going to name this variable um not pressed R key string and I'm going to change the variable type to a string and I'm going to compile that so that leaves our variable open to editing and I'm just going to write what we want to display in here so this can be used so you can update this variable and it will update everywhere that you've got this in your graph so if you're reusing values you want to store it in a variable so you can call upon that later or you can change it in one spot and it will update everywhere in your code so that is a fundamental thing of why variables are great so in this value here we're going to do it for the false so I'm just going to say R key not pressed and hit enter and I'm just going to left click drag that in and we can get or we can left trick drag this in and go over the pin and it says make in string equal not pressed R key string so I'm just going to press that and now this print string is going to display whatever this variable says so pretty simple right so I'm going to do a more complicated example in a minute and we're going to get player location and make the player teleport but for now I'm just going to save this so I'm going to compile and save so remember if we press the r key it sets our R press to true if we press the T key it will see if R is true and depending if R is true or not it will say something different so I'm going to go back to the third person map and I'm just going to press play to play our game so at first I'm just going to press the T key and this should see that we haven't pressed R yet because we haven't so I go press T and it says r key not pressed I'm going to press T again just to make sure nothing's changed says R key not pressed now if we press the r key and then I press the T key it now says R is pressed already so now that has been pressed we've changed that variable to true so I'm just going to hit escape to go back out with that and I'm going to open our character blueprint again so now that's a good example of using Boolean and also a string but now we might want to use something more complex such as a transform so I'm going to create a new variable and I'm just going to call this player transform so a transform is basically a location But it includes the location and the rotation in scale of an object so I'm just going to create a transform here and I'm going to compile that to open up the variable and if we bring down this dropdown we can see we've got lo location rotation and scale so what I'm going to do now is I'm going to copy this key press again just so we can easily see our examples I'm going to set it when we press y so when we press y we can what we will do is I will get our object so this can also be a variable objects so our player is an object blueprint a cube could be an object that kind of thing so I'm going to go to the components tab here again if you don't have this go to window and turn components on and you can drag this in so you can drag any component from here into the graph and then we can change this component so for the Collision example we can change the Collision size but for this I want to do self and as you can see we can't drag in the top part because it is us so to reference us we could just reference self in the code so if I right click and type in set transform set actor transform click that now under Target we can set these things here and we can add this target we can't add it here because this isn't an actor so our blueprint is an actor and everything inside of it is a component but if we had set actor transform we could set the transform of each particular actor and we'd plug that into Target because we want to Target that actor and set that actor actors's transform but in this case we want to set self which is the default value so this third person character we want to set everything in here so itself so when we press y we can set the transform of our actor and we can set that to our player transform here so we can get that and set it to that so now this will set our players location to 0000 0 rotation 0000 0 and set the scale to one but first what's probably good is to get so I'm going to right click and type in get actor transform and now this will get the current transform of our actor so if I right click and type in print string like we did earlier I can connect the get actor transform to our print string but as you can see it's not the same type of data but we can convert that data so far I pull this pin which is a transform so a transform is orange and try and plug it into the string it says convert transform to string and basically that's converting this data into something that's text based so we can display the transform so when we press y we're going to print the act as transform so I'm just going to press compile and save and now when we play the game again and I press y it now shows our players transform in the top left corner so if I move around and spam it as you can see it's changing because we're moving around if I stay the same the players transform is not changing so now you can see that data right there we can do something useful with that data so what I'm going to do is set our actor transform so what I'm going to do with that is is our player transform when we press y we can set the player transform and I'm going to set it to the current transform at the time so when we press y it's going to save the transform of our player so location rotation and scale um and it's going to save that to this variable and we can reference that later on so we're saving it when we press Y and then when we press the U key so I'm going to just copy that and paste it and press U here when we press the U key we're going to set our characters transform to the transform that it was when we pressed y so I'm going to compile and save that so now if I move over here and I stand on this Edge and press y That's saved our transform to that variable and now if I move over this corner and press U we're back to where we pressed Y and we could do the same so I press Y and then I press U and we teleport back to that location because we're saving our a location when we press y saving that in our variable and then when we press U U we're then using that variable to set the act as transform so we're storing that data in the Box in the variables and then we're calling it again at a later date and using that elsewhere so that's a basic rundown of variables and how you can use them in a simplistic way so we've went over booleans we've went over string and we've went over transform I can give another quick example um might take a bit longer to give a full example in the situation of a game but if we get a variable and I just call it number one and I set the variable type to an integer I do number two and that's an integer as well we compile that and save that I'll just set this variable to one this variable to two compile save and we drag in number one get and number two get and we can drag off this and do an add so we can add these two together and we get an output here so we'll get an output of three because we got 1 + 2 equal 3 so if we do another print string just to debug what we're doing when we press r R convert again you can convert an integer to a string to display that compile and save so now when we press R it should display three in the corner up there now another thing to be aware of with variables is this I here so what we can do is we can press this I and what that does is toggle instance edible on that variable so if I compile and save that what that does is if I drag my player in for example it now shows that Val value here so we can change that directly in the viewport without changing it in our code so if we had multiple objects so we had a Wall's maximum damage to break we could duplicate that wall and say this wall has more damage so set that damage to 500 whereas this wall here we set to 300 and then all the code will work the same just that health value is different for each of the objects and that way we can just control that a lot easier inside of the editor which is pretty cool so yep we've went over now booleans strings transforms and integers and then of course the same will happen with a float so we can change that and that's went red so I just hold alt and left click to remove that connection and then we can connect them together so if I disconnect that now as well it's doing integer plus a float which is going to give us a float value because it's not going to be a whole number bless me so the output's going to be a float so that's why this has turned to Green now so with number two we can do a whole number but we can also do a part number as well I need to compile that first so now we can set that at 2.5 instead of two so now we should get 3.5 when we press R where if we try number one to change out at 2.5 it will not it just runs down to two because it's not a float but you'd want to use integers where you're always going to have whole numbers one just to make sure it is a whole number but two that saves a lot more processing power than a float for example so just use them sparingly so I think I'm going to wrap it up for this tutorial um I will link at the end of this video a playlist of my programming basic series a lot of it might not be out yet I am working on it and editing a few videos but I thought I'd get this one out for you just to explain some key fundamentals um and I hope it helps you in your game death Journey if you found this video helpful please do give it a like and consider subscribing it is free and check out my other videos that you might find helpful and thank you for watching and I'll see you in the next video
Info
Channel: 3D Ross
Views: 45
Rating: undefined out of 5
Keywords:
Id: m6OixPGaC7A
Channel Id: undefined
Length: 23min 5sec (1385 seconds)
Published: Fri Jun 14 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.