Download

Inventory System: Struct of Items or Array of classes?

Hello!, I recently started to work on a inventory system but im unsure of which method to use for the actual creation and storing of items…

On my initial iteration i just had an array of “Item” Actors but since the items needed to be spawned in the world to be able to access any of their properties and Were also garbage collected every 60 seconds i ditched this method

Now im trying to decide if i should use either a Array of a Struct of items or an Array of item classes

Struct of Items

If i went ahead and used a struct to handle all my items i would be able to easly access all of their properties without the need of spawning/despawning and no more troubles with GC either…
The only problem i can see is that if i want to display the items in the world id need to make an Actor class with a struct that holds all the properties of the Item itself

Array of Classes

On the other hand instead of having a array of Actors in the player inventory, i could use a array of item classes instead , Spawn its blueprinted class when i need to access any of its members ( Item Name, Weight etc ) and de-spawn it when not needed
Again the only problem i can see is that for someting like Displaying the items in a inventory screen id need to spawn all the items and loop trough them since im nearly sure you cant access a Actor class properties without spawning it first?

So tl:dr; I Would really appreciate some advice on which method would work best … Thanks!

I would approach it in a method like this.

Make each item class have a structure. The structure should have variables that main items class needs to have. And also have functions to spawn the item then edit all the item variables to correspond to the variables present in the structure. It also should have have a pointer to the spawned structure. and a bool variable to check if the item is spawned or not.

The inventory class would be an array of those item structures instead of the item. That way, you can change the variables of the structure without actually spawning anything. When you need to spawn the item, the structure spawns the item and changes the item variables to match the structure then make the pointer in the structure store the item that is spawned.

Hey, thanks for the reply!, So if i went for a struct based approach instead … Someting like this then?

// Random pseudo code from player character


#include "RGFpsItemBase.h"
#include "RGFpsCharacter.generated.h"

UCLASS(config=Game)
class ARGFpsCharacter : public ACharacter
{
	GENERATED_UCLASS_BODY()


	UPROPERTY()
	TArray<FItemDataStruct> Inventory;

};

// Random BASE item class code



#include "GameFramework/PlayerController.h"
#include "RGFpsPlayerController.generated.h"



USTRUCT()
struct FItemDataStruct
{
	UPROPERTY()
	FString ItemName;

	UPROPERTY()
	int32 ItemValue;

	UPROPERTY()
	UTexture2D ItemIcon;



};
/**
 * 
 */
UCLASS()
class RGFPS_API ARGFpsItemBase : public UObject
{
	GENERATED_UCLASS_BODY()
	
};


So now id have a array that can hold the struct, the base class that defines the struct and now i would only need an actor that also uses the struct that serves as the “item” ?

// Random code for what would be the basic actor class


#pragma once

#include "RGFpsItemBase.h"
#include "RGFpsPlayerController.generated.h"


/**
 * 
 */
UCLASS()
class RGFPS_API ARGFpsItemActor : public AActor
{
	GENERATED_UCLASS_BODY()

	UPROPERTY(EditAnywhere,BlueprintReadWrite,Category = Item)
	TArray<FItemDataStruct*> ItemInfo;

	// mesh, model etc
	
};


So i would end up with a array of structs in my player character that acts as the “Inventory”, And now i would be able to display each item information without the need of spawning it first, as well if i wanted to spawn the item i just spawn the base actor class, pass in the struct corresponding to the requested item and thats about it right?

Why don’t you define the structure in the item class i.e. ARGFpsItemActor instead of using another class?

Ah right, my bad so well besides that this is very much the best method to go for right?

Cant say its the best method but I guess it solves both the problems you are currently facing so yeah, its good unless this gives you any problem.

The way I would do it is to have objects representing each item base like: coin, sword, shield, etc. Then in the inventory you would have a struct which includes a pointer to that item base and an integer representing the quantity of the item.

Things like the item name and icon will go in the item base object, while only data unique to a particular instance, such as the quantity would go in the struct. Whether or not a struct is sufficient or you need the item instance to be a class depends on how much data it holds. If it’s just a quantity and a pointer to the item base, then a struct is fine. If you’re going to have a larger amount of data depending on how the game or player might be able to customize a particular item instance then it would make sense to use a class.

This is basically pseudo code so syntax might not be correct:



UCLASS()
class UItemBase: public UObject
{
	UPROPERTY()
        FString Name;

        UPROPERTY()
        uint32 Value;

        UPROPERTY()
        UTexture2D* Icon;
};

USTRUCT()
struct FItem
{
        UPROPERTY()
        UItemBase* Base;

        UPROPERTY()
        uint32 Quantity;
};

UCLASS()
class AMyActor: public AActor
{
	GENERATED_UCLASS_BODY()

	UPROPERTY(EditAnywhere,BlueprintReadWrite,Category = Item)
	TArray<FItem> Items;
};


You could then define item bases with something like a data table described here: https://docs.unrealengine.com/latest/INT/Gameplay/DataDriven/index.html