-
Notifications
You must be signed in to change notification settings - Fork 1
Getting Started ‒ Implementation
To implement the Gameplay Containers Plugin and add an inventory component to a class, follow these steps:
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<UGameplayInventoryComponent>(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);
}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
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<UGDAbilitySystemComponent>(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;
}void AGCDemoCharacter::PossessedBy(AController * NewController)
{
Super::PossessedBy(NewController);
if (AbilitySystemComponent)
{
AbilitySystemComponent->InitAbilityActorInfo(this, this);
}
// ASC MixedMode replication requires that the ASC Owner's Owner be the Controller.
SetOwner(NewController);
}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.
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.