﻿1
00:00:04,490 --> 00:00:05,540
‫Hello and welcome.

2
00:00:05,540 --> 00:00:09,260
‫In this lecture, we're going to be using variables to keep track of how much ammo we've got.

3
00:00:09,260 --> 00:00:15,140
‫If you look in the top left corner, you can see that it's telling me the ammo value and each time I

4
00:00:15,140 --> 00:00:18,350
‫fire it is decreasing that value.

5
00:00:18,350 --> 00:00:21,860
‫Let's dive in and find out how variables help us do this.

6
00:00:22,460 --> 00:00:27,380
‫So we're going to need variables in order to keep track of our ammo.

7
00:00:27,380 --> 00:00:28,700
‫So what our variables?

8
00:00:28,700 --> 00:00:33,440
‫Variables help us store, manipulate and refer back to information.

9
00:00:33,440 --> 00:00:37,640
‫And most crucially for us, it's going to be that storage of the information.

10
00:00:37,640 --> 00:00:41,870
‫So it's a bit like a box that we label as ammo.

11
00:00:41,870 --> 00:00:47,540
‫And inside it we can put a value such as 20, which tells us how much ammo we've got left.

12
00:00:47,540 --> 00:00:49,820
‫So each variable has to have a name.

13
00:00:49,820 --> 00:00:56,420
‫It contains some data such as the data value 20 and crucially, each variable is of a particular type.

14
00:00:56,420 --> 00:01:03,020
‫So in this case, the ammo box could be said to be an integer because it contains whole numbers.

15
00:01:03,020 --> 00:01:07,880
‫You can't have half a piece of ammunition, so it makes sense for it to be an integer.

16
00:01:08,030 --> 00:01:13,010
‫Now how would we go about creating a variable in blueprint?

17
00:01:13,010 --> 00:01:17,990
‫So let's head over to our Blueprint panel and close down the details pane.

18
00:01:17,990 --> 00:01:19,550
‫For some reason I have that open.

19
00:01:19,550 --> 00:01:21,500
‫What we need is a different pane.

20
00:01:21,500 --> 00:01:27,680
‫I'm going to go to the window menu and we're going to click on the My Blueprint element there.

21
00:01:27,680 --> 00:01:33,230
‫And this is going to give us a new pane that allows us to add things to our blueprint that we couldn't

22
00:01:33,230 --> 00:01:34,220
‫do before.

23
00:01:34,610 --> 00:01:38,120
‫Here you can see there is a variables section and a little plus button.

24
00:01:38,120 --> 00:01:40,160
‫So let's click the plus button, see what we get.

25
00:01:40,190 --> 00:01:42,530
‫Okay, so we're starting to see what I told you.

26
00:01:42,560 --> 00:01:44,870
‫We've got a name that we have to enter for our variable.

27
00:01:44,870 --> 00:01:46,220
‫We're going to call it ammo.

28
00:01:46,220 --> 00:01:48,710
‫And then next to that, we have a type.

29
00:01:48,710 --> 00:01:54,200
‫You can see it's defaulted to it being of boolean type, but we can click a dropdown and we can see

30
00:01:54,200 --> 00:01:55,910
‫all of the types available to us.

31
00:01:55,910 --> 00:01:57,890
‫There's quite a few in our case.

32
00:01:57,890 --> 00:02:03,170
‫We're just going to go for a straightforward, simple integer, like, so now what can we do?

33
00:02:03,200 --> 00:02:06,800
‫How can we manipulate the value in this variable?

34
00:02:06,800 --> 00:02:09,590
‫Well, for a start, it has a default value.

35
00:02:09,590 --> 00:02:13,790
‫If you go over to the details pane which will close, but it has popped back up here.

36
00:02:13,910 --> 00:02:18,170
‫You can see that it says please compile the blueprint to be able to set the default value.

37
00:02:18,170 --> 00:02:24,320
‫So to compile the blueprints, we go to the toolbar and it's the leftmost button there is called compile.

38
00:02:24,320 --> 00:02:25,610
‫It's got a little question mark to it.

39
00:02:25,610 --> 00:02:30,920
‫I click compile and then it goes into a green arrow and now you can see that the default value can be

40
00:02:30,920 --> 00:02:31,250
‫set.

41
00:02:31,250 --> 00:02:37,940
‫So if I wanted to say have 20 ammo to start the game with, I can just type the value 20 in here and

42
00:02:37,940 --> 00:02:40,610
‫that's going to be the value when we start playing.

43
00:02:40,610 --> 00:02:47,690
‫But what if we want to update this as indeed we do want to update our ammunition value as we go through

44
00:02:47,690 --> 00:02:48,650
‫playing the game?

45
00:02:48,770 --> 00:02:55,490
‫Well, you can drag a variable from the my blueprint pane into your event graph, and when you do that

46
00:02:55,490 --> 00:02:56,990
‫it gives you two options.

47
00:02:56,990 --> 00:03:04,070
‫You've got the option to get the ammo, in other words, to get the value stored within it, or we've

48
00:03:04,070 --> 00:03:11,120
‫got the value, the option to set the ammo which would allow us to overwrite the value that's currently

49
00:03:11,120 --> 00:03:11,720
‫in there.

50
00:03:11,720 --> 00:03:14,000
‫So let's have a look at what both these nodes look like.

51
00:03:14,000 --> 00:03:20,990
‫And by the way, you can control drag a variable to get it and you can alt drag a variable to set it.

52
00:03:20,990 --> 00:03:28,430
‫Now noteworthy is that the set needs to have some execution pins to tell it when to overwrite because

53
00:03:28,430 --> 00:03:34,380
‫it does make a difference when you overwrite is going to have different effects on your gameplay, whereas

