﻿1
00:00:04,340 --> 00:00:05,390
‫Hello and welcome.

2
00:00:05,390 --> 00:00:09,320
‫In this lecture, we're going to be dealing damage to this actor here.

3
00:00:09,320 --> 00:00:13,610
‫We're going to do that by overriding a virtual method called take damage on the actor.

4
00:00:13,610 --> 00:00:18,380
‫And you can see in the log here, we've got the health decreasing as I shoot this character all the

5
00:00:18,380 --> 00:00:21,860
‫way down to zero and then it stops at zero, not going into negative numbers.

6
00:00:21,860 --> 00:00:23,780
‫So let's dive in and see how that's done.

7
00:00:26,450 --> 00:00:33,050
‫So while we are currently being able to do damage to the actors that we hit here, such as this guy,

8
00:00:33,380 --> 00:00:36,590
‫we are not actually receiving that damage.

9
00:00:36,590 --> 00:00:43,190
‫So this is where virtual methods of last lecture come in because just like begin, play or tic, we

10
00:00:43,190 --> 00:00:46,080
‫want to be able to receive damage on an actor.

11
00:00:46,100 --> 00:00:50,690
‫Now, fortunately, if you go to the actor H and have a little look in here, you can see that there

12
00:00:50,690 --> 00:00:53,160
‫is this take damage virtual method.

13
00:00:53,180 --> 00:01:00,410
‫Now this can be done also with events and you will possibly have seen it done that way before where

14
00:01:00,410 --> 00:01:01,470
‫you register for events.

15
00:01:01,490 --> 00:01:06,980
‫Now what I'm going to do is just a different way of doing damage, where we actually have damage stored

16
00:01:06,980 --> 00:01:09,680
‫against the actor rather than in a component.

17
00:01:10,160 --> 00:01:15,950
‫And one easy way of receiving that damage is through a virtual method here.

18
00:01:15,950 --> 00:01:18,620
‫And this is the virtual method that we're going to override.

19
00:01:18,620 --> 00:01:25,210
‫So I'm going to go ahead and copy this and go over to our particular actor, which is the shooter character

20
00:01:25,250 --> 00:01:31,340
‫is going to be quicker to go and search for the shooter character dot H And the one thing that I wanted

21
00:01:31,340 --> 00:01:35,480
‫to check is where this comes in, the actor dot H.

22
00:01:35,720 --> 00:01:41,660
‫So if we scroll up, we should be able to see which section we are in here.

23
00:01:42,080 --> 00:01:50,090
‫So I'm just going to scroll right up until I find that what whether we're in public or what they go,

24
00:01:50,090 --> 00:01:51,200
‫we're in the public section.

25
00:01:51,200 --> 00:01:54,530
‫So this ought to be in the public section here as well.

26
00:01:54,530 --> 00:02:00,200
‫So I'm just going to stick it underneath our tick and set up player input component over in the shooter

27
00:02:00,440 --> 00:02:09,650
‫H and we just paste it in and as I said in the last lecture, you can use override to make sure that

28
00:02:09,650 --> 00:02:14,690
‫you're indeed overriding a virtual method from the parent and it's not necessary to have virtual in

29
00:02:14,690 --> 00:02:17,180
‫front here, but we'll keep it anyway.

30
00:02:17,180 --> 00:02:21,560
‫And here you can see all of the data that's coming in to take damage.

31
00:02:21,560 --> 00:02:29,690
‫We have got a damage amount, the damage event and the controller that is causing the damage, the instigator

32
00:02:29,690 --> 00:02:30,530
‫and the damage cause.

33
00:02:30,530 --> 00:02:34,250
‫All that stuff that we'd passed in from the gun is cropping up here as well.

34
00:02:34,250 --> 00:02:35,150
‫So that's great.

35
00:02:35,150 --> 00:02:41,300
‫Now if we go ahead and do a control shift P create implementation or copy and paste, then we can go

36
00:02:41,300 --> 00:02:43,820
‫and make the implementation for take damage.

37
00:02:43,820 --> 00:02:49,250
‫But the first thing to be wary of is that whenever we're overriding something, we want to think about

38
00:02:49,250 --> 00:02:56,660
‫calling the the parent class version of take damage because there may be some implementation in there

39
00:02:56,660 --> 00:02:58,580
‫that needs to run.

40
00:02:58,940 --> 00:03:00,140
‫So that's what we're going to do.

41
00:03:00,170 --> 00:03:01,070
‫We do this always.

42
00:03:01,070 --> 00:03:02,210
‫We've done it with tech.

43
00:03:02,210 --> 00:03:03,710
‫We've done it with Begin Play.

