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.
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.
The CastAbility method is this:
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*>)