UGameplayStatics class in Unreal Engine

What class to select to fit the needs? How they differ each other and for what purpose are they intended?

UGameplayStatics is a static class with useful gameplay utility functions that can be called from both Blueprint and C++

Inheritance Hierarchy

Get all Actors of Class

int32 ASomeClass::GetTargetCount()
{
   TArray<AActor*> ActorsArr;
   UGameplayStatics::GetAllActorsOfClass(this, AtargetedActor::StaticClass(), ActorsArr);
   return ActorsArr.Num();
}

Apply Damage

It applies a damage. It's usually inside OnHit or similar triggerthat provides OtherActor, Damage and other parameters.

UGameplayStatics::ApplyDamage(OtherActor, Damage, GetOwner()->GetInstigatorController(), this, UDamageType::StaticClass());

An alternate way to applying damage is a TakeDamage() function provided by AActor class. As it's a virtual function, you can override it in any class inherited from AActor and process a functionality for applying a damage.

Spawn UGameplayStatics item

Plays the specified effect at the given location, rotation, fire and forget, once completed. It works also for sounds.

Dynamically creates UParticleSystem class in the runtime.

It's usually inside OnHit or similar trigger.

  • Spawn particle effect

    SpawnEmitterAtLocation

    When the effect is being spawned within the given Actor, such as existing bullet in the form of AProjectile, a following code to play a sound can be used

    if(Hitparticles) UGameplayStatics::SpawnEmitterAtLocation(this, HitPartices, GetActorLocation(), GetActorRotation());

    If a particle effect is being triggered from a different AActor, such as ACharacter (e.g. because we do not use bullet objects in the project), there's necessary to work with a trigger providing a Hit.Location, such as ,OnHit. Then there can be used something like the following code to spawn an effect.

    UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), FlashFile, Hit.Location, ShotDirection.Rotation());

    SpawnEmitterAttached - Spawn into a socket pointer

    // .h file
    UPROPERTY(EditAnywhere)
    UParticleSystem* FlashFile;
    // .cpp file
    UGameplayStatics::SpawnEmitterAttached(FlashFile, Mesh, TEXT("FlashFileSocket"));
  • Spawn Sound into a socket pointer

    // .h file
    UPROPERTY(EditAnywhere)
    USoundBase* SoundFile;
    // .cpp file
    UGameplayStatics::SpawnSoundAttached(SoundFile, Mesh, TEXT("SoundFileSocket"));

Play Sound

When the sound is being played within the given Actor, such as existing bullet in the form of AProjectile, a following code to play a sound can be used

if(HitSound) UGameplayStatics::PlaySoundAtLocation(this, HitSound, GetActorLocation());

If a game sound is being triggered from a different AActor, such as ACharacter (e.g. because we do not use bullet objects in the project), there's necessary to work with a trigger providing a Hit.Location, such as ,OnHit. Then there can be used something like the following code to play a sound.

UGameplayStatics::PlaySoundAtLocation(GetWorld(), SoundFile, Hit.Location);

For sounds settings and working withe the sound, check Sound in Unreal Engine wiki page.