Expose variable from C++ component

Hi,

I’m trying to do something pretty simple yet I believe I’m running into a bug with unreal. Basically, I have an InventoryComponent that I’ve made in C++ which has an exposed variable for the capacity of the inventory:

UCLASS(BlueprintType, ClassGroup = (Custom), meta = (BlueprintSpawnableComponent))
class MY_API UInventoryComponent : public UActorComponent
{
	GENERATED_BODY()
private:
	// The capacity of the inventory
	UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Properties", meta = (AllowPrivateAccess = "true"))
	int32 Capacity = 30;
}

Then in my Player.h file I use it like this:

UCLASS()
class MY_API APlayerBase : public ACharacter
{
	GENERATED_BODY()
public:
	// Sets default values for this character's properties
	APlayerBase(const FObjectInitializer& ObjectInitializer = FObjectInitializer::Get());
private:
    UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "PlayerBase", meta = (AllowPrivateAccess = "true"))
	UInventoryComponent* Inventory = nullptr;
}

And in Player.cpp I create it like this:

APlayerBase::APlayerBase(const FObjectInitializer& ObjectInitializer) :
	Super(ObjectInitializer)
{
	// Create the inventory component
	Inventory = CreateDefaultSubobject<UInventoryComponent>("Inventory");
}

Initially it all works great and I can see and edit the capacity in my derived player BP class, but after making changes / recompiling my C++ (I honestly don’t know what causes it) one of two things happen:

  1. The details panel no longer shows when I click on the component in the editor. I can fix this by either rebasing the player back to a Character and then back to my APlayerBase class or by commenting out the call to CreateDefaultSubobject, compiling, uncommenting it, and compiling again.
  2. The value I set for Capacity in the blueprint no longer gets applied in C++. It still shows as whatever I set it to in the details panel but in code it is the default value of 30.

This is the bane of my existence currently. If anyone has any idea what I am doing wrong please let me know. Thanks.

I believe that your UInventoryComponent property in your APlayerCharacter class needs a UPROPERTY(VisibleAnywhere) specifier.

Oops, I copy and pasted it incorrectly. I just updated my post, check it again. I actually already had that there. Sorry about that, good catch though.

Hmm, try making it public?

try removing
Category = “PlayerBase”,
unreal sometimes behaved weirdly for me when I give a component a category

Hmm interesting you say that. I changed the Category to “Character” before reading what you write here (since that’s the category the CharacterMovementComponent is using which works totally fine). After I did that I haven’t seen the issue but the issue is intermittent so I’m going to give it like a week or so before resolving this task. I think the category may have been the culprit…

Actually, I just now had it happen again :confused: I’ll try removing the Category altogether.

That didn’t fix it either :confused: