﻿1
00:00:04,330 --> 00:00:05,110
‫Hello and welcome.

2
00:00:05,110 --> 00:00:11,350
‫In this lecture we're going to be making our trigger component print re locking when it has got an empty

3
00:00:11,380 --> 00:00:15,010
‫space and when we put the gargoyle in, it will unlock.

4
00:00:15,010 --> 00:00:17,830
‫And if I take it out again, it will be lock again.

5
00:00:17,830 --> 00:00:23,170
‫And the way we're going to do this is by pulling out our functionality for finding whether we have an

6
00:00:23,170 --> 00:00:26,320
‫acceptable actor into its own separate function.

7
00:00:26,320 --> 00:00:29,320
‫And we're going to learn about early returns within four loops.

8
00:00:29,320 --> 00:00:31,120
‫To do that, let's dive in.

9
00:00:31,920 --> 00:00:36,660
‫So this chunk of code that we've got in tech component is already starting to feel like a function to

10
00:00:36,660 --> 00:00:36,900
‫me.

11
00:00:36,900 --> 00:00:43,620
‫Essentially, it's a function that gets hold of the acceptable actor, if we've got one, so we can

12
00:00:43,620 --> 00:00:45,420
‫go ahead to our trigger component.

13
00:00:46,230 --> 00:00:49,680
‫And under the private section, create such a function.

14
00:00:49,680 --> 00:00:51,850
‫Now, what should its return type be?

15
00:00:51,870 --> 00:00:58,980
‫Well, I think it should be an A actor pointer, which can always be a null pointer if there is no acceptable

16
00:00:58,980 --> 00:00:59,690
‫actor.

17
00:00:59,700 --> 00:01:05,190
‫So we can then call this get acceptable actor empty parentheses.

18
00:01:05,190 --> 00:01:06,510
‫We don't need to give it anything.

19
00:01:06,510 --> 00:01:12,420
‫And because it doesn't change our function, we can make it const.

20
00:01:12,450 --> 00:01:16,350
‫Let's go ahead and implement this over in the CPP file.

21
00:01:16,350 --> 00:01:26,190
‫We're going to indent it, give it a you trigger component colon, colon and replace it semicolon with

22
00:01:26,190 --> 00:01:27,030
‫braces.

23
00:01:27,570 --> 00:01:35,520
‫And then the code we want is basically the contents of tic component, except what we don't want to

24
00:01:35,520 --> 00:01:39,640
‫be doing is just logging that we've unlocked actor here.

25
00:01:39,660 --> 00:01:47,190
‫What we want to do is in the case where we've found this actor, we want to essentially be returning

26
00:01:47,190 --> 00:01:47,280
‫it.

27
00:01:47,280 --> 00:01:48,870
‫So there's a couple of ways we could do this.

28
00:01:48,870 --> 00:01:56,370
‫We could say at the start of our function that there is an a actor pointer, which is going to be the

29
00:01:56,370 --> 00:02:00,840
‫return actor, and by default we set this to null pointer.

30
00:02:00,840 --> 00:02:04,380
‫So there is no actor that we're going to return.

31
00:02:04,380 --> 00:02:09,360
‫But when we go through our for loop and we eventually find an actor that has the acceptable tag, then

32
00:02:09,360 --> 00:02:16,410
‫we can say, Hey, the return actor should actually point to the actor pointer that we've found here

33
00:02:16,410 --> 00:02:17,940
‫in our array.

34
00:02:17,940 --> 00:02:22,590
‫And then at the end of the for loop, after the for loop is finished, we've been through all the actors.

35
00:02:22,590 --> 00:02:28,920
‫We can go ahead and return the return actor variable because that is the right type.

36
00:02:29,400 --> 00:02:30,390
‫That's okay.

37
00:02:30,390 --> 00:02:35,400
‫But it requires us to create a variable and also it requires us to run through the entire for loop,

38
00:02:35,400 --> 00:02:38,040
‫even if we've already found an acceptable actor.

39
00:02:38,460 --> 00:02:42,180
‫So a better way of doing this is using an early return.

40
00:02:42,180 --> 00:02:48,240
‫And we've seen this sort of thing before in our grab a CP where we've done this.

41
00:02:48,240 --> 00:02:54,270
‫If the physics handles equals a null pointer, then we return and the rest of the tick component doesn't

42
00:02:54,270 --> 00:02:55,260
‫need to run.

43
00:02:55,260 --> 00:03:00,180
‫We can use the same kind of efficiency within for loops too.

44
00:03:00,180 --> 00:03:06,960
‫So what I'm going to do is if we've found the acceptable actor, we're just going to do a return actor.

45
00:03:06,960 --> 00:03:11,850
‫We're literally returning the pointer that we're just found in this for loop and the rest of the for