54
00:03:34,400 --> 00:03:39,410
‫doesn't matter when you get a variable so it doesn't have any execution pins there.

55
00:03:39,410 --> 00:03:45,290
‫And then you notice the key difference is that the getter only has an output data pin and the setter

56
00:03:45,290 --> 00:03:48,860
‫has an input one so that you could set a different value.

57
00:03:48,860 --> 00:03:56,120
‫And let me just show you that we can get the value of our ammo in Begin Play.

58
00:03:56,120 --> 00:04:01,970
‫So at the end when we've done Welcome to Warehouse Wreckage, Get Ready to Rumble, we can put in a

59
00:04:01,970 --> 00:04:04,460
‫new print for the ammo here.

60
00:04:04,610 --> 00:04:09,500
‫So I'm going to drag off and create a new print string node.

61
00:04:09,500 --> 00:04:17,300
‫And in here I'm going to control, click my variable and drag it into the scene and then connect up

62
00:04:17,300 --> 00:04:19,010
‫my ammo to the print string.

63
00:04:19,010 --> 00:04:24,260
‫It automatically adds in a little conversion node so that we can convert from the integer that's given

64
00:04:24,260 --> 00:04:27,260
‫into the string that's required and we can hit play.

65
00:04:27,260 --> 00:04:33,830
‫And sure enough, you can see the value 20 gets printed out there, meaning that our current ammo is

66
00:04:33,830 --> 00:04:34,970
‫what we set it to.

67
00:04:35,090 --> 00:04:41,600
‫Now I just want to show you one more piece of the puzzle here which would allow us to subtract ammo.

68
00:04:41,600 --> 00:04:47,060
‫What we're going to have to do is after we have spawned our projectile and given an impulse, we're

69
00:04:47,060 --> 00:04:54,110
‫going to want to, first of all, get the current ammo going, to want to subtract one from that ammo

70
00:04:54,110 --> 00:04:55,610
‫and then set it back.

71
00:04:55,610 --> 00:05:00,260
‫So there's three steps to it, but the step that we're missing is how do we subtract?

72
00:05:00,260 --> 00:05:04,010
‫Well, if you right click anywhere in the event graph, you can.

73
00:05:04,090 --> 00:05:11,500
‫Just search for subtract and it gives you this little deep default subtraction node which you can plug

74
00:05:11,500 --> 00:05:12,200
‫anything into.

75
00:05:12,220 --> 00:05:16,660
‫So if you plug in an integer, then it will expect an integer in the second pin as well.

76
00:05:16,660 --> 00:05:21,760
‫So let me challenge you to subtract one from the value we've currently got in our variables.

77
00:05:21,760 --> 00:05:29,830
‫So get the ammo value from the variable by using the control and drag to get a get node, then subtract

78
00:05:29,830 --> 00:05:36,580
‫one from it using the subtract node, remembering to put one, put the getter into the input pin and

79
00:05:36,580 --> 00:05:39,100
‫then just type in one into the other pin.

80
00:05:39,100 --> 00:05:46,420
‫Then store it back in the ammo variable by dragging in a set node by doing an alt click and dragging

81
00:05:46,420 --> 00:05:52,600
‫in the set node and finally print the result afterwards using a print string node and make sure that

82
00:05:52,600 --> 00:05:57,750
‫all of your execution pins are connected up in the order we've just described.

83
00:05:57,760 --> 00:05:59,380
‫Pause the video and have a go.

84
00:06:02,540 --> 00:06:03,110
‫Okay.

85
00:06:03,110 --> 00:06:04,070
‫Welcome back.

86
00:06:04,100 --> 00:06:11,690
‫So first things first, we're going to control click in to get the ammo and we're going to plug that

87
00:06:11,690 --> 00:06:14,420
‫into the first pin of our Subtract node.

88
00:06:14,420 --> 00:06:18,740
‫And you notice that then the second pin becomes green as well and allows us to enter a value.

89
00:06:18,740 --> 00:06:20,240
‫I'm going to enter the value of one.

90
00:06:20,240 --> 00:06:23,300
‫So I've got the ammo value, subtracting one from it.

91
00:06:23,300 --> 00:06:26,870
‫So the output is going to be the current ammo minus one.

92
00:06:26,870 --> 00:06:33,230
‫So initially it will be 20, then it will be 18, etc. etc. Then with their output value, I want to

93
00:06:33,230 --> 00:06:40,850
‫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

94
00:06:40,850 --> 00:06:43,490
‫pin from the subtraction to the input pin of the set.

95
00:06:43,490 --> 00:06:50,210
‫And then I need to connect up the execution pins so that the set happens after add impulse.

96
00:06:50,210 --> 00:06:57,080
‫And then I can take the output value from set if I want to or the output value from the subtraction.

97
00:06:57,080 --> 00:07:04,430
‫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

98
00:07:04,430 --> 00:07:08,780
‫directly into a print string node because it doesn't know that I can do the conversion.

99
00:07:08,780 --> 00:07:15,260
‫So I need to pull in a print string node, then connect up the output pin from set to the input pin

100
00:07:15,260 --> 00:07:20,990
‫of that print string and move around by conversion node and connect up the execution pins.

101
00:07:20,990 --> 00:07:27,170
‫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

102
00:07:27,170 --> 00:07:31,070
‫to 19, 18, 17 and so on.

103
00:07:31,070 --> 00:07:36,410
‫I can keep shooting until I get down to zero, but I can keep going.

104
00:07:36,410 --> 00:07:42,350
‫I can go into the negative numbers here because there's nothing in my code that tells me to stop.

105
00:07:42,350 --> 00:07:45,260
‫Well, we'll figure that one out in the next lecture.

106
00:07:45,260 --> 00:07:46,070
‫I'll see you there.

