Skip to content

Faction Component

The Faction Component is the backbone of the system. It's responsible for storing Faction Memberships and providing functions to maintain these memberships.

Assign the Component to a Member

The NinjaFactionComponent can be assigned as you'd expect, either in your Pawn/Character Blueprint or using C++.

Adding the Faction Component

#pragma once

#include "CoreMinimal.h"
#include "Character.h"
#include "FactionCharacter.generated.h"

class UNinjaFactionComponent;

UCLASS()
class PLUGINLABS_API AFactionCharacter : public ACharacter
{

    GENERATED_BODY()

public:

    AFactionCharacter(const FObjectInitializer& ObjectInitializer);

private:

    /** Faction Component. */
    UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Components", meta = (AllowPrivateAccess = true))
    TObjectPtr<UNinjaFactionComponent> FactionComponent;

};
#include "FactionCharacter.h"
#include "Components/NinjaFactionComponent.h" 

AFactionCharacter::AFactionCharacter(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
    FactionComponent = CreateDefaultSubobject<UNinjaFactionComponent>(TEXT("FactionComponent"));
}

Implement the Member Interface

The FactionMemberInterface can be implemented in the Blueprint or C++. It provides fast access to the Faction Component, since it may be constantly used by Controllers.

Optional Interface

Implementing this Interface is optional as long as you always retrieve component using the provided Function Library or Subsystem. It's however recommended to avoid iterating over Actor Components.

Implement the Faction Member Interface

#pragma once

#include "CoreMinimal.h"
#include "Character.h"
#include "Interfaces/FactionMemberInterface.h"
#include "FactionCharacter.generated.h"

class UNinjaFactionComponent;

UCLASS()
class PLUGINLABS_API AFactionCharacter : public ACharacter, public IFactionMemberInterface
{

    GENERATED_BODY()

public:

    AFactionCharacter(const FObjectInitializer& ObjectInitializer);

    // -- Start Faction Member Interface implementation
    virtual UNinjaFactionComponent* GetFactionComponent_Implementation() const override;
    // -- End Faction Member Interface implementation

private:

    /** Faction Component. */
    UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Components", meta = (AllowPrivateAccess = true))
    TObjectPtr<UNinjaFactionComponent> FactionComponent;

};
#include "FactionCharacter.h"
#include "Components/NinjaFactionComponent.h" 

AFactionCharacter::AFactionCharacter(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
    FactionComponent = CreateDefaultSubobject<UNinjaFactionComponent>(TEXT("FactionComponent"));
}

AFactionCharacter::GetFactionComponent_Implementation() const
{
    return FactionComponent;
}

Default Memberships

Once the Component has been assigned to your characters, you can determine the default Memberships, which will be granted by default when the game starts.

A default Faction Membership is defined by the following attributes:

Attribute Description
Faction Data Data Asset representing the Faction
Main Faction Is this to be considered the Main Faction?
Reputation Initial amount of reputation
Main Factions

Main Factions are important when a character has more than one Faction. Make sure to only have one Main Faction in your character, as multiple Main Factions are not supported by default, and may lead to odd behaviors.

Assign Factions to the Component

The most straightforward method to assign Factions is to grant them directly to the Faction Component assigned to the character (Faction Member).

Default Memberships (Component)

  1. In the Components Panel, select the Faction Component.
  2. In the Details Panel, look for the Faction Member category and add Default Factions.

Assign Factions via the Faction Member Interface

Alternativelly, if you want to implement some logic to decide which default Faction Memberships will be granted to each character, it's also possible to allow the Member Interface to provide default Factions.

Default Memberships (Interface)

  1. Create a variable to host the default factions in the Character.
  2. Configure the Factions appropriately.
  3. Implement the Get Default Memberships Function from the Member Interface, providing the default factions.
Merged Memberships

Memberships granted by this interface are merged with the ones provided to the Component directly and duplicates, determined by the Faction Tag, are discarded.

Assign Factions via Gameplay Features

A third option is to use configure factions via Game Features. Using the same action, you can configure multiple factions, their reputation and configure the main one.

Default Memberships (Feature)

  1. First, make sure that this module is properly enabled, as per the official documentation, which includes
  2. Also make sure that you are using Characters, Pawns and Controllers compatible with this functionality!
  3. In your Game Feature Data, add the "Add Faction" Action and configure it appropriately.
Optional Module Configurations

Optionally, you can add the Faction Data Asset to the list of Assets in the module setup and also add the Faction Component to specific actors, using the "Add Component" Action.

Faction Member Implementation

If you configure the Feature so it will also grant the Faction Component to your actors, consider adjusting your interface (or base class) to find and cache the component and then return it via the interface method, as usual.

Faction Maintenance

The Faction Component provides many ways to maintain Factions and Reputations. Each one of these functions are fully documented in the code, so you can have all necessary details from the Engine Editor or IDE, while working with them.

Function Description
GetFactionMemberships Retrieves all Faction Memberships.
GetMainFactionMembership Retrieves the Main Faction Membership.
GetFactionMembership Retrieves Membership data for a certain Faction.
AddFaction Joins a new Faction.
AddReputation Adds a specific reputation from a source.
AddReputationFromSource Adds all eligible reputations from a certain Faction Source.
SetMainFaction Set a Faction as the Main Faction. Clears any previous one.
RemoveFaction Removes a Faction.
HasMembership Evaluates if this character is member of a certain Faction.
GetReputationTier Provides the Reputation Tier for a given Faction.
GetReputationRewards Provides rewards in case the Owner is also a Faction Source.

Delegates

If you have functionalities that must be notified of changes in Faction Memberships, you can subscribe to one of the available Faction Delegates.

Delegate Description
OnFactionAdded The Member has joined a new Faction.
OnFactionRemoved The Member has left a Faction.
OnMainStatusChanged The Main Faction has changed. Invoked for new and old Main Factions.
OnReputationChanged A Reputation has changed (increased or decreased).
OnTierChanged A Reputation Tier has changed. Usually follows reputation changes.