Access members of a class with a UClass pointer

Hi, it’s me again.

I hope I even ask the question correctly ^^

I’m trying to rewrite my item-system. So far I used an array of pointers to the actual instances of the items, and made them invisible when picked up. This works fine as long as I only interact with existing instances. However it gets a little messy when, for example, I try to give NPCs an inventory before the game has begun. I would need to place the actors in the world, make them invisible and then add them to the array.

So I was experimenting the last few hours, how I could improve this, and worked out how to get the class of an actor and use it to spawn the actor. This works fine, I can store the class of a item on interaction and then spawn a new instance of the class. However, I need to access some of the members of the class, for example Name and Value (For example in my HUD). But I don’t know how to do this without actually having an instance of it.

That’s my array:

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Inventory)
	TArray<class UClass*> InventoryItems;

I tried the following:

AItem* const Item = Cast<AItem>(InventoryItems[0]);
GEngine->AddOnScreenDebugMessage(-1, 5.0, FColor::Green, FString(Item->Name));

But this just crashes the editor. I assume I need to rethink the casting. I just don’t know how exactly it needs to be. I hope you can help me. Thank you in advance :slight_smile:

Hi and thank you for your answer :slight_smile:

I hope you don’t mind me asking a few more things.

The only difference of making it TSubClassOf is that it now can only take Actors that are subclasses of the specified type, right? What is the actual benefit of that?

I’m still having trouble accessing InventoryItems’ members. The problem is not that I can’t get the name of the class. I need to access a few members inside AItem (Name, Value etc.) without having an instance of it. A while ago I read somwhere, that for every class I create, UE4 creates some kind of default instance. I can’t find the post anymore though. If this is true, do I need to find this instance some way to access those values? Or is there an easier way to do this?

I just didn’t have to deal with stuff like this so far, so please apologize :slight_smile:

Hey Hammel,

You’ll want to use the TSubclassOf template for your inventory class array. So something like:

TArray<TSubclassOf<AItem>> InventoryItems

You’re also trying to cast an entry in the classes array to an AItem which will never work. You shouldn’t need to cast if you just want to print out the class name.

If you want to spawn an instance of AItem using an entry from your classes array, you’ll need to call UGameplayStatics::BeginSpawningActorFromClass(…) and feed it a class type.

Hope this helps.

TSubclassOf is safer and will only allow derived classes of AItem to be added to the array.

To access the default object of a class type, you’ll want to do something like:

TSubclassOf<AItem> ItemClass = InventoryItems[0];
AItem* DefaultItem = ItemClass->GetDefaultObject<AItem>();
GEngine->AddOnScreenDebugMessage(-1, 5.0, FColor::Green, FString(DefaultItem ->Name));

This is very helpful. Thank you once again for your help! :smiley: