When I try to set a blueprint default variable, it keeps resetting it to ‘none’. I’m bad at explaining stuff so I recorded it:
I’ve never seen anything like this before. this is my code in the cpp header file:
Am i doing something wrong?
protected:
//Item to add to inventory
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = “Inventory”, meta = (AllowPrivateAccess = “true”))
UItem* ItemToAdd;
I’ve tried:
moving the include item header to cpp file and making UItem* to class UItem in this header file.
trying various combinations in the UPROPERTY parameters
Changing UItem to UObject, which allowed me to set the default variable, but it I can’t use it that way because I need to downcast it back down to use it as an item, which apparently doesn’t work.
Are you trying to add the instance of BP_TestCPPItem to a static class? I’m afraid you can’t do that. Place an instance of this BP class into the level, and there you’ll be able to select the instance of the second BP class.
UItem* is a pointer to instance, and generally at the time of the editor(before the world starts) there not that much instances of classes you need.
I guess you want to refer to some predefined item bp? Then you should define your variable type as TSubclassOf<UItem>, to work with item class, not particular instance.
From there the next step will depends on your goals, but note that every uobject class has a “ClassDefaultObject(CDO)”, so you can get bp’s default values from it.
oh right, I totally forgot about that tsubclassof, thanks. I’m trying to add it to a Tarray in a blueprint event but now it became a class reference instead of an object reference. can you help me convert it for one last time if you don’t mind?
You can “Construct Object From Class” to spawn object instance based on class you pass. “Outer” is an object owner, you can set it to an inventory or a character.