﻿1
00:00:04,290 --> 00:00:05,490
‫Hello and welcome.

2
00:00:05,490 --> 00:00:12,810
‫In this lecture, we're going to be learning how we can get a pointer to the owning actor of a component.

3
00:00:12,810 --> 00:00:16,740
‫We're going to find out what a pointer is, what addresses are in memory, and we're going to print

4
00:00:16,740 --> 00:00:19,740
‫them out into our log messages as well.

5
00:00:19,740 --> 00:00:20,640
‫Let's dive in.

6
00:00:21,090 --> 00:00:27,660
‫So let's take a second to understand a core concept of what's going on in programming in general about

7
00:00:27,660 --> 00:00:29,130
‫variable addresses.

8
00:00:29,130 --> 00:00:30,810
‫What is an address of a variable?

9
00:00:30,810 --> 00:00:42,030
‫Well, we store all of our variables in memory in this ram chips on your computer and variables require

10
00:00:42,030 --> 00:00:43,920
‫memory to store their data.

11
00:00:43,920 --> 00:00:51,840
‫So a floating point number requires either 32 bits of data or 64, if it's a double integer, requires

12
00:00:51,840 --> 00:00:52,740
‫a certain amount of memory.

13
00:00:52,740 --> 00:00:58,440
‫Also, the bigger the struct you have or the bigger the classes you have, like actors can be very large

14
00:00:58,440 --> 00:01:04,230
‫and encompass a lot of data might require even a few kilobytes of memory per actor, things like that.

15
00:01:04,590 --> 00:01:12,510
‫Well, that memory has to live somewhere on this large field of empty data that is your memory banks.

16
00:01:12,660 --> 00:01:16,410
‫And that location is known as an address.

17
00:01:16,410 --> 00:01:18,420
‫So how does this work in practice?

18
00:01:18,420 --> 00:01:21,870
‫Well, imagine we have this large field of zeros and ones.

19
00:01:21,870 --> 00:01:25,140
‫That is the data on your ram.

20
00:01:25,320 --> 00:01:32,580
‫Well, then the variable and a actor variable called My actor needs to have a certain location within

21
00:01:32,580 --> 00:01:37,110
‫that memory that is allocated to store the data in the actor.

22
00:01:37,110 --> 00:01:42,300
‫So this red section of memory might be where we're storing the data for this one particular actor,

23
00:01:42,300 --> 00:01:50,610
‫and it has an address usually denoted in binary or in hexadecimal that is essentially just the location.

24
00:01:50,610 --> 00:01:57,840
‫If you start counting off all the bytes, all the ones and zeros along here, in fact, we don't actually

25
00:01:57,840 --> 00:01:59,220
‫count the individual ones and zeros.

26
00:01:59,220 --> 00:02:03,360
‫We count in steps of eight, but that's not too important.

27
00:02:03,360 --> 00:02:04,410
‫We count the bytes.

28
00:02:04,620 --> 00:02:08,070
‫That is essentially the address where we start.

29
00:02:08,810 --> 00:02:14,180
‫Talking about our actor, and then each type has a certain size.

30
00:02:14,180 --> 00:02:18,020
‫So we know that the actor would require so many bytes of data.

31
00:02:18,020 --> 00:02:23,240
‫And so that tells us where to find the data about our particular actor.

32
00:02:23,360 --> 00:02:24,880
‫So why is this important?

33
00:02:24,890 --> 00:02:30,680
‫Well, let's have a look at the situation where we might want to make a take to actors.

34
00:02:30,680 --> 00:02:34,340
‫We want to have two variables that reference the same data.

35
00:02:34,520 --> 00:02:38,240
‫Well, in the naive case, we would simply just copy this.

36
00:02:38,240 --> 00:02:44,360
‫So we've got a chunk of memory that is allocated to the my actor variable.

37
00:02:44,510 --> 00:02:50,030
‫We've got another chunk of memory that's allocated to an empty variable called Your Actor, which we

38
00:02:50,030 --> 00:02:52,280
‫haven't yet assigned anything to.

