GAME DEV MASTERCLASS

Launching Grenades in Unreal with Gameplay Ability System: Part 1

An easily-digestible GAS tutorial

Denis Savosin
12 min readNov 18, 2020

--

This is the first tutorial in the series dedicated to Gameplay Ability System. The goal for these tutorials is to be easily digestible, 10 minute-long reads focused on getting a working prototype of a gameplay mechanic on screen as fast as possible. So whether you’re new to Unreal Engine or new to Gameplay Ability System, Designer, Animator, or Technical Artist, these tutorials will help you build your projects faster and stop being afraid of using GAS for your projects.

This series requires little to no knowledge of C++. It requires a basic knowledge of Blueprints scripting and general knowledge about unreal asset types and their use for building gameplay. I tried to cover steps in as much detail as possible and provide GIFs and plenty of images to help you on your way to the result, which is just 20–30 minutes away from now.

I wish you good luck and let’s go!

The end result we will be able to see very soon.

Getting Started

Prerequisites:

The setup for the Gameplay Ability Plugin has been 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:

  • We will learn a new blueprint node which will activate the ability within the character blueprint. It allows us to be more flexible in the ways we trigger our abilities on and off.
  • You may use a different input setup for your project; maybe you decide to play around with controls, so keeping it simple and plugging a pin from one input to another is a decent way to prototype and test things out. Another case if you want to activate your abilities when your character interacts with other non-controllable objects or triggers.
  • After all, it is an excuse to learn a bit about ability Gameplay Tags, and the ways how they control almost everything in the GAS. So, by skipping a boring part, let’s jump into the actual ability.

Launch Grenade Ability

The launching grenade ability will need the following:

  • Launching animation Montage (depending on your project, I’ll likely find that most of the basic gameplay mechanics can be done with a few nodes in the graph and an animation montage, so go team up with your Technical Animator friend)
  • GA_LaunchGrenade blueprint derived from the Gameplay Ability Base class.

To start, go to the convenient content folder of your choice and create a new gameplay ability blueprint by right-clicking, selecting the Gameplay Ability Blueprint option, and choosing the Gameplay Ability base class to derive from. Let’s call it GA_LaunchGrenade.

It will help you later on if you establish the naming convention for your assets early, but I suggest going with the following conventions:

GA_ -Gameplay Ability

GE_ -Gameplay Effect

GC_ -Gameplay Cue (that’s for later)

Open the created blueprint and perform a basic setup by adding some ability logic. For now, it will be a simple print string to show that ability works. And add and assign Ability Tag. You can create any tag you want as long as it will make sense for you.

Ability Tag — the unique identifier for ability which is used for actions like activation, deactivation, prohibition and canceling of abilities, and more.

The Tags topic is massive and I will talk about them in more detail later, but for now, let’s pin this and move on to the fun stuff.

As we skipped the binding part, we are now free to go directly to the character blueprint, open it up, and setup ability activation by listening for the ‘G’ input event on our keyboard. Then perform the following:

  1. Drag an execution pin from the ‘Pressed’ and type “Try Activate Abilities by Tag”. It should suggest the function node of the Ability System Component. Add it to the graph.
  2. We need to supply a Gameplay Tag into the Gameplay Tag Container input pin. Simply drag our the pin, click Promote to Variable, and give it a name of your choice. Compile blueprint and add our Ability Tag as a default value.

3. That’s all! When press G, the event will execute our Try Activate Abilities by Function and will activate LaunchGrenade ability as long as tags are matched. Moreover, you can activate multiple abilities if they share a common Ability Tag. For example, if you come up with the list of abilities that should be activated on a single button press, you can try an “Activate On X button” tag for your ability, in addition to the unique identifier tag. Yes! Ability can be identified by multiple tags.

Well, before it will work, simply go into your character Class Defaults and select GA_ThrowGrenade as a startup ability. Now press Run and observe the Debug Print once the G key is pressed.

Press Run and observe the Debug Print once the G key is pressed. Now it is time to build on top of the basic setup using Anim Montage and Grenade Actor.

Ability content setup

Thankfully Epic Games made their Paragon character assets free on the Asset Store. I think this content is a great starting point for beginners to build their prototypes, especially when it comes to finding the set of montages, character skeletons, and animation blueprints. The Wraith has a nice little montage where the suit shoots the grenade from the built-in launcher. This reminds me of Iron Man’s suit. I’d prefer a hand-throw animation where we could control the grenade release timing, but this content is enough for us to learn the basics of Ability Tasks.

Setup Guide:

  1. Go to the character Blueprint and select character mesh. Replace your SK_Mannequin skeletal mesh with the Wraith from the drop-down menu.
  2. Then up in the Animation settings, select a new Anim Class called Wraith_AnimBlueprint.
  3. Thanks, Epic! We have our character set up and running. Press Play and give it a try.

Now let’s go back to our GA_LaunchGrenade ability, and add our first task called “Play Montage and Wait”. For montage to play select Ability_Q_Fire_Montage anim montage. The asset path is:

Content/ParagonWraith/Characters/Heroes/Wraith/Animations/Ability_Q_Fire_Montage.uasset

The ‘Play Montage and Wait’ gameplay task is one of the most commonly used tools to build the gameplay with GAS. The concept of the Task is simple as “an action which does not block execution flow and provide a set of events based on the result upon task completion”. Simply meaning that you can start a certain task, continue your logic, and then the task is complete (in this case the montage play is completed) it will allow you to “do something else”, like jumping to the next part of ability logic, another gameplay task or simply End ability on completion. You can run multiple Ability Tasks at the same time, just chain them up using the execution pin in the top right corner.

  1. The chosen montage sequence also starts with the launcher fully retracted on the first frame, meaning we don’t need to deal with AnimNotifiers and listening to GameplayEvents in this tutorial. That means that as soon as the task has started and the montage started playing we can spawn our grenade actor using another neat function called ‘Spawn Actor For Gameplay Task’. I think the name is self-explanatory. Later, we will use our Grenade actor here.

Let’s repeat all the steps again:

  1. Add ‘Play Montage and Wait’ task node and select Ability_Q_Fire_Montage
  2. Drag the top execution pin to continue Ability execution flow immediately after montage starts playing, add ‘Spawn Actor For Gameplay Task’ task node.
  3. Let’s finish our ability by calling End Ability when the montage stop playing. Add the End Ability node first, then drag all pins from the ‘Play Montage and Wait’ events like On Finish, On Cancelled, On Interrupted. That means that if montage play ends for whatever reason, the ability ends there. From that point, a grenade actor and the ability will live their own separate lives.
The resulting Gameplay Ability Graph of GA_ThrowGrenade ability

Before moving on to create a Grenade actor, we should finish the setup and get the right spawn location for a Grenade. To do that:

  1. Open Anim Montage and click on the Skeleton tab in the top right corner.
  2. Find Muzzle_03 bone and look up its socket name.
  3. In the Ability Graph call “Get Owning Actor from Actor Info’ — this node returns the owning character of our ability.
  4. Cast it to Character class, do not cast to your Blueprint version, we need a Skeletal Mesh component, which is defined in Character. That way you will get a base class only and will keep asset reference count low. If you cast to your Blueprint class, the asset reference graph will contain all Blueprint class dependencies as well, which is a good practice to avoided.
  5. Get Mesh component and get its Socket Location by providing the Muzzle_03 socket name, plug the location into Spawn Actor Gameplay Task.

Here is how Ability Graph should look:

We are reaching the culmination of the first part of this tutorial. All that is remaining for today is to create a simple grenade projectile and plug it into our ability.

Grenade Actor

Create a new Actor and call it BP_Grenade. Add the following components:

  1. Sphere component. Set scale its scale to 0.1 on each axis. Set a collision preset to Overlap All Dynamic.
  2. Projectile Movement Component. Set Initial Speed and Max Speed to 1000.0 and set Gravity Scale to 0.2.

Now we need to create a function that will launch the grenade forward. For this, we will need a projectile velocity equal players forward direction vector multiplied by the initial speed of the projectile.

Create ‘LaunchInDirection’ function with the following logic:

BP_Greanade/LaunchInDirection function graph.

Now let’s go back to the GA_LaunchGrenade ability and select BP_Grenade as our actor to spawn in the ‘Spawn Actor For Gameplay Task’ node. Task provides us a reference to the spawned actor. On successful spawn, we call the LaunchInDirection function on it and give us a forward vector of our character capsule collision component.

And we should not forget to set our character as Instigator for a spawned Grenade. That means our character is responsible for all damage and gameplay effects caused by the spawned grenade. It is also a shortcut reference to our actor, in case we need to trace back from a grenade to the character.

Finally, for the Task Owner — add reference to Self. Compile, run, enjoy the result!

Result and Key Takeaways

Just in a short time, we were able to build a simple grenade launching ability, learn the basics of the Gameplay Ability System, and how simple it is really to create gameplay mechanics with it. But we just touched the tip of the iceberg! There is a lot more to cover the basics of GAS. And we are certainly going to do that in the next parts of the tutorial.

The key takeaways in this part:

  • Gameplay Ability Plugin requires minimal effort a basic ability to read C++ files to set up your first ability.
  • Most of the abilities can be done in Blueprints using the built-in functionality of GAS and Unreal. Note how we operate with nodes and added zero custom logic.
  • Gameplay Ability is a logic which runs on the owning Actor. It features Gameplay Tags system for simple identification of abilities and setting some rules of what is allowed and prohibited.
  • You’re not necessarily need to bind your abilities to the input. You can grant them and activate them almost from anywhere where you have the access to owning actor and its ability system component.
  • Gameplay Task is a key building block of abilities. They provide simple way to perform actions, build execution chains from multiple tasks and wait for certain events to happen to change the execution flow of an ability or end it.

I hope you enjoyed this tutorial and now able to understand the basics of GAS and realize how easy it is to set up. With time you’ll get to know how cool the system is and how much time it saves in prototyping and building compelling gameplay mechanics. I also suggest you team up with an animator friend, as more core gameplay mechanic can be done by a pair of one designer / technical-designer and an animator. If you are an animator or designer, I hope this article helps you start building your prototypes and experiments in Unreal Engine.

In Part 2 we will cover the topic of Gameplay Effects, expand our Grenade class, and introduce ourselves to the Attributes system.

Until next time!

D.S.

A big thanks to my colleague Oliver Stubenrauch, Sr. Technical Designer at Yager for help in the preparation of this material.

Please follow this link to the original post on denismakes.games.

Follow me on LinkedIn and Twitter.

--

--