44
00:03:03,710 --> 00:03:07,580
‫You can see we've always got this super cool at the beginning, and that's because when we override

45
00:03:07,580 --> 00:03:09,200
‫a method, we override it completely.

46
00:03:09,200 --> 00:03:11,120
‫We completely remove that behavior.

47
00:03:11,120 --> 00:03:17,000
‫So if we do want that behavior, which we typically do, then we should always start with a cool to

48
00:03:17,000 --> 00:03:17,630
‫super.

49
00:03:17,630 --> 00:03:20,000
‫So we're going to do a cool take damage.

50
00:03:21,450 --> 00:03:23,580
‫That's going to take the damage amount.

51
00:03:23,580 --> 00:03:25,760
‫So this is where it gets a little bit tedious, sadly.

52
00:03:25,800 --> 00:03:27,870
‫My autocomplete tool doesn't do this for us.

53
00:03:27,870 --> 00:03:29,250
‫Damage event.

54
00:03:30,560 --> 00:03:32,660
‫Then what was the next one?

55
00:03:32,660 --> 00:03:34,970
‫We had the control.

56
00:03:35,180 --> 00:03:37,550
‫So the event instigator, let's just copy.

57
00:03:37,820 --> 00:03:42,380
‫And then we've got the damage causer, which is the actor.

58
00:03:43,160 --> 00:03:48,320
‫So all of that gets called up to the parent implementation of take damage.

59
00:03:48,560 --> 00:03:52,220
‫And the one other thing is that this has a return type.

60
00:03:52,220 --> 00:03:55,880
‫So the actual damage applied so we're going to do float.

61
00:03:56,750 --> 00:04:01,460
‫Damage applied is the return value from take damage.

62
00:04:01,460 --> 00:04:05,000
‫So we're just going to keep hold of that damage applied there.

63
00:04:05,270 --> 00:04:11,450
‫And realistically, that's going to be the same as the damage amount in our particular use case, but

64
00:04:11,450 --> 00:04:14,690
‫not necessarily so we're going to use the damage applied.

65
00:04:14,720 --> 00:04:21,000
‫The next thing we want to do is keep hold of the information about how much health we've actually got.

66
00:04:21,020 --> 00:04:24,980
‫Now the way I'm going to do this is with two variables here.

67
00:04:25,010 --> 00:04:30,260
‫The first one is I'm going to make both of them new properties because I want to make the health visible

68
00:04:30,260 --> 00:04:32,090
‫in the inspector, at least for now.

69
00:04:32,420 --> 00:04:37,220
‫And we're going to have the first property be the max health.

70
00:04:37,220 --> 00:04:39,620
‫That's what we initialize the health to initially.

71
00:04:39,620 --> 00:04:48,170
‫So you property, it's going to be an edit default only because it's going to be used in begin play.

72
00:04:48,170 --> 00:04:53,120
‫And so changing that value of max health after the effect, it's not going to have any impact.

73
00:04:53,120 --> 00:05:01,340
‫So Max health, I'm going to set the max health to 100 here and then we're going to have one other property

74
00:05:01,340 --> 00:05:06,230
‫here, which, as I've said, is going to be a new property just for us to be able to inspect it.

75
00:05:06,230 --> 00:05:09,770
‫And I'm going to have that as visible anywhere.

76
00:05:10,690 --> 00:05:15,310
‫But not editable because we don't want to be able to change that health, at least not at the moment.

77
00:05:15,520 --> 00:05:18,950
‫You might want that for debugging purposes, but I don't.

78
00:05:18,970 --> 00:05:23,080
‫So we're going to have health here and in play.

79
00:05:23,260 --> 00:05:27,250
‫We're going to set it up so that that health.

80
00:05:28,080 --> 00:05:31,060
‫Is equal to the max health.

81
00:05:31,080 --> 00:05:32,460
‫So we start off with spawn.

82
00:05:32,460 --> 00:05:36,150
‫We have our maximum health and then we can start decreasing it.

83
00:05:36,780 --> 00:05:41,760
‫So it's time for a challenge for you to decrease the health of the character.

84
00:05:41,760 --> 00:05:48,480
‫So you're going to use the damage applied here rather than the other option here.

85
00:05:48,480 --> 00:05:52,710
‫So we've got our shooter character, CP take damage.

86
00:05:52,710 --> 00:05:57,450
‫We've got a damage amount, but we actually want the damage applied that comes back from take damage

87
00:05:57,450 --> 00:06:02,100
‫because that could have been reduced by some process in the parent.

88
00:06:02,550 --> 00:06:03,420
‫So we do that.

