﻿1
00:00:03,990 --> 00:00:09,630
‫So we're going to create a fire function and we're going to associate this function with our left mouse

2
00:00:09,630 --> 00:00:10,110
‫button.

3
00:00:10,110 --> 00:00:16,350
‫And here you can see that I'm drawing debug spheres at the tip of our turret barrel just to prove that

4
00:00:16,350 --> 00:00:18,210
‫this action mapping is working.

5
00:00:18,210 --> 00:00:21,360
‫Whenever I click the left mouse button, something is happening.

6
00:00:21,720 --> 00:00:23,090
‫Hey, welcome back.

7
00:00:23,100 --> 00:00:28,770
‫In this video, we're going to talk about creating a fire function or a function we can use for when

8
00:00:28,770 --> 00:00:31,020
‫we want to spawn our projectiles.

9
00:00:31,020 --> 00:00:36,090
‫So we're going to create a fire function and we're going to bind it to an action mapping.

10
00:00:36,090 --> 00:00:38,370
‫Now, we've been using axis mappings.

11
00:00:38,370 --> 00:00:40,260
‫An action mapping is similar.

12
00:00:40,260 --> 00:00:41,550
‫Let's talk about it now.

13
00:00:41,550 --> 00:00:47,280
‫Here's a section of a screenshot of the fire action mapping that we have in our project settings.

14
00:00:47,280 --> 00:00:51,690
‫You'll see that we have the left mouse button assigned to the fire action mapping.

15
00:00:51,690 --> 00:00:55,290
‫Now an action mapping is very much like an axis mapping.

16
00:00:55,290 --> 00:00:57,720
‫You can assign a key or a button to it.

17
00:00:57,720 --> 00:01:03,420
‫And just like an axis mapping, it requires a function that can be bound to the action mapping.

18
00:01:03,420 --> 00:01:07,440
‫Now, axis mappings fire off every single frame of the game.

19
00:01:07,440 --> 00:01:12,840
‫They're like the tic function and the float value passed in changes depending on which buttons are being

20
00:01:12,840 --> 00:01:13,590
‫pressed.

21
00:01:13,590 --> 00:01:15,930
‫Action mappings are slightly different.

22
00:01:15,960 --> 00:01:22,740
‫A callback function bound to an action mapping takes no input parameters, and all that happens is when

23
00:01:22,740 --> 00:01:25,200
‫you press the button, the function gets called.

24
00:01:25,200 --> 00:01:29,430
‫What goes on inside of the function is up to us as the developer.

25
00:01:29,430 --> 00:01:31,590
‫Now, action mappings are one offs.

26
00:01:31,590 --> 00:01:33,030
‫They don't happen every frame.

27
00:01:33,030 --> 00:01:35,790
‫They only happen once when you press the button.

28
00:01:35,790 --> 00:01:40,950
‫So if you release the button and press it again, the function will be called once more.

29
00:01:40,980 --> 00:01:47,280
‫Now, when we bound functions to the axis mappings, we used bind axis for action mappings we use a

30
00:01:47,280 --> 00:01:52,650
‫function called bind action and it takes inputs similar to bind axis.

31
00:01:52,650 --> 00:01:56,400
‫It takes the name of the action mapping as a string literal.

32
00:01:56,430 --> 00:01:58,710
‫It also requires a pointer to the class.

33
00:01:58,710 --> 00:02:04,470
‫We're binding the function on for which we can use this, and it takes, of course, a callback function.

34
00:02:04,470 --> 00:02:11,580
‫Now an additional parameter it takes that bind axis does not require as an enum constant specifying

35
00:02:11,580 --> 00:02:17,130
‫whether or not we'd like this function to be called when we press the button or when we release the

36
00:02:17,130 --> 00:02:17,640
‫button.

37
00:02:17,640 --> 00:02:24,000
‫So there are two options for this input parameter the enum constant, i.e. pressed and the enum constant,

38
00:02:24,000 --> 00:02:25,350
‫i.e. released.

39
00:02:25,350 --> 00:02:31,110
‫This means we can call bind action passing in i.e. pressed if we'd like to bind a callback function

40
00:02:31,110 --> 00:02:36,960
‫for when we press the button and instead if we used i.e. released then our callback function would be

41
00:02:36,960 --> 00:02:40,380
‫called when the button was released for our fire function.

42
00:02:40,380 --> 00:02:42,900
‫We're concerned with when we press the button.

