Spawning blueprint actor from c++

When trying to spawn a blueprint actor from c++ I get the following error:

Ensure condition failed: GetShadowIndex() == 0

The code used for spawning the actor

static ConstructorHelpers::FObjectFinder`<UBlueprint>`blueprint_finder_cartpole(TEXT("Blueprint'/Game/Blueprints/BP_Cartpole.BP_Cartpole'"));
if (blueprint_finder_cartpole.Succeeded())
	mBCCartpole = (UClass*)blueprint_finder_cartpole.Object->GeneratedClass;
    const FVector spawn_point = FVector(0, 4, 7);
 	const FRotator spawn_rotation = FRotator();
 	ListOfActors.push_back(GetWorld()->SpawnActor<ACartpole>(mBCCartpole, spawn_point, spawn_rotation));

Is there a way to fix this error or is there a better way of spawning an actor?

Hey @Dependo. I would suggest you use a different method for spawning an actor. You can use GetWorld()->SpawnActor(YourBlueprintClass, [optional transform, location, rotation, scale])

For the variable YourBlueprintClass you can declare a variable in your .h file like:

UPROPERTY(EditAnywhere)
TSubclassOf<AActor> YourBlueprintClass; // Set up in the editor
// Instead of AActor you can use a more specific class if you want, as long as the blueprint inherits from it

Hope this helps!

2 Likes

I tried to implement the code you provided but I still get the same error.
I also tried to change the default scene component of the blueprint class, but that only gave me a new error.

Assertion failed: IsInGameThread() || HasAnyFlags(RF_ClassDefaultObject|RF_ArchetypeObject) || IsPostLoadThreadSafe() || IsA(UClass::StaticClass())

You might find this useful

What function are you trying to spawn the actor in? Normally you would use FObjectFinder in the constructor, and then call SpawnActor in a function like BeginPlay or Tick. You can’t spawn actors in the constructor of a class I don’t think.

@Dependo Where are you calling this function? The issue seems to be that it is not being called at runtime. A snippet of your code would be helpful

@zos @kribbeck Thanks for your answerers that help me spot the error.
I created a C++ class from an actor class, in the constructor of the class I set up a TCP socket. In the tick function I launch a new thread if it does not exist, this thread is handling the incoming message from the TCP and it was here I tried to spawn a new actor if I got a specific message. After your answers, I tried to set a bool in the class from that thread. I then added the SpawnActor to the tick function and now it works :smiling_face_with_three_hearts:

So, I think the problem was I tried to call spawn actor outside of the Game Thread

I think it’s not thread-safe to create any UObject (which includes AActors) from a non-game thread (and modifying UObjects in certain ways as well), as the garbage collector runs on the game thread unsyncronized with non-game threads, and I don’t think the garbage collector is threadsafe/lockfree. As for getting the callback from your TCP socket onto the game thread, I believe there is a function “defer into game thread” or something, where you can pass a functor that will be called later on the game thread.

i get this issue will writing this : GetWorld()->SpawnActor(BPclass, [Location, Rotation, Transform])
i get : C:\Users\slnob\OneDrive\Documents\Unreal Projects\MultiplyOrRelease\Source\MultiplyOrRelease\Spawner.cpp(24) : error C2760: erreur de syntaxe? ‘)’ ?tait inattendu ici; ‘{’ attendu
C:\Users\slnob\OneDrive\Documents\Unreal Projects\MultiplyOrRelease\Source\MultiplyOrRelease\Spawner.cpp(28) : error C2059: erreur de syntaxe? ‘}’

This have been solverd by changing the code and written this :
FVector Location = FVector(-760.0f, -20.0f, 3170.0f); // Remplacez par la position désirée
FRotator Rotation = FRotator(0.0f, 0.0f, 0.0f); // Remplacez par la rotation désirée

// Vérifiez que BPClass est une référence correcte à votre classe de blueprint
if (BPclass)
{
	AActor* SpawnedActor = GetWorld()->SpawnActor<AActor>(BPclass, Location, Rotation);
}

thank you !