Download

Add to array not working

I can spawn the actor without a problem, however, it won’t be added to the array

void UCharacterExtensions::AddStarterSpells()
{
UWorld* const World = GetWorld();
if (World)
{
FActorSpawnParameters SpawnParams;
FVector Loc = FVector(0.0f, 0.0f, 0.0f);
AMasterSpell *spell = World->SpawnActor<AMasterSpell>(StarterSpells[0], Loc, FRotator(0.0f, 0.0f, 0.0f), SpawnParams);
ActiveSpells.Add(spell);
}
}

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = ActiveEffects)
TArray<TSubclassOf<AMasterSpell>> StarterSpells;

UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = ActiveEffects)
TArray<AMasterSpell*> ActiveSpells;

This is the code of the function i’m using.
PS: Starter spells contains a blueprint class.
It’s being spawned , but it’s not being added to the ActiveSpells

Likely it’s trying to do some cast and failing. What is ActiveSpells an array of?

The spells that are in the action bar.
To explain a little bit further, i’ve done the spells and spell cast, aswell as widgets with blueprints(action bar and such). But i realised doing the way i was doing would give me problems in the future, so i started defining what i realised would cause problems in the future in c++.
Currently, the c++ is not doing anything, but fixing it after finishind this Add part would be a matter of changing a few nodes.

You likely need to do



SpawnActor<AMasterSpell>(StarterSpells[0]->GetClass(), ...);


Right now I’m pretty sure SpawnActor is returning null since the cast is failing (but it still spawns properly).

I found the issue. I was checking if it was valid through BP, but since i was starting to define things in c++ that were from BP, i actually was checking the value of the bp variable and not the c++ var, that had a very similar name.
But i changed the code to use ->GetClass() before i realised that, and it woudln’t work, the way i was doing was the right one.