Spawning an actor *after* constructing it

Hi,

Nobody seems to be answering my simple questions and my last question disappeared for no reason.

All I need to know is something very simple so I can actually start creating this game and stop trying to climb over pointless hurdles.

In my GameMode, I cannot figure out how to deferred-spawn an actor. That is, create the object, and make the thing visible in the world later, giving time to initialise variables and do some calculations first.

void ANocturnalSynergyGameMode::StartPlay()
{
	FTransform spawnArgs(ENoInit::NoInit);
	auto fuelTank = Cast<AFuelTank>(UGameplayStatics::BeginDeferredActorSpawnFromClass(GetWorld(), AFuelTank::StaticClass(), spawnArgs));
// do anything here
	UGameplayStatics::FinishSpawningActor(fuelTank, spawnArgs);

	delete fuelTank;
}

All this should do is construct it with no position or rotation, hence ENoInit::NoInit, do anything to the object, then spawn it into the world. The issue is that GetWorld() returns NULL and just crashes UE4. I’ve seen people talking about doing this in the ‘level’, but there’s no file with anything to do with levels, I start the game and the level is just there.

241939-files.png

If GetWorld() is null that means actor is not attached to world yet at stage this code is executed. If you can’t get world instance from home actor, you can get world instance from any other actor that is in the level, you can also try using GWorld but using is risky specially in editor where there multiple world instances, you can also try finding something in UEnigne with GEngine http://api.unrealengine.com/INT/API/Runtime/Engine/Engine/UEngine/.

Also delete fuelTank; will delete actor data from memory, which most likely will cause crash when engine will try to access the actor as it will still formally exist in world. You should not use delete on UObjects in general, UE4 deals with cleaning of UObjects so don’t worry, actors in the perticilar will exist as long you nor remove it from the world with Destroy().

Another option is to Spawn the actor invisible and with collisions ignored, and then turn those on after initialization.