Making a struct in C++ that works in BP

I am trying to make an inventory struct as a start to learning C++. The problem is that I can get it to show up in BP but I can’t add anything to it and I’m not sure why. All the things I have tried aren’t working.

H



#include "CoreMinimal.h"
#include "ItemData.h"
#include "MyStructs.generated.h"


/**
 *
 */
USTRUCT(BlueprintType)
struct FInvItem
{
    GENERATED_BODY()

    public:

    FInvItem();

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Inventory")
    UItemData* Item;

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Inventory")
    int32 SlotNumber;

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Inventory")
    int32 Amount;
};



CPP



#include "MyStructs.h"
#include "MyProject.h"

FInvItem::FInvItem()
{
    this->Item = NULL;
    this->Amount = 0;
    this->SlotNumber = 0;
}


The declaration seems to be just fine. How are you trying to access the struct in blueprints?

I’m just trying to add the struct as a variable. Do you have to do anything else to use it?

Using BlueprintType ustruct specifier should let you use it as a variable type in blueprints, so it should work just fine.

Hmm it apparently decided to work after I restarted the engine.

I’m glad to hear that!

I have found that whenever you modify a struct or enum in C++, you have to restart the editor for it to recognize the changes in blueprints.

Don’t keep editor open if you have to change + recompile anything on header files.

Hot reload only can be trusted when you edit code inside existing functions

Slightly off topic but to the OP. I am working on an inventory system right now and have been exploring/researching a large amount of existing inventory systems.

I was curious what kinda of data/variables was in your UItemData class?

Thanks!

It inherits from the DataAsset class and has the item name, weight, if it stacks etc. Then I can have other item classes inherit off that for different variables for equipment, potions, ammo or whatever.

+1 to restarting the editor after making changes in C++