Simple cube spawning

Hi,

I’m trying to spawn a simple mesh using C++ but the editor is crashing constantly when it spawn in the world.

The only error specified in logs is : ensure(GetShadowIndex == 0) failed,

here’s sample of my code.

ACubeActor.cpp

ACubeActor::ACubeActor(const class FObjectInitializer& PCIP)
	: Super(PCIP)
{
	CubeMesh = PCIP.CreateAbstractDefaultSubobject<UStaticMeshComponent>(this, TEXT("CubeMesh"));

	static ConstructorHelpers::FObjectFinder<UStaticMesh> StaticMesh(TEXT("StaticMesh'/Game/DemoContent/StaticMeshes/QtCube_StaticMesh.QtCube_StaticMesh'"));
	static ConstructorHelpers::FObjectFinder<UMaterial> Material(TEXT("MaterialInstanceConstant'/Game/StarterContent/Materials/M_Basic_Floor.M_Basic_Floor'"));

	CubeMesh->SetStaticMesh(StaticMesh.Object);
	CubeMesh->SetMaterial(0, Material.Object);
	//CubeMesh->AttachTo(RootComponent);

}



// Called when the game starts or when spawned
void ACubeActor::BeginPlay()
{
	Super::BeginPlay();

}

// Called every frame
void ACubeActor::Tick( float DeltaTime )
{
	Super::Tick( DeltaTime );

}

ACubeActor.h

UCLASS()
class QTUEDEMO_API ACubeActor : public AActor
{
GENERATED_UCLASS_BODY()

UPROPERTY(Category = Meshes, VisibleAnywhere)
UStaticMeshComponent* CubeMesh;

public:
// Sets default values for this actor’s properties
//ACubeActor();
//ACubeActor(FObjectInitializer& PCIP);

// Called when the game starts or when spawned
virtual void BeginPlay() override;

// Called every frame
virtual void Tick( float DeltaSeconds ) override;

};

// Called when QtPushButton Cube is triggered in Qt
void UQtActorComponent::TestCallBack() {
	
	FVector Position = FVector(FMath::RandRange(-1000,1000), FMath::RandRange(0, 1000), FMath::RandRange(1000, 10000));
	FRotator Rotation = FRotator(0.0f);

	//ArrayCubeActor.Push( GetWorld()->SpawnActor<ACubeActor>( Position, Rotation ));
	//ArrayCubeActor.Push( GetWorld()->SpawnActor<ACubeActor>( ACubeActor::StaticClass() ));
	//ArrayCubeActor.Push( (ACubeActor*)GetWorld()->SpawnActor( ACubeActor, &Position));
	//ArrayCubeActor.Push( (ACubeActor*)GetWorld()->SpawnActor<ACubeActor>( Position, Rotation ));
	//ArrayCubeActor.Push( SpawnActor<ACubeActor>( this ));

}

Is anyone maybe got a tutorial in UE 4.10, C++ helping me. Or a solution ?

Thanks in advance.

Problem solved.

Now the spawned Actor isn’t Kinematic at all ; even specifying it.

Error coming from GetValueOnGameThread() and Ensure condition failed: GetShadowIndex() == 0.

If someone knows how to deal with that ?

Hi!
I receive recently the same error, and it happens becouse i try Spawn an Actor in a different thread. This usually happen when we try execute Async Tasks in different threads or are calling the spawn in functions being executed outside the Gamethread.
To guarantee it be called inside of the Gamethread i fireup a AsyncTask specifying the thread i want this be executed like this:

    AsyncTask(ENamedThreads::GameThread, [this]()
	{
		SpawnRailCamera();
		// Time when the press play...
		StartTime          = GetWorld()->GetTimeSeconds();
		CurrentSplineTime  = 0.0f;
		SplineLength       = GetRailSplineComponent()->GetSplineLength();
		bLockOrientationToRail = IVR_FollowRail;

		RailCam->IVR_StartRecord();
		MoveCamera = true;
		FinishRecording = false;
	});

Everything inside of the AsyncTask {} , will be executed inside of the Gamethread. In this way, for example , code not related with the game itself can be executed outside of the main Game Thread without stuck the game, like database updates etc…
I see in your example some Qt comunication between you UE aplication and Qt , be carefull with this becouse all QObjects are managed by the QAplication Thread, so i strongly suggest you create functions that create new Qt Objects inside of the QtApp and return the pointers to the UE aplication. In this way you avoid Crashes and Memory Leaks in your project.
This is an example of how create new Objects in Qt to move them in the correct Qt Thread:

CIVRFrameRecorder *IVRLowLevelSDK::IVR_CreateFrameRecorder(uint              pIVR_RecordingMode     ,
                                                           QString          &pIVR_RecorderName      ,
                                                           qint64            pIVR_Timestamp)
{
    CIVRFrameRecorder *pFrameRecorder = new CIVRFrameRecorder();

    pFrameRecorder->moveToThread( IVR_QtApp->instance()->thread() );

    pFrameRecorder->IVR_CameraName    = pIVR_RecorderName;
    pFrameRecorder->IVR_RecordingMode = pIVR_RecordingMode;
    pFrameRecorder->IVR_Timestamp     = pIVR_Timestamp;
    pFrameRecorder->IVR_RootFolder    = IVR_RootFolder;

    return pFrameRecorder;
}

This is an example when you need destroy them:

void IVRLowLevelSDK::IVR_DestroyFrameRecorder(CIVRFrameRecorder* pIVR_Recorder)
{
    pIVR_Recorder->moveToThread( IVR_QtApp->instance()->thread() );
    pIVR_Recorder->deleteLater();
    delete pIVR_Recorder;
    pIVR_Recorder=nullptr;
}

In this way you can have Qt objects living in your UE aplication and guarantee they will be allocated and destroyed in the correct Qt Thread.

Hope it helps.

1 Like