Spell Casting with Blueprints via c++

Dear Unreal Community,

I am currently working on a 3rd person rpg game as part of my final year project at university and I have hit a snag when it comes to spell casting.

my character class has a variable type UBlueprint (based on class ASpell) which represents the currently equipped spell when I press mouse button I call CastSkill function in my character class which cast the currently equiped spell blueprint as ASpell and calls the ASpells class function CastSkill and passes in character location and rotation :

void ASkill::CastSkill(FVector Location, FRotator Rotation)
GetWorld()->SpawnActor<UBlueprint>(VisualEffect->GetClass(), Location, Rotation, FActorSpawnParameters());

VisualEffects is a variable type UBlueprint that represents the spell projectile.

during the spawning i get an error “Access violation reading location” and crash

Any ideas on how to solve this or what I’m doing wrong would be greatly appreciated. If needed I can post or explain more of my code.

Thanks in advance.

That error means one of your pointers doesn’t point to anything. What you can do is run the editor in debug mode from Visual Studio and set a breakpoint on that line. Mouse over each variable to make sure that it has an expected value. My guess is that your VisualEffect pointer is null. If you don’t want to do a debug, you can do a simple test case by writing if(VisualEffect) { GetWorld()->SpawnActor<…>(…)); } and if it doesn’t crash, you know that pointer isn’t pointing to anything.

I managed to figure it out. for some reason the “GetClass()” function was causing the crash. so what I did was I combined the spell and the visual effect into a single blueprint (that still caused a crash when i called this->GetClass()) so before I cast the currently equipped spell into ASpell class I grab GeneratedClass from the spells blueprint and pass it into the CastSkill function and that works.