﻿1
00:00:04,320 --> 00:00:05,040
‫Hello and welcome.

2
00:00:05,040 --> 00:00:11,940
‫The subject of this lecture is going to be the take damage function of the actor and how we call it.

3
00:00:11,940 --> 00:00:14,700
‫Let's dive in and find out all about it.

4
00:00:15,770 --> 00:00:21,110
‫So in order for us to be able to deal damage to something, we need to actually have something which

5
00:00:21,110 --> 00:00:22,250
‫we can hit.

6
00:00:22,370 --> 00:00:29,480
‫So let's go to our content browser route and bring in one of the BP shooter characters.

7
00:00:29,810 --> 00:00:36,170
‫Because actually what happens is if you bring in a character into the scene, then automatically Unreal

8
00:00:36,170 --> 00:00:42,530
‫is going to make this character AI controlled and the ones that spawn at the player starts will be player

9
00:00:42,530 --> 00:00:43,190
‫controlled.

10
00:00:43,190 --> 00:00:44,660
‫So that's great.

11
00:00:44,660 --> 00:00:45,890
‫That's exactly the behavior we want.

12
00:00:45,890 --> 00:00:51,200
‫If I go ahead and hit alt p, you'll notice I'm actually in the wrong spawn point, so maybe I want

13
00:00:51,200 --> 00:00:54,080
‫to delete this second player start.

14
00:00:54,080 --> 00:00:57,110
‫If I hit alt p now we'll be in the right location.

15
00:00:57,110 --> 00:00:58,610
‫There is my enemy.

16
00:00:58,610 --> 00:01:00,050
‫I can shoot at the enemy.

17
00:01:00,050 --> 00:01:01,190
‫I get the hit effect.

18
00:01:01,190 --> 00:01:03,140
‫But obviously I'm not doing any damage.

19
00:01:03,140 --> 00:01:04,250
‫Not yet anyway.

20
00:01:04,700 --> 00:01:07,400
‫And doing damage is a two part process.

21
00:01:07,400 --> 00:01:12,650
‫The first part is sending the damage and the second part is receiving it.

22
00:01:12,650 --> 00:01:18,440
‫In this lecture, we're going to deal with sending damage to the player and the function calls involved

23
00:01:18,440 --> 00:01:19,400
‫with that.

24
00:01:19,820 --> 00:01:28,100
‫So the main one that we're going to be looking at is in the actor Dot H and we're looking for a function

25
00:01:28,100 --> 00:01:30,890
‫call called take damage.

26
00:01:31,340 --> 00:01:34,370
‫And you can see here there is this virtual function here.

27
00:01:34,370 --> 00:01:35,960
‫It returns a float.

28
00:01:35,960 --> 00:01:41,870
‫What it's returning is the actual damage applied to the actor, the actual amount that really got applied.

29
00:01:42,500 --> 00:01:50,810
‫And what goes in is the damage amount, a damage event, which gives us more information about that.

30
00:01:50,810 --> 00:01:54,590
‫The damage that happened, we have an instigator.

31
00:01:54,590 --> 00:01:58,730
‫So that's saying who was the controller responsible for causing this damage?

32
00:01:59,000 --> 00:02:03,770
‫And then the damage causer, which is what was the actor responsible?

33
00:02:03,770 --> 00:02:06,380
‫So in this case, it might be a gun.

34
00:02:06,380 --> 00:02:11,810
‫Or is it saying here the projectile that exploded, the rock that landed on you in this case, the gun

35
00:02:11,810 --> 00:02:15,830
‫that shot you, is going to be the actor that was responsible.

36
00:02:15,920 --> 00:02:18,920
‫So that's take damage in a nutshell.

37
00:02:18,950 --> 00:02:24,230
‫Most of that should be pretty straightforward for you to put together with the exception of this damage

38
00:02:24,230 --> 00:02:25,250
‫event struct.

39
00:02:25,250 --> 00:02:26,510
‫And that's a little bit more complicated.

40
00:02:26,510 --> 00:02:32,120
‫So let's have a look at this damage event structure and see what can what can we do?

41
00:02:32,150 --> 00:02:34,160
‫How is it going to be composed?

42
00:02:34,160 --> 00:02:36,020
‫How are we going to put this together?

43
00:02:36,020 --> 00:02:42,350
‫Now, it's not immediately obvious, but there are actually two subtypes for this damage event.

