In my character’s beginplay I spawn a BaseSpell actor, which is a blueprint actor that is a subclass of a C++ class called Spell.
void ADMagicCharacter::BeginPlay()
{
Super::BeginPlay();
//SetupSpellList Move to GameMode
UWorld* const World = GetWorld();
if (World)
{
FActorSpawnParameters SpawnParams;
SpawnParams.Owner = this;
SpawnParams.Instigator = Instigator;
TSubclassOf<class ASpell> SpellClass;
FVector tVect = FVector(0,0,0);
FRotator tRot = FRotator(0,0,0);
SpellClass = Spells;
ASpell* CurrentPrimarySpell = World->SpawnActor<ASpell>(SpellClass , tVect, tRot, SpawnParams);
if (CurrentPrimarySpell != NULL)
{
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Green, CurrentPrimarySpell->GetName());
}
}
}
Spells is a variable that can be set in the editor.
I set Spells for the character to BaseSpell, a blueprint subclassing Spell.
When play testing, the DebugMessage immediately after spawning CurrentPrimarySpell correctly displays the name of CurrentPrimarySpell (BaseSpell_C_#).
Later in the C++ code, I have an OnFire Function that calls another function within CurrentPrimarySpell
And another DebugMessage that displays a red message “NULL” if CurrentPrimarySpell is NULL.
void ADMagicCharacter::OnFire()
{
if (CurrentPrimarySpell == NULL)
{
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, "NULL");
} else
{
CurrentPrimarySpell->OnFire();
}
}
For some reason, OnFire always shows CurrentPrimarySpell as NULL and displays the red DebugMessage.
There is no other references to CurrentPrimarySpell, and looking in the World Outliner during playtest, shows the BaseSpell actor is still there.
Why is CurrentPrimarySpell becoming NULL, and how can I prevent it?