Difference between classes and object instances

Hello everyone.
I m currently making a ability system, where the abilities (that are just a derived class of UObject ) are stored in a TArray so i can change them later and use them ingame.


This is the declaration, since i’m going to use different types of abilities, and I want to create Blueprints for each of them, I use the “tsubclass”, so I can assign them from the inspector view.
The problem comes when I try to do anything with the abilities stored.
image
First of all, If I try to call a public method of the ability class called “castAbility”, I cant, because when I access the array position with the “[ ]” operator, it is treated as a UClass item. But if I try to do a Cast before calling the method, the cast allways fails.
image
The CastAbility method is this:
image
image
Nothing special as you can see. However I cant figure out why I cant call the methods from the character if everything is a UAbility derived o why I cant Cast.
Anything helps, thanks.

The reason the ability is getting treated as a UClass is because it is one.
Your declaration is TArray<TSubclassOf<UAbility>>. This is basically saying “any class that inherits from X can be in this array”. The allowed items are CLASSES, not objects of those classes.

The easiest way to make this work would be to spawn an ability UObject whenever casting the ability, set any variables you need within the ability, and then call CastAbility on it. If you need to store a reference to this object, you can store references to them using the type UAbility* (ie TArray<UAbility*>, or if you want to get fancy with it: TMap<TSubclassOf<UAbility>, TArray<UAbility*>)

1 Like

You can acces without spawning using abilitiesArray[0].GetDefaultObject()->CastAbility(); or:

TObjectPtr<UAbility> AbilityCDO = abilitiesArray[0].GetDefaultObject();

AbilityCDO->CastAbility();

More info:

@rokenrock covered the rest.