Crashing - Adding Blueprint Object to C++ TArray

Let me start with my objective in case someone has a better solution than the one I am trying:

I am attempting to create a spell casting system within Unreal 4. When the player left clicks, they should try to cast the spell from the SpellArray with the matching index. The “spell” class (AProjectBlackSpell; inherits from Actor) is intended to store the variables such as the spell’s damage, cast time, cooldown time, etc. The projectile is created when the spell is “cast”, and the spell passes the projectile the pertinent variables. I set it up like this because we have a customization system planned for down the road (such as doubling the damage of the projectile, but doubling the cooldown as well). I intended to use the AProjectBlackSpell class as an abstract class for blueprints to inherit from.

AProjectBlackCharacter is the character class (And BP_ProjectBlackCharacter is its child blueprint). It stores all of the information about what spells the character currently has learned.

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Stats)
		TArray< AProjectBlackSpell* > SpellArray;

This array is added to through the AddSpell() function:

UFUNCTION(BlueprintCallable, /*BlueprintImplementableEvent,*/ Category = Stats)
	void AddSpell(AProjectBlackSpell* NewSpell);

AddSpell is called from the character’s blueprint to add the spells the player should know by default. It is called in the BP_ProjectBlackCharacter Construction Script.


The “Spell” node is a custom node:

AProjectBlackSpell* UCreateNewObject::NewSpellFromBlueprint(UObject* WorldContextObject, TSubclassOf<AProjectBlackSpell> UC)
{
	UWorld* World = GEngine->GetWorldFromContextObject(WorldContextObject);
	AProjectBlackSpell* tempObject = Cast<AProjectBlackSpell>(StaticConstructObject(UC));

	return tempObject;
}

This is roughly where the problem occurs. This problem has occurred in the C++ and the Blueprint Implementations, but I’d prefer to keep AddSpell as a C++ function.

I’ve used breakpoints in both Blueprints and C++ to try to find exactly where it is crashing, and I have tracked it down to either being the point where AddSpell adds the new spell to the array, or to the Blueprint Class → Object creation node.

MachineId:15C8B3AF4C09AF40751D43882B00B9BE
EpicAccountId:52ed2a875edc46fa9ee878b068607d15

Unknown exception - code 00000001 (first/second chance not available)

Fatal error: [File:D:\BuildFarm\buildmachine_++depot+UE4-Releases+4.5\Engine\Source\Runtime\CoreUObject\Private\Templates\Casts.cpp] [Line: 11]
Cast of Package //Engine//Transient to Level failed

KERNELBASE + 37901 bytes
UE4Editor_Core + 3174852 bytes
UE4Editor_Core + 1677215 bytes
UE4Editor_CoreUObject + 356903 bytes
UE4Editor_Engine + 1235050 bytes
UE4Editor_Engine + 2559645 bytes
UE4Editor_Engine + 9871685 bytes
UE4Editor_Engine + 3769504 bytes
UE4Editor_Engine + 3873565 bytes
UE4Editor_Engine + 3883827 bytes
UE4Editor_Engine + 3885994 bytes
UE4Editor_UnrealEd + 4144983 bytes
UE4Editor_UnrealEd + 4524876 bytes
UE4Editor_UnrealEd + 4429599 bytes
UE4Editor_UnrealEd + 4526262 bytes
UE4Editor_UnrealEd + 1893329 bytes
UE4Editor_UnrealEd + 6486374 bytes
UE4Editor!FEngineLoop::Tick() + 3524 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.5\engine\source\runtime\launch\private\launchengineloop.cpp:2129]
UE4Editor!GuardedMain() + 479 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.5\engine\source\runtime\launch\private\launch.cpp:133]
UE4Editor!GuardedMainWrapper() + 26 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.5\engine\source\runtime\launch\private\windows\launchwindows.cpp:125]
UE4Editor!WinMain() + 249 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.5\engine\source\runtime\launch\private\windows\launchwindows.cpp:201]
UE4Editor!__tmainCRTStartup() + 329 bytes [f:\dd\vctools\crt\crtw32\dllstuff\crtexe.c:618]

I kept getting the “Cast of object to level failed” error. I added MyArray.Init(SomeCapacity) in my constructor and that helped the error go away. MyArray would be whatever TArray object you are using and SomeCapicity is just a number you can choose to initialize the array.

Hope this helps.