GameplayTasks API – Making things happen with GameplayTasks

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.

Getting ready

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.

How to do it…

  1. Ensure that you have linked GameplayTasks API into your ProjectName.Build.cs file.
  2. Click on File | Add C++ Class… Choose to derive from 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.
  3. Once your 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;
    }
  4. Override the 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 );
    }
  5. Add GameplayTasksComponent to your Actor class derivative, which is available in the Components dropdown of the Components tab in the Blueprint editor.
  6. Create and add an instance of your 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 );
    }
  7. This code runs anywhere in your Actor class derivative, any time after GameplayTasksComponent is initialized (any time after PostInitializeComponents()).

How it works…

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.

There's more…

You can derive GameplayTasks for any number of events in your game. What's more is that you can override a few more virtual functions to hook into additional functionality.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.118.159