About "this"

We have functions

void UGameplayStatics::GetAllActorsOfClass(const UObject* WorldContextObject, TSubclassOf<AActor> ActorClass, TArray<AActor*>& OutActors)

APawn* UGameplayStatics::GetPlayerPawn(const UObject* WorldContextObject, int32 PlayerIndex)

Both needed an argument UObject* WorldContextObject
But our implementation is

UGameplayStatics::GetAllActorsOfClass(GetWorld(), APawnTurret::StaticClass(), TurretActors);
PlayerTank = Cast<APawnTank>(UGameplayStatics::GetPlayerPawn(this, 0));

Why in first case we use GetWorld() and in second this?

For safety I ask one more thing. What return this for “ProjectileBase” actor, “PawnBase” pawn, derived pawn “PawnTank” , “HealthComponent” component, “TankGameModeBase” Game mode?

1 Like

No particular reason. They are both in the world you want to work with so should achieve the same thing.

this is a pointer to the instance currently executing that code.

class Thing
{
    void DoSomething();
};

void Thing::DoSomething()
{
}

// used like
Thing T;
T.DoSomething();

This is just syntactic sugar for the following

class Thing
{
    void DoSomething();
};

void DoSomething(Thing* Foo /*this*/)
{
}

// used like
Thing T;
DoSomething(&T);

All member functions have an invisible first parameter which is what this is.

1 Like

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

Privacy & Terms