Spawning an actor from a blueprint


First I’m aware that this problem seems to be quite common, and I saw many topics over the net explaining how to overcom this problem. But I can’t find where are my mistake(s)!

This is part of a spell system I’m trying to make. I put here only the concerned part, I hope it’s enough. There is no problem during the compilation, but each time I try to launch the editor, it crash right away.


class ADCDH_Character : public ACharacter
	TSubclassOf<class ADCDH_Spell> SpellBlueprint;


ADCDH_Character::ADCDH_Character(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
	// The error is here, when I comment this part, the editor don't crash anymore.
	static ConstructorHelpers::FObjectFinder<UBlueprint> ItemBluePrint(TEXT("Blueprint'/Game/Blueprints/BP_Spell_Fireball.BP_Spell_Fireball'"));
	if (ItemBluePrint.Object != NULL)
		BP_Spell = (UClass*) ItemBluePrint.Object->GeneratedClass;
/* This function spawn a spell actor and launch it */
void ADCDH_Character::Spell()
	ADCDH_Spell* spell = GetWorld()->SpawnActor<ADCDH_Spell>(SpellBlueprint);

Thanks for you kind help

I guess i will tell you a basics of dealing with crashes :stuck_out_tongue:

C++ is compiled directly to bindery machine code which is executed directly by your CPU, because of that if something goes wrong it won’t pop friendly message but crashes. There to ways to deal with them in UE4 easier and harder.

1.Sometimes UE4 on last breath before crash logs if something going wrong which might direcly tell you what cause may crash or at least give you some clues what may happen wrong. Engine also dumps the stack when crash happens to the log. You can find logs in your project directory Saved/Logs/ProjectName.log

2.If logs does not give you out any information you will need to use debugger. In VS start the editor by Debug->Start Debugging, when VS and crash will happen insted of closing editor, VS will jump in and break the code right before the crash and analyze it. It will show expect line where it happens which is already huge help (thru you can read code line from stack dump in logs too :p), i will also show you state of variables in memory (sometimes i buggy), you may see if something is not set etc. If you still can’t figure whats wrong you can put a breakpoint (by clicking on left edge of code editor) on begining of function tha cause a crash and then execute code step by step (F11 Step Into or F10 Step Over) it might give you more clues.

Most common cause of crash is null or wrongly set pointer, for exemple if GetWorld()->SpawnActor<ADCDH_Spell>(SpellBlueprint); fail to spawn the actor for some reason it will return Null pointer instead of pointer to actor, then on spell->Cast() you gonna have a crash because you trying to call function from null pointer, object that does not exist. So when you discover on which line you have a crash, check if whole varable chain leading to function call have pointers set correctly. Other common cause is invalid cast, when you try to cast object to incompatible type.

as i heard static ConstructorHelpers::FObjectFinder<UBlueprint> ItemBluePrint(TEXT(“Blueprint’/Game/Blueprints/BP_Spell_Fireball.BP_Spell_Fireball’”)); will cause you crash when you gonna package the game as UBlueprint exist only in editor (it as anwser from UE4 dev on AndwerHub, it had solution can’t find it now :p). Best is to avoid referencing Blueprint from C++ and design your code so it can handle things just by using base class and send blueprint objects from game/editor side. For example it seems you want to make Fire spell a default spell for character, insted of doing that in C++ you can make property in which you set default spell (or whole inventory) in your extended AWorldSettings class (which controls level settings, when you click “World Settings” editor you see exposed properties of that class which are saved to the level files), you can also do that in you base character class which you then extend from blueprint and set default inventory there. You can also handle giving spells via blueprint by making spell giving nodes (thats easiest way :p)

Thanks for the information on the crashes, very informative :slight_smile:

It seems that the cause of the crash is an invalid cast during the FObjectFinder.

My idea was to have a basic spell class with all function required to do spell stuf (A function which apply damage to a target, a function which heal a target, etc…), and an event LaunchSpell. Within the editor I create blueprint based on this basic spell class and use the function to make any spell needed. For exemple to do a fireball I just have to add to my blueprint graph the function damage and the function fire, and those functionswill be launched when the event LaunchSpell will be sent from C++ by the player (when he input LMB, for exemple).

The problem was to store the spell somewhere so the character can use and change them easily. I went for an array containing all the path of the blueprints for the spells and, when he need to fire a spell, the character use those path to spawn a spell from the blueprint and then call the event LaunchSpell.

I don’t understand when you say “using base and send blueprint objects from game/editor side” Do you mean that I should have my character class as a blueprint with its property editable in the editor? I would have to store the blueprint spells directly from the editor but can I have my character as a blueprint?

What do you mean by spell giving nodes?

Once again thanks for your help :slight_smile:

Little update for people who happen to find in this post and have the same problem I had :slight_smile:

In my Character.h class I referenced a

TSubclassOf<class Spell> BP_Spell

And, in my Character.cpp I used :

Spell* MySpell = GetWorld()->SpawnActor<ADCDH_Spell>(BP_Spell)

And voila.

Using a TSubclassOf<Spell> allow me to create any blueprint needed (given they are derived from Spell)

And there is the answer that set me on the good tracks : [Solved] How to typecast TSubclassOf<>? - UE4 AnswerHub

Yes, if you gonna make default inventory settings in character, it might be also useful when you do enemies based of character class. But you could do it in world settings if you want to make it more map based.

Spell giving nodes i mean blueprint nodes that gives spell to player or enemy inventory, best is if you make subsystem for spell managment in C++ which is operable in Blueprint, this way you will help you avoid using ObjectFinder