Launching Grenades in Unreal with Gameplay Ability System: Part 1
By: Denis Savosin
Tags: Prototyping, Game Development, Unreal Engine, Unreal Engine 4, Programming

Getting Started

Prerequisites:

Unreal Engine 4.25+

Visual Studio 2019, please install C++, .NET and UWP development packages.

Download Paragon: Wraith character pack from the Marketplace. This character pack is a free. [https://www.unrealengine.com/marketplace/en-US/product/paragon-wraith]

The setup for the Gameplay Ability Plugin was covered many times in different tutorials, so if you read them before and already have a setup running, you can skip to the “Cutting A Corner” part where I did something different and simple in contrast to other tutorials. If it is your first time, then it will take you a few copy/paste to get running.

Start by creating a C++ Unreal Project using the ThirdPersonCharacter template. After creating the project, you need to go to the Edit→Plugins and enable the Gameplay Abilities plugin, then close the Editor.

After closing, open the YouProjectName.Build.cs file in Visual Studio and include the following dependencies. I’ve highlighted all added code in bold for clarity.

using UnrealBuildTool; public class SimpleGAS : ModuleRules

{

public SimpleGAS(ReadOnlyTargetRules Target) : base(Target)

{

PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs; PublicDependencyModuleNames.AddRange(new string[] { "GameplayAbilities", "GameplayTags", "GameplayTasks", "Core", "CoreUObject", "Engine", "InputCore", "HeadMountedDisplay" });

}

}

First, we need to declare the AbilitySystemComponent in the ThirdPersonCharacter.h (ASimpleGASCharacter in my project and AYourProjectNameCharacter in your project) header. To keep it organized, you can declare all your component properties in one place.

/** Follow camera */

UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))

class UCameraComponent* FollowCamera; /** Gameplay Ability Component */

UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Abilities, meta = (AllowPrivateAccess = "true"))

class UAbilitySystemComponent* AbilitySystemComponent;

And on the bottom of the same header file let’s add a getter:

public:

/** Returns CameraBoom subobject **/

FORCEINLINE class USpringArmComponent* GetCameraBoom() const { return CameraBoom; }

/** Returns FollowCamera subobject **/

FORCEINLINE class UCameraComponent* GetFollowCamera() const { return FollowCamera; }

/** Returns Ability System Component subobject **/

FORCEINLINE UAbilitySystemComponent* GetAbilitySystemComponent() const override { return AbilitySystemComponent; }

Then declare a StartupAbility property. Once we create our ability blueprint, we will drag and drop it into this property field in the Editor.

public:

ASimpleGASCharacter(); /** Startup Ability to Grant on BeginPlay */

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Abilities)

TSubclassOf<class UGameplayAbility> StartupAbility;

To initialize and grant Ability on the startup, add the following code to the BeginPlay method.

void ASimpleGASCharacter::BeginPlay()

{

Super::BeginPlay(); if ( AbilitySystemComponent && HasAuthority() )

{

if ( StartupAbility )

{

AbilitySystemComponent->GiveAbility( FGameplayAbilitySpec(StartupAbility.GetDefaultObject(), 1, 0) );

AbilitySystemComponent->InitAbilityActorInfo( this, this );

}

}

}

A ThirdPersonCharacter.h doesn’t have BeginPlay declared out of the box, so we should not forget to add these in the header file somewhere in the protected section, above the rest of the methods there.

protected: void BeginPlay() override; void PossessedBy(AController* NewController) override; /** Resets HMD orientation in VR. */

void OnResetVR();

Finally, add the RefreshAbilityActorInfo() method as our character is possessed by a default character controller on the startup.

void ASimpleGASCharacter::PossessedBy(AController* NewController)

{

Super::PossessedBy(NewController); AbilitySystemComponent->RefreshAbilityActorInfo();

}

Make sure your project is set as a Startup Project in VS. Press F5 to compile and run the Editor.

Cutting a Corner

Now let’s do something different from the rest of the tutorials and skip a binding part where we bind the ability activation to the input mappings in our ThirdPersonCharacter.cpp. Instead, we jump directly to our Blueprint class and set it up in an easy way, and here are a few reasons why: