How can I spawn an actor at a specific location in code?

In my game mode class (c++) I’m using the following to spawn a test block.

UWorld* const World = GetWorld();
	if (World)
	{
		ATestIcoCorner* TheWallNinty = World->SpawnActor<ATestIcoCorner>(ATestIcoCorner::StaticClass(), FVector(20000.f, -60000.f, -60000.f), FRotator::ZeroRotator);
	}

When I run the game the block only spawns at the worlds center (0.f, 0.f, 0.f). It doesn’t appear to follow the Fvector I’m giving it. Any ideas?

Thanks

Just some quick questions:

  1. What class are you deriving ATestIcoCorner from?
  2. Are you sure that the component you
    expect to see at that location is
    set to be the RootComponent of the
    ATestIcoCorner actor?

Just asking these as I ran into the same problem of components spawning at (0,0,0) when they weren’t attached properly to the actor.

ATestIcoCorner is an Actor class.

Interesting. So there’s a chance that in my actor class I’ve not set a root component yet? When I play the game The block just falls from the worlds center. I’ve enabled physics on it so it makes sense that it falls but it’s always falling from the worlds center, no matter where I set the FVector to put it.

Do you know how to set this actor to a root component?

Awesome thanks! Worked perfectly,

Yes, luckily it’s fairly simple:

In ATestIcoCorner.h you probably have something like this:

UPROPERTY()
UStaticMeshComponent* MyStaticMeshComponent;

Now, in ATestIcoCorner.cpp constructor you do:

MyStaticMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyStaticMeshComponent "));

RootComponent = MyStaticMeshComponent;