GameplayTasks
are used to wrap up some gameplay functionality in a reusable object. All you have to do to use them is derive from the UGameplayTask
base class and override some of the member functions that you prefer to implement.
Go in the UE4 Editor and navigate to Class Viewer. Ensure that you have linked in the GameplayTasks
API into your ProjectName.Build.cs
file and search with Actors Only tickbox off for the GameplayTask
object type.
GameplayTasks
API into your ProjectName.Build.cs
file.GameplayTask
. To do so, you must first tick Show All Classes, and then type gameplaytask
into the filter box. Click on Next, name your C++ class (something like GameplayTask_TaskName
is the convention) then add the class to your project. The example spawns a particle emitter and is called GameplayTask_CreateParticles
.GameplayTask_CreateParticles.h
and .cpp
pair are created, navigate to the .h
file and declare a static constructor that creates a GameplayTask_CreateParticles
object for you:// Like a constructor. UGameplayTask_CreateParticles* UGameplayTask_CreateParticles::ConstructTask( TScriptInterface<IGameplayTaskOwnerInterface> TaskOwner, UParticleSystem* particleSystem, FVector location ) { UGameplayTask_CreateParticles* task = NewTask<UGameplayTask_CreateParticles>( TaskOwner ); // Fill fields if( task ) { task->ParticleSystem = particleSystem; task->Location = location; } return task; }
UGameplayTask_CreateEmitter::Activate()
function, which contains code that runs when GameplayTask
is effected, as follows:void UGameplayTask_CreateEmitter::Activate() { Super::Activate(); UGameplayStatics::SpawnEmitterAtLocation( GetWorld(), ParticleSystem->GetDefaultObject<UParticleSystem>(), Location ); }
GameplayTasksComponent
to your Actor
class derivative, which is available in the Components dropdown of the Components tab in the Blueprint editor.GameplayTask
inside your Actor
derivative instance using the following code:UGameplayTask_CreateParticles* task = UGameplayTask_CreateParticles::ConstructTask( this, particleSystem, FVector( 0.f, 0.f, 200.f ) ); if( GameplayTasksComponent ) { GameplayTasksComponent->AddTaskReadyForActivation( *task ); }
Actor
class derivative, any time after GameplayTasksComponent
is initialized (any time after PostInitializeComponents()
).
GameplayTasks
simply register with the GameplayTasksComponent
situated inside an Actor
class derivative of your choice. You can activate any number of GameplayTasks
at any time during gameplay to trigger their effects.
GameplayTasks
can also kick off GameplayEffects
to change attributes of AbilitySystemsComponents
if you wish.
18.117.230.81