89
00:06:03,420 --> 00:06:10,410
‫Then we reduce the value of health and log the results just for simplicity's sake so that you can check

90
00:06:10,410 --> 00:06:16,050
‫that when you're shooting at that eye that his health is going down and then test it out.

91
00:06:16,050 --> 00:06:17,820
‫So pause the video and have a go.

92
00:06:18,060 --> 00:06:19,200
‫Okay, welcome back.

93
00:06:19,200 --> 00:06:20,550
‫So let's give it a shot.

94
00:06:20,550 --> 00:06:27,930
‫So health, we basically want to decrease that by the damage applied.

95
00:06:29,260 --> 00:06:30,820
‫But is that enough?

96
00:06:30,850 --> 00:06:37,150
‫Well, what if our damage applied is greater than the amount of health we have got left?

97
00:06:37,330 --> 00:06:43,830
‫Well, in that case, we probably want the damage applied to be that amount of health.

98
00:06:43,840 --> 00:06:47,860
‫So the way we can do this is we can say that the damage applied.

99
00:06:48,770 --> 00:06:54,620
‫Should be equal to the f math dot min of the health.

100
00:06:55,610 --> 00:06:57,870
‫And the damage applied.

101
00:06:57,890 --> 00:07:01,010
‫So this is the damage that we basically want to apply.

102
00:07:01,130 --> 00:07:06,260
‫So we're going to I'm actually going to go ahead and rename that instead of calling it damage applied.

103
00:07:06,380 --> 00:07:10,040
‫I'm going to call it the damage to apply.

104
00:07:10,780 --> 00:07:13,480
‫Damage to apply.

105
00:07:13,900 --> 00:07:18,490
‫So go rename that throughout here and then the damage to apply.

106
00:07:18,520 --> 00:07:21,450
‫We're going to health minus the damage to apply.

107
00:07:21,460 --> 00:07:22,180
‫That makes sense.

108
00:07:22,180 --> 00:07:25,270
‫And the basically at worst that's going to be the health.

109
00:07:25,270 --> 00:07:28,270
‫So that's going to set our health down to zero.

110
00:07:28,840 --> 00:07:33,070
‫And remember, we're supposed to return the damage that we actually applied.

111
00:07:33,070 --> 00:07:41,530
‫So we'll do a return damage to apply, which has now been applied and we finally want a UII log at the

112
00:07:41,530 --> 00:07:45,430
‫end here, which is just going to be a warning so we can see it.

113
00:07:46,150 --> 00:07:54,310
‫And I want this to be the health left here and it's going to be a percent F to or percent lowercase

114
00:07:54,310 --> 00:07:56,020
‫F to show a float.

115
00:07:56,230 --> 00:08:03,070
‫And then we can pop in the health right there and go ahead and hit build.

116
00:08:04,890 --> 00:08:05,220
‫Now.

117
00:08:05,250 --> 00:08:08,460
‫Mine's compiled after fixing a missing semicolon.

118
00:08:08,460 --> 00:08:15,840
‫Here, let's go and make sure that we have a window developer tools and we want the output log here

119
00:08:15,840 --> 00:08:18,060
‫so that we can see that log message.

120
00:08:18,060 --> 00:08:25,710
‫And I'm going to go ahead and hit play and go and try and shoot at this character if I shoot him.

121
00:08:25,710 --> 00:08:26,170
‫There you go.

122
00:08:26,190 --> 00:08:30,210
‫Health left 1976, blah, blah, blah, all the way down to zero.

123
00:08:30,210 --> 00:08:35,490
‫And if I keep shooting, we don't go below zero because of that eff math dot min.

124
00:08:35,490 --> 00:08:38,370
‫So that tells me that we've got our health set up correctly.

125
00:08:38,370 --> 00:08:42,180
‫Just another way of doing it as opposed to doing it within a component.

126
00:08:42,180 --> 00:08:46,680
‫You can do it within the actor as well as some pros and cons of that approach.

127
00:08:46,710 --> 00:08:51,780
‫Eventually, you may want to strip this out into a separate component so that you can reuse it on different

128
00:08:51,780 --> 00:08:52,410
‫elements.

129
00:08:52,410 --> 00:08:57,990
‫But this is just another way, as I say now, because this character isn't looking particularly dead

130
00:08:57,990 --> 00:09:00,120
‫after we've shot him down to zero health.

131
00:09:00,120 --> 00:09:02,910
‫We need to fix up the animations to support that.

132
00:09:02,910 --> 00:09:06,180
‫So we'll see that in the next lecture and I'll see you there.