43
00:02:42,900 --> 00:02:46,890
‫So let's make a fire function and bind it to the fire action mapping.

44
00:02:46,890 --> 00:02:53,490
‫Now I'm here in base spawn and the reason I'm here in base pond is because this fire function is something

45
00:02:53,490 --> 00:02:57,270
‫that we can use in the tank class and in the tower class.

46
00:02:57,270 --> 00:03:03,030
‫So if we place it here in base pond, then both Tank and tower will inherit this function.

47
00:03:03,030 --> 00:03:08,850
‫And since both the tank and the tower are going to fire projectiles, then they can simply inherit the

48
00:03:08,850 --> 00:03:10,230
‫function and use it.

49
00:03:10,260 --> 00:03:16,530
‫Now, since I'd like access to the function in both the child classes, I can place this function here

50
00:03:16,530 --> 00:03:19,500
‫in the protected section, just like rotate turret.

51
00:03:19,500 --> 00:03:26,220
‫So under rotate turret I'm going to create a void function called fire that takes no inputs and we can

52
00:03:26,220 --> 00:03:30,780
‫go ahead and define the fire function in based upon CPP.

53
00:03:30,780 --> 00:03:36,990
‫So here's based upon CPP and just under rotate turret, I'm going to place the fire function and we

54
00:03:36,990 --> 00:03:41,430
‫need to fully qualify this function name and give it a function body.

55
00:03:41,430 --> 00:03:43,680
‫So now we have a fire function.

56
00:03:43,680 --> 00:03:47,040
‫Now the next step is to bind this to an action mapping.

57
00:03:47,040 --> 00:03:53,190
‫Now we only want to bind this function in the tank class because the tank is the only class that will

58
00:03:53,190 --> 00:03:54,720
‫be taking user input.

59
00:03:54,720 --> 00:03:59,610
‫We'll be calling the fire function in the tower class later, but that will be as a result of code we

60
00:03:59,610 --> 00:04:02,820
‫create and not as a result of user input.

61
00:04:02,820 --> 00:04:08,730
‫So we're here in Tank CP and we're going to bind this and set up player input component just like we

62
00:04:08,730 --> 00:04:10,290
‫used bind axis.

63
00:04:10,290 --> 00:04:12,900
‫So we're going to use the player input component.

64
00:04:12,900 --> 00:04:18,720
‫So player input component and the function is going to be bind action.

65
00:04:18,720 --> 00:04:24,150
‫Now the first input is going to be the string literal, which we're going to wrap in a text macro as

66
00:04:24,150 --> 00:04:24,780
‫usual.

67
00:04:24,780 --> 00:04:27,810
‫And this action mapping is called fire.

68
00:04:27,960 --> 00:04:34,080
‫Now the next parameter is going to be that enum constant that determines whether or not we'd like this

69
00:04:34,080 --> 00:04:39,000
‫function to be called when we press the button versus when we release the button.

70
00:04:39,000 --> 00:04:42,150
‫And we're going to use, I underscore pressed.

71
00:04:43,170 --> 00:04:46,260
‫Since we'd like our function called when we press the button.

72
00:04:46,260 --> 00:04:49,520
‫Now, next is the user class object that's going to be this.

73
00:04:49,530 --> 00:04:55,620
‫And finally we have our callback function so we can say a tank fire.

74
00:04:55,770 --> 00:05:00,690
‫And now our fire function is bound to the fire action mapping.

75
00:05:00,690 --> 00:05:07,200
‫So we know that our fire function is bound to the fire action mapping, but we'd like some visual confirmation

76
00:05:07,200 --> 00:05:09,900
‫and that's what your challenge is going to be.

77
00:05:09,900 --> 00:05:14,130
‫So in the fire function, I'd like you to draw a debug sphere.

78
00:05:14,130 --> 00:05:19,440
‫We've already seen how to do this, so go ahead and fill in the parameters with values that you think

79
00:05:19,440 --> 00:05:20,640
‫are appropriate.

80
00:05:20,670 --> 00:05:24,030
‫Use the location of the projectile spawn point.

81
00:05:24,270 --> 00:05:30,210
‫Now the projectile spawn point is a use scene component and you've seen components have a function called

82
00:05:30,210 --> 00:05:32,100
‫get component location.

83
00:05:32,100 --> 00:05:33,780
‫This returns an F vector.

84
00:05:33,810 --> 00:05:38,220
‫This is what you can use to get the projectile spawn point location.

