Hello friends,
So, I’m making a turn-based combat prototype, and I’m trying to give abilities to an ability system component.
If I add the ability with the TSubclassOf<> version of the constructor for the FGameplayAbilitySpec, it works OK, but I can’t really assign stuff to the TSubclassOf object.
If I instantiate an ability with the NewObject… and use it for the constructor version of the FGameplayAbiltiySpec that takes a UGameplayAbility*, at the end of the method that gives the ability to the ability system component, I get a crash with a message “Assertion failed: Ability->HasAllFlags(RF_ClassDefaultObject)” from a “check” in the AbilitySystemComponent_Abilities.cpp file in the plugin.
Here is my code with the TSubclassOf version, :
void ACharacterUnit::UseAbilityOnTarget(int32 AbilityIndex, AUnit* InTargetUnit)
{
if (AbilityIndex >= 0 && AbilityIndex < BarAbilities.Num())
{
TargetGASComponent = InTargetUnit->GetGASComponent();
TargetUnit = InTargetUnit;
if (TargetGASComponent)
{
BarAbilities[AbilityIndex]->MeleeStrikeAnimation = MeleeStrikeAnimation;
//AbilitiesInBar[AbilityIndex].GetDefaultObject()->MeleeStrikeAnimation = MeleeStrikeAnimation;
AbilityUsed = AbilitySystemComponent->GiveAbility(
FGameplayAbilitySpec(AbilitiesInBar[AbilityIndex],
1,
static_cast<int32>(AbilitiesInBar[AbilityIndex].GetDefaultObject()->AbilityInputID),
this));
AbilitySystemComponent->TryActivateAbility(AbilityUsed, false);
}
}
}
And the version with the instantiated object:
void ACharacterUnit::UseAbilityOnTarget(int32 AbilityIndex, AUnit* InTargetUnit)
{
if (AbilityIndex >= 0 && AbilityIndex < BarAbilities.Num())
{
TargetGASComponent = InTargetUnit->GetGASComponent();
TargetUnit = InTargetUnit;
if (TargetGASComponent)
{
BarAbilities[AbilityIndex]->MeleeStrikeAnimation = MeleeStrikeAnimation;
//AbilitiesInBar[AbilityIndex].GetDefaultObject()->MeleeStrikeAnimation = MeleeStrikeAnimation;
AbilityUsed = AbilitySystemComponent->GiveAbility(
FGameplayAbilitySpec(BarAbilities[AbilityIndex],
1,
static_cast<int32>(BarAbilities[AbilityIndex]->AbilityInputID),
this));
AbilitySystemComponent->TryActivateAbility(AbilityUsed, false);
}
}
}
The instantiation of the ability I’m doing currently as:
UAbilityBase* Ability = NewObject<UAbilityBase>(this, DefaultAbility);
BarAbilities.Add(Ability);
EDIT: It was me not having set the flags of the ability.
Doing this fixed the issue for now:
UAbilityBase* Ability = NewObject<UAbilityBase>(this, DefaultAbility);
Ability->SetFlags(EObjectFlags::RF_ClassDefaultObject);
BarAbilities.Add(Ability);
Thanks in advance and best regards,
Svetlin