39
00:02:52,550 --> 00:02:59,060
‫And then what we want to do because we want them to contain the same data, is we just assign one to

40
00:02:59,060 --> 00:02:59,690
‫the other.

41
00:02:59,690 --> 00:03:07,070
‫And that essentially results in us just making a copy of all of the bits of data that were in my actor

42
00:03:07,070 --> 00:03:10,400
‫and putting them into all the bits that make up your actor.

43
00:03:10,730 --> 00:03:17,990
‫So this can be a fairly costly procedure because your CPU has to go through and evaluate each piece

44
00:03:17,990 --> 00:03:22,680
‫of memory in the original and put it into the new location, which takes up time.

45
00:03:22,700 --> 00:03:25,760
‫Now there's a way around this using something called a pointer.

46
00:03:25,760 --> 00:03:33,530
‫So here, instead of storing the entirety of the My Actor, we have a variable called a my actor or

47
00:03:33,530 --> 00:03:38,840
‫an actor a actor pointer that doesn't store the whole actor.

48
00:03:38,840 --> 00:03:42,170
‫It just stores the address where you can find an act.

49
00:03:42,170 --> 00:03:46,580
‫So you can see actually uses up a lot less memory to store an address.

50
00:03:46,580 --> 00:03:54,140
‫And then what we do is into your actor, we store the address of my actor, so it ends up pointing to

51
00:03:54,140 --> 00:03:59,090
‫the original actor rather than copying the data over, hence the name pointer.

52
00:03:59,240 --> 00:04:01,880
‫Now this isn't the actual syntax in C++.

53
00:04:01,880 --> 00:04:04,430
‫In C++, the syntax is much more condensed.

54
00:04:04,640 --> 00:04:09,650
‫We say that something is a pointer by putting a star after the type.

55
00:04:09,650 --> 00:04:17,660
‫So a actor star means that this is an a actor pointer and the to get the address of a variable, we

56
00:04:17,660 --> 00:04:22,310
‫use an ampersand we won't be using making much use of this address of just yet because there's a lot

57
00:04:22,310 --> 00:04:25,250
‫of functions in unreal that return pointers.

58
00:04:25,250 --> 00:04:31,160
‫So we just need to know how to use those pointers rather than create them for the time being.

59
00:04:31,640 --> 00:04:40,070
‫So one such function that allows us to get access to other objects in our scene is get owner.

60
00:04:40,070 --> 00:04:46,460
‫So why does this apply when we've got a component, it is owned by an actor.

61
00:04:46,460 --> 00:04:53,690
‫So if we wanted this component to talk to our actor, to get its location, to set its location and

62
00:04:53,690 --> 00:04:58,400
‫do things like that, then we need to be able to get a pointer to that actor.

63
00:04:58,400 --> 00:05:00,040
‫We don't want to take a copy of it.

64
00:05:00,050 --> 00:05:03,350
‫In fact, we don't want to take a copy of it because we want to be changing the actor.

65
00:05:03,350 --> 00:05:07,160
‫If we were making a change to a copy, it wouldn't actually show up in the scene.

66
00:05:07,280 --> 00:05:13,850
‫So we need to be able to store a variable that is a pointer to an actor.

67
00:05:14,330 --> 00:05:19,760
‫The way we do this, as we've just seen in the slides, is to have an A actor type.

68
00:05:19,760 --> 00:05:25,970
‫But then we put a star at the end which tells us this isn't an a actor being stored here, this is just

69
00:05:25,970 --> 00:05:28,880
‫the address of an actor being stored.

70
00:05:29,150 --> 00:05:36,800
‫So we take the actor and we're going to call this owner and then we're going to set that equal to the

71
00:05:36,800 --> 00:05:39,140
‫result of calling get owner.

72
00:05:39,140 --> 00:05:46,970
‫So get owner is a function of an actor component which allows you to get an owner pointer.

73
00:05:46,970 --> 00:05:53,690
‫Get an actor pointer that points at the owner, put a semicolon at the end of that line.

