Super Confused About GetOwner()

I didn’t understand why we were getting owner here and checking for it or why we’re using the AActor pointer rather than ‘this’ to pass the return of the GetInstigatorController() function into the ApplyDamage() function. I went on a journey of discovery in the docs and ended up even more confused than when I started. What does GetOwner() do exactly? The documentation is sparse and I feel like I’m going in circles.

To not self-inflict damage. The owner was set to be the tank or turret that shot the projectile.

Because that argument is what you want to apply damage to. Passing this would be the projectile that just hit something.

That argument is for the controller responsible for the damage which would be the controller of the tank/turret.

Get’s the owning actor of the actor or component. For a component it’s whatever the component is attached to. For an actor it’s whatever you decide it to be. It is nothing if not set.

1 Like

Hi Dan, thanks for the reply. I wasn’t clear on one part, unfortunately, so let me rephrase from my point of view. Maybe I’m thinking about some things wrongly, don’t know.

‘this’ acts like a pointer to the instance.
if an object is not owned by something, GetOwner() returns a pointer to itself, correct?

In that case ‘this’ and pointer returned by GetOwner() would be equivalent?

So here we are checking GetOwner() just in case as a general rule?

And then along the same lines, we use MyActor->GetInstigatorController() rather than this->GetInstigatorController() for the same reasons?

It is a pointer to the instance executing that code.

Incorrect, it would return nullptr

They would do completely different things. this->GetInstigatorController() is exactly the same as GetInstigatorController()

Unattached, by default an object owns itself though, right?

Maybe a better question at this point: in the above code snippet, what is the owner?

Again, no.

Answered previously. The tank or turret that shot it. You yourself set this.

AProjectile* Projectile = GetWorld()->SpawnActor....;
Projectile-SetOwner(this);

Hey Dan, thanks a ton for your patience. A lot of my misunderstanding there stemmed from forgetting that we set the owner.

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms