Skip to content

Implementing Weapons

Weapons can be represented in many ways in games. The system is defined to support multiple designs, including systems without any weapons at all. Here are some examples of design choices compatible with the system:

  • Hand-to-Hand combat.
  • The character has weapons, which are part of the main character mesh itself.
  • The character has weapons, which are represented by meshes attached to it.
  • Weapons are represented by Actors, attached to the character.

There's no lock-in and you don't need to decide or stick to one of these. Once again, the system supports all these strategies and you can mix them as needed.

For the first scenarios, there's not much else that needs to be done right now and you can continue to the Melee and Ranged Combat sections. They will go into more details regarding the setup to implement such functionalities.

However, if your weapons are implement as Actors, then please read on to know more about how these can be integrated into the system, via the Weapon Manager Component.

Here's how weapon actors are managed and integrated to the system.

    sequenceDiagram
        ExternalComponent->>WeaponManager: weapon query
        activate WeaponManager
            WeaponManager->>WeaponManager: find weapon
            WeaponManager->>ExternalComponent: weapon actor
        deactivate WeaponManager

Weapon Implementation

First, any actor representing a weapon must implement the ICombatWeaponInterface. This interface provides is used to make a weapon "visible" to the system, but won't interfere with the way it operates, since this is something specific to each project. As usual, this interface can be implemented in Blueprint or C++.

A weapon's visibility is defined by the presence of this interface and the Gameplay Tags used to define the weapon, with characteristics that are relevant to your weapon and where it's equipment, for example Sword, Crossbow, LeftHand, RightHand, etc.

You can also use the base Actor, ANinjaCombatWeaponActor. It provides the structure to define Weapon Tags via the class' default properties. This class is very lean and should be a good "white canvas" for your system.

Interface versus Base Class

Both are valid choices. Usually you'll have to implement the interface if you already have a class hierarchy for your weapons, which is something that could happen if you are using an Equipment System.

Weapon Functionality

Concepts such as Melee and Ranged combat are defined by their own interfaces and functionality. These are separate from the Weapon Interface, but can (and most likely will) be added to Weapon Actors, as needed.

To wrap it up, let's create a Sword using the base Weapon Actor.

  1. First, create a new actor, extending from the ANinjaCombatWeaponActor.
  2. Feel free to add a Static or Skeletal Mesh component, adding any asset you find appropriate.
  3. For Weapon Tags, here we'll define a weapon type and a slot that this weapon occupies, in this case, we are using the tag Inventory.Container.Primary.

Keep in mind that these tags are defining an equipped weapon, so the Gameplay Tags should reflect this, and also where a weapon is equipped, in case you can have two weapons of the same type at the same time.

Weapon Example

Additional Weapon Interfaces

Like mentioned above, the main interface that defines a weapon in the system is the ICombatWeaponInterface. This interface will be used by the Weapon Manager Component, which we will discuss below, to find/interact with weapons.

However, there are two other interfaces that you should know about. They may or may not be used in your weapons, depending on your system design. These are the ICombatMeleeInterface and ICombatRangedInterface

The ICombatMeleeInterface is responsible for providing the Mesh used for Melee Scans and you can learn more about this functionality in the Melee Combat page. As for the ICombatRangedInterface, this interface is responsible for providing the Mesh from which a projectile should be launched and you can learn more about this functionality in the Ranged Combat page.

Add them as you need

These interfaces are not implemented by the ANinjaCombatWeaponActor as the system shouldn't assume that design. If your weapons are indeed performing melee scans (like a sword) or launching projectiles (like a staff), then you should implement these interfaces as needed.

Weapon Manager Component

To access weapons, the Weapon Manager Component is used. It implements the ICombatWeaponManagerInterface, with the important GetWeapon function.

This function receives the Manager requesting a weapon and the FGameplayTagQuery representing the search. Here is were the Weapon Tags come into play, as the query is applied to them to determine which weapon is being requested. It returns the AActor representing the weapon.

The system provides a default repository, UNinjaCombatWeaponManagerComponent, with a simple, replicated, array-based weapon storage. You can implement any component, such as your own Equipment or Inventory Manager, by implementing the appropriate interface, ICombatWeaponManagerInterface and defining it in the Settings Page.

When do I need a custom component?

A custom component might be necessary if you want to connect the Combat System to a anoter system managing weapons (or equipment) source, such as an Inventory or Equipment System.

Weapon State has to be managed elsewhere!

The Combat System deals with a weapon that is considered "equipped" by the character and is added to the repository. Actions such as equip or holster weapons are not handled by this component.

This means that any weapons added to this default implementation are considered available for melee scans and projectiles. If you need to manage the equipment state, this is where you'll have to add and integrate an Inventory or Equipment Manager!

Weapon Summary

Here's a summary of all the types that were introduced in this section:

Type Description
ICombatWeaponInterface Interface that defines a weapon and how to identify it.
ICombatMeleeInterface Interface that that can be added to your weapons if they are performing melee scans.
ICombatRangedInterface Interface that that can be added to your weapons if they are launching projectiles.
ACombatWeaponActor Base implementation of WeaponInterface. Implements ICombatWeaponInterface.
UCombatWeaponManagerComponent The default Weapon Manager used by the system.
IWeaponManagerInterface Interface that defines the Weapon Manager.