74
00:05:53,690 --> 00:06:00,800
‫And what we've got here is a variable that stores the address of an actor that owns this particular

75
00:06:00,800 --> 00:06:01,670
‫component.

76
00:06:01,670 --> 00:06:07,580
‫So I'd like to challenge you to do something by printing the address of this variable.

77
00:06:07,580 --> 00:06:14,690
‫So use the a actor variable and use the percent you format, specify it in the logs.

78
00:06:14,690 --> 00:06:19,550
‫So percent you essentially means that you're using an unsigned integer.

79
00:06:19,550 --> 00:06:25,250
‫That means an integer that can't be negative, which makes sense because addresses can't be negative

80
00:06:25,730 --> 00:06:31,790
‫when we're talking about where to find memory and then figure out what does it print, go and play,

81
00:06:31,820 --> 00:06:33,980
‫see the log message, see what it prints out.

82
00:06:34,250 --> 00:06:38,000
‫Does it change between different plays if you stop playing and start playing again?

83
00:06:38,000 --> 00:06:41,900
‫And what about if the mover is on multiple actors?

84
00:06:41,900 --> 00:06:45,680
‫Is there multiple different addresses printed out in the console?

85
00:06:45,710 --> 00:06:48,620
‫Pause the video and have a little investigation.

86
00:06:52,030 --> 00:06:52,390
‫Okay.

87
00:06:52,390 --> 00:06:53,410
‫Welcome back.

88
00:06:53,440 --> 00:07:03,460
‫So we're going to go over to this Yule log and we're going to say mover owner, address, colon percent,

89
00:07:03,490 --> 00:07:04,380
‫you.

90
00:07:04,510 --> 00:07:07,540
‫So that's where it's going to put the pointers value.

91
00:07:07,930 --> 00:07:10,210
‫And then we're going to put the actual pointer.

92
00:07:10,210 --> 00:07:12,430
‫So we're going to have the owner in here.

93
00:07:12,940 --> 00:07:16,360
‫So it's going to print out the address of the owner.

94
00:07:16,450 --> 00:07:19,150
‫Let's go over into unreal.

95
00:07:19,180 --> 00:07:21,910
‫Hit control alt f 11.

96
00:07:21,910 --> 00:07:24,610
‫And when that finishes compiling, let's hit play.

97
00:07:24,610 --> 00:07:29,740
‫And what we're seeing is that the log message mover owner address is coming through and it's giving

98
00:07:29,740 --> 00:07:32,300
‫us a very large number actually for this address.

99
00:07:32,320 --> 00:07:36,460
‫What if I stop playing and play again just for a second?

100
00:07:36,460 --> 00:07:42,250
‫You can see the address is quite substantially different, and that's because every time that we hit

101
00:07:42,250 --> 00:07:51,130
‫play, Unreal is creating a whole new set of actors in the scene, basically ready to play, starting

102
00:07:51,130 --> 00:07:51,850
‫from scratch.

103
00:07:51,850 --> 00:07:57,220
‫So that's why we're seeing new address locations each time we hit play.

104
00:07:57,220 --> 00:08:05,320
‫What happens if I went to this pillar, for example, and added a mover component to the pillar as well,

105
00:08:05,320 --> 00:08:08,020
‫and we hit play and scroll down to the bottom of my logs.

106
00:08:08,020 --> 00:08:15,310
‫You can see that there are each tick two different addresses being printed out here because there are

107
00:08:15,310 --> 00:08:20,890
‫two different actors which are stored in two different locations in memory.

108
00:08:20,890 --> 00:08:25,990
‫So these are the two addresses and you can see there's actually quite a few megabytes of difference

109
00:08:25,990 --> 00:08:31,810
‫between these two addresses, which just goes to tell you how much information is being stored in memory

110
00:08:31,810 --> 00:08:38,290
‫by Unreal and also why it's important to be efficient with not making too many copies.

111
00:08:38,290 --> 00:08:39,700
‫That's it for this lecture.

112
00:08:39,700 --> 00:08:43,570
‫I will see you in the next one where we'll be doing more fun things with pointers.

