‫Hello and welcome.
‫In this lecture, we're going to be learning how we can get a pointer to the owning actor of a component.
‫We're going to find out what a pointer is, what addresses are in memory, and we're going to print
‫them out into our log messages as well.
‫Let's dive in.
‫So let's take a second to understand a core concept of what's going on in programming in general about
‫variable addresses.
‫What is an address of a variable?
‫Well, we store all of our variables in memory in this ram chips on your computer and variables require
‫memory to store their data.
‫So a floating point number requires either 32 bits of data or 64, if it's a double integer, requires
‫a certain amount of memory.
‫Also, the bigger the struct you have or the bigger the classes you have, like actors can be very large
‫and encompass a lot of data might require even a few kilobytes of memory per actor, things like that.
‫Well, that memory has to live somewhere on this large field of empty data that is your memory banks.
‫And that location is known as an address.
‫So how does this work in practice?
‫Well, imagine we have this large field of zeros and ones.
‫That is the data on your ram.
‫Well, then the variable and a actor variable called My actor needs to have a certain location within
‫that memory that is allocated to store the data in the actor.
‫So this red section of memory might be where we're storing the data for this one particular actor,
‫and it has an address usually denoted in binary or in hexadecimal that is essentially just the location.
‫If you start counting off all the bytes, all the ones and zeros along here, in fact, we don't actually
‫count the individual ones and zeros.
‫We count in steps of eight, but that's not too important.
‫We count the bytes.
‫That is essentially the address where we start.
‫Talking about our actor, and then each type has a certain size.
‫So we know that the actor would require so many bytes of data.
‫And so that tells us where to find the data about our particular actor.
‫So why is this important?
‫Well, let's have a look at the situation where we might want to make a take to actors.
‫We want to have two variables that reference the same data.
‫Well, in the naive case, we would simply just copy this.
‫So we've got a chunk of memory that is allocated to the my actor variable.
‫We've got another chunk of memory that's allocated to an empty variable called Your Actor, which we
‫haven't yet assigned anything to.
‫And then what we want to do because we want them to contain the same data, is we just assign one to
‫the other.
‫And that essentially results in us just making a copy of all of the bits of data that were in my actor
‫and putting them into all the bits that make up your actor.
‫So this can be a fairly costly procedure because your CPU has to go through and evaluate each piece
‫of memory in the original and put it into the new location, which takes up time.
‫Now there's a way around this using something called a pointer.
‫So here, instead of storing the entirety of the My Actor, we have a variable called a my actor or
‫an actor a actor pointer that doesn't store the whole actor.
‫It just stores the address where you can find an act.
‫So you can see actually uses up a lot less memory to store an address.
‫And then what we do is into your actor, we store the address of my actor, so it ends up pointing to
‫the original actor rather than copying the data over, hence the name pointer.
‫Now this isn't the actual syntax in C++.
‫In C++, the syntax is much more condensed.
‫We say that something is a pointer by putting a star after the type.
‫So a actor star means that this is an a actor pointer and the to get the address of a variable, we
‫use an ampersand we won't be using making much use of this address of just yet because there's a lot
‫of functions in unreal that return pointers.
‫So we just need to know how to use those pointers rather than create them for the time being.
‫So one such function that allows us to get access to other objects in our scene is get owner.
‫So why does this apply when we've got a component, it is owned by an actor.
‫So if we wanted this component to talk to our actor, to get its location, to set its location and
‫do things like that, then we need to be able to get a pointer to that actor.
‫We don't want to take a copy of it.
‫In fact, we don't want to take a copy of it because we want to be changing the actor.
‫If we were making a change to a copy, it wouldn't actually show up in the scene.
‫So we need to be able to store a variable that is a pointer to an actor.
‫The way we do this, as we've just seen in the slides, is to have an A actor type.
‫But then we put a star at the end which tells us this isn't an a actor being stored here, this is just
‫the address of an actor being stored.
‫So we take the actor and we're going to call this owner and then we're going to set that equal to the
‫result of calling get owner.
‫So get owner is a function of an actor component which allows you to get an owner pointer.
‫Get an actor pointer that points at the owner, put a semicolon at the end of that line.
‫And what we've got here is a variable that stores the address of an actor that owns this particular
‫component.
‫So I'd like to challenge you to do something by printing the address of this variable.
‫So use the a actor variable and use the percent you format, specify it in the logs.
‫So percent you essentially means that you're using an unsigned integer.
‫That means an integer that can't be negative, which makes sense because addresses can't be negative
‫when we're talking about where to find memory and then figure out what does it print, go and play,
‫see the log message, see what it prints out.
‫Does it change between different plays if you stop playing and start playing again?
‫And what about if the mover is on multiple actors?
‫Is there multiple different addresses printed out in the console?
‫Pause the video and have a little investigation.
‫Okay.
‫Welcome back.
‫So we're going to go over to this Yule log and we're going to say mover owner, address, colon percent,
‫you.
‫So that's where it's going to put the pointers value.
‫And then we're going to put the actual pointer.
‫So we're going to have the owner in here.
‫So it's going to print out the address of the owner.
‫Let's go over into unreal.
‫Hit control alt f 11.
‫And when that finishes compiling, let's hit play.
‫And what we're seeing is that the log message mover owner address is coming through and it's giving
‫us a very large number actually for this address.
‫What if I stop playing and play again just for a second?
‫You can see the address is quite substantially different, and that's because every time that we hit
‫play, Unreal is creating a whole new set of actors in the scene, basically ready to play, starting
‫from scratch.
‫So that's why we're seeing new address locations each time we hit play.
‫What happens if I went to this pillar, for example, and added a mover component to the pillar as well,
‫and we hit play and scroll down to the bottom of my logs.
‫You can see that there are each tick two different addresses being printed out here because there are
‫two different actors which are stored in two different locations in memory.
‫So these are the two addresses and you can see there's actually quite a few megabytes of difference
‫between these two addresses, which just goes to tell you how much information is being stored in memory
‫by Unreal and also why it's important to be efficient with not making too many copies.
‫That's it for this lecture.
‫I will see you in the next one where we'll be doing more fun things with pointers.