‫Hello and welcome.
‫In this lecture, we're going to be using variables to keep track of how much ammo we've got.
‫If you look in the top left corner, you can see that it's telling me the ammo value and each time I
‫fire it is decreasing that value.
‫Let's dive in and find out how variables help us do this.
‫So we're going to need variables in order to keep track of our ammo.
‫So what our variables?
‫Variables help us store, manipulate and refer back to information.
‫And most crucially for us, it's going to be that storage of the information.
‫So it's a bit like a box that we label as ammo.
‫And inside it we can put a value such as 20, which tells us how much ammo we've got left.
‫So each variable has to have a name.
‫It contains some data such as the data value 20 and crucially, each variable is of a particular type.
‫So in this case, the ammo box could be said to be an integer because it contains whole numbers.
‫You can't have half a piece of ammunition, so it makes sense for it to be an integer.
‫Now how would we go about creating a variable in blueprint?
‫So let's head over to our Blueprint panel and close down the details pane.
‫For some reason I have that open.
‫What we need is a different pane.
‫I'm going to go to the window menu and we're going to click on the My Blueprint element there.
‫And this is going to give us a new pane that allows us to add things to our blueprint that we couldn't
‫do before.
‫Here you can see there is a variables section and a little plus button.
‫So let's click the plus button, see what we get.
‫Okay, so we're starting to see what I told you.
‫We've got a name that we have to enter for our variable.
‫We're going to call it ammo.
‫And then next to that, we have a type.
‫You can see it's defaulted to it being of boolean type, but we can click a dropdown and we can see
‫all of the types available to us.
‫There's quite a few in our case.
‫We're just going to go for a straightforward, simple integer, like, so now what can we do?
‫How can we manipulate the value in this variable?
‫Well, for a start, it has a default value.
‫If you go over to the details pane which will close, but it has popped back up here.
‫You can see that it says please compile the blueprint to be able to set the default value.
‫So to compile the blueprints, we go to the toolbar and it's the leftmost button there is called compile.
‫It's got a little question mark to it.
‫I click compile and then it goes into a green arrow and now you can see that the default value can be
‫set.
‫So if I wanted to say have 20 ammo to start the game with, I can just type the value 20 in here and
‫that's going to be the value when we start playing.
‫But what if we want to update this as indeed we do want to update our ammunition value as we go through
‫playing the game?
‫Well, you can drag a variable from the my blueprint pane into your event graph, and when you do that
‫it gives you two options.
‫You've got the option to get the ammo, in other words, to get the value stored within it, or we've
‫got the value, the option to set the ammo which would allow us to overwrite the value that's currently
‫in there.
‫So let's have a look at what both these nodes look like.
‫And by the way, you can control drag a variable to get it and you can alt drag a variable to set it.
‫Now noteworthy is that the set needs to have some execution pins to tell it when to overwrite because
‫it does make a difference when you overwrite is going to have different effects on your gameplay, whereas
‫doesn't matter when you get a variable so it doesn't have any execution pins there.
‫And then you notice the key difference is that the getter only has an output data pin and the setter
‫has an input one so that you could set a different value.
‫And let me just show you that we can get the value of our ammo in Begin Play.
‫So at the end when we've done Welcome to Warehouse Wreckage, Get Ready to Rumble, we can put in a
‫new print for the ammo here.
‫So I'm going to drag off and create a new print string node.
‫And in here I'm going to control, click my variable and drag it into the scene and then connect up
‫my ammo to the print string.
‫It automatically adds in a little conversion node so that we can convert from the integer that's given
‫into the string that's required and we can hit play.
‫And sure enough, you can see the value 20 gets printed out there, meaning that our current ammo is
‫what we set it to.
‫Now I just want to show you one more piece of the puzzle here which would allow us to subtract ammo.
‫What we're going to have to do is after we have spawned our projectile and given an impulse, we're
‫going to want to, first of all, get the current ammo going, to want to subtract one from that ammo
‫and then set it back.
‫So there's three steps to it, but the step that we're missing is how do we subtract?
‫Well, if you right click anywhere in the event graph, you can.
‫Just search for subtract and it gives you this little deep default subtraction node which you can plug
‫anything into.
‫So if you plug in an integer, then it will expect an integer in the second pin as well.
‫So let me challenge you to subtract one from the value we've currently got in our variables.
‫So get the ammo value from the variable by using the control and drag to get a get node, then subtract
‫one from it using the subtract node, remembering to put one, put the getter into the input pin and
‫then just type in one into the other pin.
‫Then store it back in the ammo variable by dragging in a set node by doing an alt click and dragging
‫in the set node and finally print the result afterwards using a print string node and make sure that
‫all of your execution pins are connected up in the order we've just described.
‫Pause the video and have a go.
‫Okay.
‫Welcome back.
‫So first things first, we're going to control click in to get the ammo and we're going to plug that
‫into the first pin of our Subtract node.
‫And you notice that then the second pin becomes green as well and allows us to enter a value.
‫I'm going to enter the value of one.
‫So I've got the ammo value, subtracting one from it.
‫So the output is going to be the current ammo minus one.
‫So initially it will be 20, then it will be 18, etc. etc. Then with their output value, I want to
‫set my ammo so I'm going to click the ammo and drag it in and then I'm going to connect up the output
‫pin from the subtraction to the input pin of the set.
‫And then I need to connect up the execution pins so that the set happens after add impulse.
‫And then I can take the output value from set if I want to or the output value from the subtraction.
‫Either one of these will be the same and I can feed that in to a print string node or I can't do it
‫directly into a print string node because it doesn't know that I can do the conversion.
‫So I need to pull in a print string node, then connect up the output pin from set to the input pin
‫of that print string and move around by conversion node and connect up the execution pins.
‫Now if I go ahead and hit play, you can see it says My ammo is 20 and if I shoot, my ammo goes down
‫to 19, 18, 17 and so on.
‫I can keep shooting until I get down to zero, but I can keep going.
‫I can go into the negative numbers here because there's nothing in my code that tells me to stop.
‫Well, we'll figure that one out in the next lecture.
‫I'll see you there.