85
00:05:38,220 --> 00:05:45,630
‫Now when you call draw debug sphere, use a duration of about 3 seconds so we can see that sphere for

86
00:05:45,630 --> 00:05:47,760
‫a little while before it disappears.

87
00:05:47,760 --> 00:05:50,460
‫Pause the video and do this now.

88
00:05:53,360 --> 00:05:53,550
‫Okay.

89
00:05:53,660 --> 00:05:56,810
‫So I'm back here in Bass Pond and here in Bass Pond.

90
00:05:57,110 --> 00:06:00,770
‫CP I'm going to include draw debug helpers.

91
00:06:00,770 --> 00:06:03,500
‫So we have access to draw debug sphere.

92
00:06:03,500 --> 00:06:07,010
‫So I'm going to say include draw debug helpers.

93
00:06:08,240 --> 00:06:12,320
‫And now down here in the fire function, we can draw the debug sphere.

94
00:06:12,350 --> 00:06:18,050
‫Now, the first thing I want to do though is get the location of the projectile spawn point.

95
00:06:18,080 --> 00:06:26,240
‫Now I can create a local f vector for this and I can call this projectile spawn point location and I

96
00:06:26,240 --> 00:06:29,870
‫can initialize it using the projectile spawn point.

97
00:06:32,030 --> 00:06:35,780
‫And calling get component location.

98
00:06:35,780 --> 00:06:43,100
‫So now I have the location of the projectile spawn point and this is what I can use when calling draw

99
00:06:43,100 --> 00:06:43,850
‫debug sphere.

100
00:06:43,850 --> 00:06:46,130
‫So I'm going to call draw debug sphere.

101
00:06:47,450 --> 00:06:53,720
‫And for the first input we need the world and we know that we can get the world using get world next.

102
00:06:53,720 --> 00:06:59,810
‫We need the center of the sphere that's going to be our projectile spawn point location.

103
00:06:59,810 --> 00:07:04,190
‫So that will be what I pass in as the second parameter.

104
00:07:04,190 --> 00:07:06,190
‫And next we need the radius of the sphere.

105
00:07:06,200 --> 00:07:08,330
‫Now I'm going to have a small sphere.

106
00:07:08,480 --> 00:07:15,920
‫I'll give it a radius of say 25 F for the number of segments I'm just going to use 12 and for the color

107
00:07:15,920 --> 00:07:17,690
‫I can use F color.

108
00:07:18,680 --> 00:07:21,380
‫Read Now four draw persistent lines.

109
00:07:21,380 --> 00:07:27,500
‫We're going to have our lines hang around for 3 seconds so we can make this false because we don't want

110
00:07:27,500 --> 00:07:28,910
‫them to persist forever.

111
00:07:28,940 --> 00:07:36,380
‫We're going to give this debug sphere a lifetime of three F So let's go ahead and compile this now and

112
00:07:36,380 --> 00:07:42,230
‫I'm going to hit play and click and now we can see our debug sphere and of course it goes away after

113
00:07:42,230 --> 00:07:43,310
‫3 seconds.

114
00:07:43,640 --> 00:07:49,880
‫Now this is good confirmation that our action mapping is working and it's also giving us verification

115
00:07:49,880 --> 00:07:53,450
‫of the location of the projectile spawn point.

116
00:07:53,450 --> 00:07:59,600
‫So once we actually start spawning projectiles, we know that they will spawn at the correct location

117
00:07:59,600 --> 00:08:02,420
‫as indicated by the debug sphere.

118
00:08:02,420 --> 00:08:03,500
‫So this is great.

119
00:08:03,650 --> 00:08:09,470
‫So in this video we created a fire function and we bound it to the fire action mapping.

120
00:08:09,740 --> 00:08:15,800
‫And to prove that it was working, we drew a debug sphere at the projectile spawn point whenever we

121
00:08:15,830 --> 00:08:21,710
‫click the left mouse button so we have a fire function, we're almost ready to start spawning projectiles.

122
00:08:21,710 --> 00:08:25,250
‫And we've also placed this function in the base pong class.

123
00:08:25,250 --> 00:08:27,980
‫So that way our tower can also use it.

124
00:08:27,980 --> 00:08:32,990
‫So in the next video, we're going to discuss how our tower is going to use this function.

125
00:08:33,530 --> 00:08:34,460
‫I'll see you soon.