46
00:03:11,850 --> 00:03:13,500
‫loop doesn't have to run.

47
00:03:13,800 --> 00:03:20,340
‫However, if we drop through to the end of the for loop and we have not found anything, we've not returned

48
00:03:20,340 --> 00:03:23,970
‫anything, return would stop the for loop, leave the function.

49
00:03:23,970 --> 00:03:28,050
‫But if we get to the end, we need to return something by default.

50
00:03:28,410 --> 00:03:31,860
‫This is where we can just return null pointer.

51
00:03:31,860 --> 00:03:33,450
‫So this is the default value.

52
00:03:33,450 --> 00:03:35,670
‫It means we've gone through the entire for loop.

53
00:03:35,670 --> 00:03:38,040
‫We've seen that there is no acceptable actor.

54
00:03:38,040 --> 00:03:40,410
‫None of the actors that we're overlapping are.

55
00:03:41,300 --> 00:03:42,830
‫With the correct tag.

56
00:03:42,830 --> 00:03:45,230
‫So we return null pointer.

57
00:03:45,230 --> 00:03:51,260
‫So now that means I have got this redundant return actor variable which I can go ahead and get rid of.

58
00:03:51,260 --> 00:03:53,510
‫And this reads really quite cleanly.

59
00:03:53,510 --> 00:03:58,070
‫We're going through, we're getting the overlapping actors, we are doing a full loop over them.

60
00:03:58,070 --> 00:04:02,780
‫If each actor we test it, has it got the tag, the acceptable tag?

61
00:04:02,780 --> 00:04:04,550
‫If it does, we stop right there.

62
00:04:04,550 --> 00:04:05,720
‫We return that one.

63
00:04:05,720 --> 00:04:08,330
‫If none of them have it, we return a null pointer.

64
00:04:08,330 --> 00:04:11,780
‫So let's challenge you to log if we find the actor.

65
00:04:11,780 --> 00:04:14,570
‫So call our function in your tick component.

66
00:04:14,570 --> 00:04:20,240
‫Check if the pointer is null and log in both cases, both whether we have an actor or if we don't using

67
00:04:20,240 --> 00:04:21,920
‫an if and an else statement.

68
00:04:21,920 --> 00:04:23,540
‫Pause the video and have a go.

69
00:04:26,460 --> 00:04:26,850
‫KAYE.

70
00:04:26,850 --> 00:04:27,850
‫Welcome back.

71
00:04:27,870 --> 00:04:30,120
‫So let's go over to our tech components.

72
00:04:30,120 --> 00:04:35,490
‫What we're going to do is we're going to create a variable, a actor pointer, which is going to be

73
00:04:35,490 --> 00:04:40,620
‫the actor, which is equal to get acceptable actor.

74
00:04:40,650 --> 00:04:47,220
‫Then we're going to do if actor not equal to null pointer, then in the body of this, we're going to

75
00:04:47,220 --> 00:04:56,520
‫do a u log unlocking and then in the else another you log this time re locking because that's the behavior

76
00:04:56,520 --> 00:05:01,950
‫I want our trigger component and move it together to have is that if you've got the gargoyle in there,

77
00:05:01,950 --> 00:05:08,760
‫it's going to unlock, it's going to be using the mover and it's going to be re locking if we un basically

78
00:05:08,760 --> 00:05:12,750
‫uncheck the checkbox on move if we remove the gargoyle.

79
00:05:12,750 --> 00:05:13,860
‫So let's compile that.

80
00:05:13,860 --> 00:05:15,960
‫So let's go ahead and hit play.

81
00:05:15,990 --> 00:05:19,620
‫You can see it's re locking because we don't have a gargoyle in there.

82
00:05:19,650 --> 00:05:21,660
‫Pick up the gargoyle and bring him over.

83
00:05:21,660 --> 00:05:22,560
‫And there you go.

84
00:05:22,560 --> 00:05:27,900
‫It's now unlocking take it out and it's re locking unlocking, re locking.

85
00:05:27,900 --> 00:05:28,380
‫Perfect.

86
00:05:28,380 --> 00:05:30,000
‫If I step inside, nothing happens.

87
00:05:30,000 --> 00:05:31,080
‫It's just re locking.

88
00:05:31,230 --> 00:05:38,010
‫So we've got successfully been able to find out whether we've got something in our trigger component

89
00:05:38,010 --> 00:05:38,850
‫or not.

90
00:05:38,850 --> 00:05:40,680
‫Now we just need to make it do something.

91
00:05:40,680 --> 00:05:42,750
‫And that's going to be the subject of the next lecture.

92
00:05:42,750 --> 00:05:43,500
‫I'll see you there.

