Item class: static Texture but still keep blueprint access?

Hi, I am currently working on a simple warcraft 3 like inventory. Currently, I have a base c++ ItemBase class, which consists of a UTexture2D* for the item image and a gameplay ability class reference, which defines what the item does. Both use the UPROPERTY so that I can set them in the BP editor. However, my inventory currently should be just an array of subclass references of this ItemBase class. Any real item than derives from the itembase class and its class already defines what it does, i.e., I will never have different swords or something, just a single sword.
Now I cannot receive the items icon itself, if I use the vector of class references in my inventory class, because then I would have to turn the UTexture2D* into a static variable. These, as far as I know, cannot be set from the blueprint editor, which would really suck. I could turn to an array of actual instances that are stored in the inventory’s array of items, but this in my opinion would be a slightly worse design.
Is it possible somehow to do both, use a list of class references in the inventory, receive the class’s UTexture2D* reference (this MUST use a static in some way) while keeping the convenience of setting the texture in the BP editor? Thank you for any hint :slight_smile:

If I understand you correctly, I guess what you desribe is very much possible. If you have the subclass of your item, you can get your texture from the default object.

UCLASS(Abstract)
class MYPROJECT_API UMyItemClass : public UObject
{
	GENERATED_BODY()

public:
	UPROPERTY()
	TObjectPtr<UTexture2D> Texture;
};

void Function(TArray<TSubclassOf<UMyItemClass>> ItemClasses)
{
	for (TSubclassOf<UMyItemClass> Element : ItemClasses)
	{
		Element->GetDefaultObject<UMyItemClass>()->Texture; // this will be what you have defined in your BP child class
	}
}

Thanks, but it is not quite what I want, because I want to be able to get the Item’s Texture in blueprints, i.e., the Texture of the concrete item class derived from my ItemBase class.

Here is my ItemBase class


UCLASS(Abstract)
class REALTIMESTRATEGY_API ARTSItemBase : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	ARTSItemBase();

	UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
	TSubclassOf<UGameplayAbility> Ability;


	UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
	TObjectPtr<UTexture2D> Icon;


protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

};

All my attempts to adapt your solution failed for me unfortunately.

Similar should work in BPs as well.

If that’s not what you want, I probably misunderstood your question.

1 Like

Thank you so much, this appears to be EXACTLY what I was looking for :slight_smile:

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.