44
00:02:42,350 --> 00:02:47,360
‫In practice, you're not going to actually create an EF damage event, but rather you'd be creating

45
00:02:47,360 --> 00:02:54,410
‫an EF point damage event, or you would be creating an EF radial damage event.

46
00:02:54,410 --> 00:02:58,310
‫Those are the two main types of damage that exist in Unreal.

47
00:02:58,310 --> 00:03:03,500
‫You can create more different types of damage for your own custom stuff, like you might have a fire

48
00:03:03,500 --> 00:03:11,300
‫damage and with different parameters, but in that case, we're interested in the EF radial radial and

49
00:03:11,300 --> 00:03:11,990
‫point damage.

50
00:03:11,990 --> 00:03:18,380
‫So radial again would be something like a grenade going off, in which case you want to say, okay,

51
00:03:18,380 --> 00:03:21,020
‫where is what's the radius, what's the fall off?

52
00:03:21,020 --> 00:03:27,470
‫All of those kinds of things where as the point damage is, as it sounds much more appropriate for things

53
00:03:27,470 --> 00:03:28,310
‫like guns.

54
00:03:28,400 --> 00:03:35,930
‫So we're going to create an EF point damage event and we're going to go and look for the constructor,

55
00:03:35,930 --> 00:03:40,280
‫here's the constructor for it, and we can see what it's taking in.

56
00:03:40,310 --> 00:03:41,720
‫It's taking the damage amount again.

57
00:03:41,720 --> 00:03:43,400
‫So that's a bit of repetition there.

58
00:03:43,610 --> 00:03:48,620
‫We're having the hit result that caused this point damage to take place.

59
00:03:48,860 --> 00:03:51,380
‫Then we're having the in shot direction.

60
00:03:51,380 --> 00:03:51,680
‫Great.

61
00:03:51,680 --> 00:03:52,880
‫We've already calculated that.

62
00:03:52,880 --> 00:03:58,220
‫I was thinking a little bit ahead when we did that in the previous lecture and then this subclass of

63
00:03:58,220 --> 00:03:59,540
‫damage type.

64
00:03:59,540 --> 00:04:06,650
‫Now this can be used to pass more information so that actually, rather than creating a different damage

65
00:04:06,650 --> 00:04:09,500
‫event type, you'd probably create a different you damage type.

66
00:04:09,500 --> 00:04:15,620
‫So you might have a fire type or something like that in that case, because we don't need any kind of

67
00:04:15,620 --> 00:04:16,220
‫damage type.

68
00:04:16,220 --> 00:04:21,080
‫We're not doing fire damage or anything extra like that poison damage.

69
00:04:21,080 --> 00:04:25,310
‫Then what we're going to do is just pass in a null pointer here instead.

70
00:04:25,310 --> 00:04:31,700
‫So let's have a go at constructing one of these together and then I will let you call that take damage

71
00:04:31,700 --> 00:04:32,330
‫event.

72
00:04:32,990 --> 00:04:39,680
‫So the first thing to do is to paste in the type, we're going to call it the damage event and then

73
00:04:39,680 --> 00:04:41,510
‫we're going to construct it.

74
00:04:41,510 --> 00:04:45,860
‫So first of all, the damage amount, well, we don't have one of these for the gun yet.

75
00:04:45,860 --> 00:04:48,140
‫We don't have how much damage it does.

76
00:04:48,140 --> 00:04:51,050
‫So we can go ahead and create some information there.

77
00:04:51,050 --> 00:04:52,730
‫We've got a max range already.

78
00:04:52,730 --> 00:04:54,740
‫We want some more properties in here.

79
00:04:54,770 --> 00:05:00,440
‫It's going to be an edit anywhere, which is going to allow us to say float damage.

80
00:05:00,440 --> 00:05:01,400
‫How much damage do we do?

81
00:05:01,400 --> 00:05:05,750
‫I'm going to do a damage of ten and then we can use this over in the C++.

82
00:05:05,750 --> 00:05:09,320
‫So damage is what we're going to pass in as the first argument.

83
00:05:09,440 --> 00:05:11,510
‫The second one is that hit result?

84
00:05:11,510 --> 00:05:13,670
‫Well, fortunately, we've got the hit results called hit.

85
00:05:14,470 --> 00:05:22,450
‫Then the next thing is going to be the shot direction which we already calculated for our emitter at

86
00:05:22,450 --> 00:05:23,290
‫location.

87
00:05:24,060 --> 00:05:29,730
‫Then we're going to look at the subclass, which is just going to be a null pointer.

88
00:05:30,230 --> 00:05:31,800
‫Okay, so that is done.

89
00:05:31,800 --> 00:05:36,060
‫The next thing would just be to get hold of the actor that we're trying to damage.

90
00:05:36,060 --> 00:05:38,620
‫That's fairly straightforward from the hit you do.

91
00:05:38,670 --> 00:05:41,610
‫Hit dot get actor on here.

92
00:05:42,090 --> 00:05:48,630
‫But you will need to check that this isn't null because we don't want to end up with a null actor that

93
00:05:48,630 --> 00:05:52,620
‫we're trying to do damage on or we're going to end up crashing.

94
00:05:53,100 --> 00:05:58,800
‫So your challenge is going to be to call that take damage function check for the actor being null.

95
00:05:58,830 --> 00:06:04,690
‫First of all, we don't want a null actor and call take damage and see if you can get it to compile.

96
00:06:04,710 --> 00:06:06,360
‫Pause the video and have a go.

97
00:06:08,480 --> 00:06:08,720
‫Okay.

98
00:06:08,720 --> 00:06:09,470
‫Welcome back.

99
00:06:09,620 --> 00:06:12,260
‫So we've got our guest actor in here.

100
00:06:12,300 --> 00:06:15,710
‫I'm just going to call this I'm going to give this a little variable.

101
00:06:15,710 --> 00:06:23,960
‫So a actor pointer is going to be the hit actor and we're then going to go ahead and do an if statement

102
00:06:23,960 --> 00:06:31,790
‫in here, basically saying if the hit actor is not equal to a null pointer, then we're going to go

103
00:06:31,790 --> 00:06:37,970
‫ahead with this hit actor to call the take damage function on it.

104
00:06:38,390 --> 00:06:43,040
‫And we're going to do the damage amount in here is the first argument.

105
00:06:43,040 --> 00:06:44,630
‫What was the second one?

106
00:06:44,630 --> 00:06:49,250
‫I'm just going to navigate back to the take damage and have a little look.

107
00:06:49,250 --> 00:06:54,650
‫So the next one is the struct that we've put together this damage event.

108
00:06:54,650 --> 00:06:56,660
‫Let's put that in a second.

109
00:06:56,660 --> 00:07:00,800
‫Then we're going to have a look at the controller that created this.

110
00:07:00,800 --> 00:07:02,450
‫Well, that's our controller.

111
00:07:02,450 --> 00:07:05,210
‫The gun's controller is the one doing the damage.

112
00:07:05,210 --> 00:07:11,210
‫And then this is the instigator was the last parameter in our take damage.

113
00:07:11,210 --> 00:07:19,010
‫And we probably only want to construct this struct inside this if actor not equal to null pointer because

114
00:07:19,010 --> 00:07:23,750
‫it's not needed if we don't have an actor to apply this damage to.

115
00:07:24,410 --> 00:07:28,760
‫So let's go over and compile and see if this is going to work.

116
00:07:28,760 --> 00:07:30,290
‫So I got an error in my log.

117
00:07:30,290 --> 00:07:31,880
‫Let's see what's going on.

118
00:07:31,880 --> 00:07:33,680
‫Oh, I'm just missing a semicolon.

119
00:07:33,680 --> 00:07:34,820
‫Well, that's pretty easy.

120
00:07:34,820 --> 00:07:37,040
‫One to fix after the get actor there.

121
00:07:37,040 --> 00:07:38,330
‫Let's go and try again.

122
00:07:38,870 --> 00:07:39,320
‫Cool.

123
00:07:39,320 --> 00:07:46,760
‫Everything has compiled successfully and we can just go ahead and play and just check that shooting

124
00:07:46,760 --> 00:07:52,220
‫is going to not give us any null errors or try and crash the engine.

125
00:07:52,250 --> 00:07:53,900
‫Looks like it's okay.

126
00:07:53,990 --> 00:08:00,770
‫So we'll move on to the next lecture where we're going to try and receive the damage on this actor here

127
00:08:00,770 --> 00:08:02,810
‫that we have placed in the level.

128
00:08:02,810 --> 00:08:03,560
‫So you there.

