Offset Spawned Actor Position in C++ Not Working

I’m trying to spawn an actor at the creation of my game and offset its position from the origin of the level.

My code is running in the GameMode::BeginPlay() method. The actor spawns successfully, but is always at the origin of the level.

What am I doing wrong with the following code using the World->SpawnActor() function?

Any help is appreciated! Thank you!

GameMode Spawn of the Actor:

void AProgrammer01GameMode::BeginPlay()
{
	Super::BeginPlay();

	UWorld* const World = ();
	if (World)
	{
		FIntVector origin = World->OriginLocation;
		FVector position = FVector(origin) + FVector(1000, 0, 0);
		FRotator rotator(0, 0, 0);

		ASolidBlock* block = World->SpawnActor<ASolidBlock>(
                    ASolidBlock::StaticClass(),
			        position, rotator);
	}
}

SolidBlock.h

UCLASS()
class PROGRAMMER01_API ASolidBlock : public AActor
{
	GENERATED_UCLASS_BODY()
	
public:	
	// Sets default values for this actor's properties
	ASolidBlock();

	// Called when the game starts or when spawned
	virtual void BeginPlay() override;
	
	// Called every frame
	virtual void Tick( float DeltaSeconds ) override;

	// static mesh
	UStaticMeshComponent* MyBlock;
	
};

SolidBlock.cpp

// Sets default values
ASolidBlock::ASolidBlock(const class FObjectInitializer& PCIP)
	: Super(PCIP)
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	MyBlock = PCIP.CreateAbstractDefaultSubobject<UStaticMeshComponent>(this, TEXT("SolidBlock"));

	static ConstructorHelpers::FObjectFinder <UStaticMesh>StaticMesh(TEXT("StaticMesh'/Game/StarterContent/Architecture/Wall_500x500'"));
	static ConstructorHelpers::FObjectFinder<UMaterial> Material(TEXT("MaterialInstanceConstant'/Game/StarterContent/Materials/M_CobbleStone_Pebble'"));

	MyBlock->SetStaticMesh(StaticMesh.Object);
	MyBlock->SetMaterial(0, Material.Object);
}

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

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

}

maybe try checking what FVector(origin) is at moment of adding

FVector(origin) appears to resolve to (0,0,0). Can’t see why that shouldn’t work? Adding FVector(1000, 0, 0) sets the position to (1000, 0, 0).

It’s as if SpawnActor is ignoring the position value I inputted?

can you paste the whole class, is there another place in the code where you use a SetActorLocation ?

I don’t use a SetActorLocation anywhere else with the ASolidBlock class. I’ll post all the code that pertains to ASolidBlock.

Update: Posted all the code, including the source and header for ASolidBlock.

I’m currently working on this problem as well, however I’m stuck at getting my object to spawn when BeginPlay occurs. My test class is setup the same as this one. I also have a Game Mode where I’m trying to spawn my test class when the game is run. When I load the editor with my project I can see my class in the side, I can then drag the class out into the scene and then run the game. My object will spawn where it is supposed to but I first have to place it out in the scene. I would like to have the object placed in the scene through code.

Are you getting your object to spawn in the middle of your level without having to manually click/ drag the class out into the scene view? I’d be happy if I could get to that point.

The actor class I’ve created has it’s static mesh and material assigned exactly the way yours is. When I drag the class into the scene I can see its properties with the correct static mesh/ material assigned. I’ve even turned physics on the asset through code so it will fall down to the ground. Still I cannot get it to automatically place in the scene when I run the game. What am I missing?

I am experiencing this same problem. Has anyone found a solution?

Hi everyone,

The code that was posted by Softfalcon is missing one major thing that is causing the transform and rotation of the SpawnActor function to not apply to the SolidBlock actor. A root component needs to be set. If you add the following line of code after the MyBlock staticmeshcomponent is made in the SolidBlock.cpp everything should work perfectly.

RootComponent = MyBlock;

Hope this helps, have a nice day!

Will “StaticMeshComponent->SetMobility(EComponentMobility::Movable);” also help in fixing this issue?