Skip to content
This repository was archived by the owner on Aug 21, 2024. It is now read-only.

Getting Started ‒ Implementation

Nazake edited this page Apr 8, 2024 · 10 revisions

Implementing Gameplay Containers Plugin

Note: This plugin is using the new replicated sub objects list, that was introduced in UE 5.1, therefore it requires that to be enabled by default otherwise it will not replicate properly.

To implement the Gameplay Containers Plugin and add an inventory component to a class, follow these steps:

Add the Inventory Component to a Class

Assuming you have a custom class (e.g., ACharacter or APawn) that needs an inventory, you'll need to create an inventory component and attach it to your class.

Here is an example using C++:

// GCDemoCharacter.h

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "Core/Inventory/InventoryComponent.h"
#include "GCDemoCharacter.generated.h"

UCLASS()
class YOURPROJECT_API AGCDemoCharacter: public ACharacter
{
    GENERATED_BODY()

public:

    AGCDemoCharacter();

    virtual ELifetimeCondition AllowActorComponentToReplicate(const UActorComponent* ComponentToReplicate) const override;

protected:

    UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Character|Inventory", meta = (AllowPrivateAccess = "true"))
    UInventoryComponent* InventoryComponent;

};
// AGCDemoCharacter.cpp

#include "GCDemoCharacter.h"

AGCDemoCharacter::AGCDemoCharacter()
{
    // NOTE: Need this for containers on a player controller
    // bAttachToPawn = true;

    // Create the Inventory Component
    InventoryComponent = CreateDefaultSubobject<UInventoryComponent>(TEXT("InventoryComponent"));

    // If you want to use this for multiplayer
    InventoryComponent->SetIsReplicated(true);
}

ELifetimeCondition AGCDemoCharacter::AllowActorComponentToReplicate(const UActorComponent* ComponentToReplicate) const
{
	if (ComponentToReplicate->IsA(UInventoryComponent::StaticClass()))
	{
		return COND_None; // Or any condition you like
	}

	return Super::AllowActorComponentToReplicate(ComponentToReplicate);
}

Register With Gameplay Ability System

You need to register the inventory component with an ability system component for it to be able to activate abilities and basically use the ability system

Here is an example using C++ where the ability system lives on the player state class:

AGCDemoPlayerState::AGCDemoPlayerState()
{
	// Create ability system component, and set it to be explicitly replicated or not if you are not making a multiplayer game
	AbilitySystemComponent = CreateDefaultSubobject<UAbilitySystemComponent>(TEXT("AbilitySystemComponent"));
        AbilitySystemComponent->SetReplicationMode(EGameplayEffectReplicationMode::Mixed); // Use Full for single player games
	AbilitySystemComponent->SetIsReplicated(true);

        // AbilitySystemComponent needs to be updated at a high frequency.
	NetUpdateFrequency = 100.0f;
}
// Server only
void AGCDemoCharacter::PossessedBy(AController * NewController)
{
	Super::PossessedBy(NewController);

	AGCDemoPlayerState* PS = GetPlayerState<AGCDemoPlayerState>();
	if (PS)
	{
		// AI won't have PlayerControllers so we can init again here just to be sure. 
                // No harm in initing twice for heroes that have PlayerControllers.
		PS->GetAbilitySystemComponent()->InitAbilityActorInfo(PS, this);
                InventoryComponent->RegisterWithAbilitySystem(AbilitySystemComponent);  
	}
}
// Client only
void AGCDemoCharacter::OnRep_PlayerState()
{
	Super::OnRep_PlayerState();

	AGCDemoPlayerState* PS = GetPlayerState<AGCDemoPlayerState>();
	if (PS)
	{
		// Init ASC Actor Info for clients. Server will init its ASC when it possesses a new Actor.
		AbilitySystemComponent->InitAbilityActorInfo(PS, this);
                InventoryComponent->RegisterWithAbilitySystem(AbilitySystemComponent);  
	}

}

Grant Your Ability System The Gameplay Container Abilities You Created

Where ever you grant abilities in your game, you can include the gameplay container abilities to be added when the game starts or add them dynamically when ever you want the player to be able to use containers

NOTE: those abilities are shared, which means if granted a player will be able to use all containers they own (eg. Inventory, Hotbar, etc...) For containers they don't own, you need to explicitly call RegisterUser to start using another container, and UnregisterUser when your done and don't want to use anymore.

Here is an example using Blueprints:

In this picture, I created a variable that holds all the abilities:

And on begin play you can just give those abilities to the ability system:

Here is a simple example how to register a player to be able to use another container that they do not own based on simple collision checks (this might be different if you have your own interaction system, you will need to register the user when interaction starts and unregister them when it ends):

Configure Inventory Component

After adding the Inventory Component, you can customize its properties in the editor or during runtime. Adjust settings such as maximum capacity, supported item types, and overflow handling based on your game's requirements.

Interact with the Inventory

Now that you've added the inventory component to your class, you can interact with it in various ways. For example, you can add items, remove items, check the inventory's content, and respond to events triggered by the inventory system.

Refer to the Gameplay Containers Plugin documentation for detailed information on how to use the inventory component and implement specific functionalities.

Remember to build and compile your project after making these changes.

This is a basic setup, and you can extend it based on your specific needs and the features provided by the Gameplay Containers Plugin.