Why does SetWorldLocation not work in constructor?

If I enable Selector(4); in constructor, SetWorldLocation() does not seem to work (green arrow does not move). However, if I enable Selector(4); in BeginPlay(), it does work (green arrow moves). What causes this weird behavior?

Note: You must enable only one Selector(4);, either in constructor or BeginPlay().

// MyActor.h
#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyActor.generated.h"

UCLASS()
class ATTACHING_API AMyActor : public AActor
{
	GENERATED_BODY()

private:
	void Initializer();
	void KeepRelativeTransform();
	void KeepWorldTransform();
	void SnapToTargetNotIncludingScale();
	void SnapToTargetIncludingScale();
	void Selector(uint8 input);
public:
	AMyActor();
	UPROPERTY(VisibleAnywhere)
		class USceneComponent* Root;
	UPROPERTY(VisibleAnywhere)
		class UArrowComponent* XAxis;
	UPROPERTY(VisibleAnywhere)
		class UArrowComponent* YAxis;
	UPROPERTY(VisibleAnywhere)
		class UArrowComponent* ZAxis;

protected:
	virtual void BeginPlay() override;
};
// MyActor.cpp
#include "MyActor.h"
#include "Components/ArrowComponent.h"


void AMyActor::Initializer()
{
	Root = CreateDefaultSubobject<USceneComponent>(TEXT("Root"));
	XAxis = CreateDefaultSubobject<UArrowComponent>(TEXT("XAxis"));
	YAxis = CreateDefaultSubobject<UArrowComponent>(TEXT("YAxis"));
	ZAxis = CreateDefaultSubobject<UArrowComponent>(TEXT("ZAxis"));

	XAxis->ArrowSize = 2.f;
	YAxis->ArrowSize = 2.f;
	ZAxis->ArrowSize = 2.f;

	XAxis->SetHiddenInGame(false);
	YAxis->SetHiddenInGame(false);
	ZAxis->SetHiddenInGame(false);

	// XAxis->SetArrowColor(FLinearColor(1.f, 0.f, 0.f));
	YAxis->SetArrowColor(FLinearColor(0.f, 1.f, 0.f));
	ZAxis->SetArrowColor(FLinearColor(0.f, 0.f, 1.f));

	// XAxis->AddWorldRotation(FRotator(0.f, 0.f, 0.f));
	YAxis->AddWorldRotation(FRotator(0.f, 90.f, 0.f));
	ZAxis->AddRelativeRotation(FRotator(90.f, 0.f, 0.f));

	// Attachment
	SetRootComponent(Root);
	XAxis->SetupAttachment(GetRootComponent());
	YAxis->SetupAttachment(GetRootComponent());
	ZAxis->SetupAttachment(GetRootComponent());
}

void AMyActor::KeepRelativeTransform()
{

}

void AMyActor::KeepWorldTransform()
{

}

void AMyActor::SnapToTargetNotIncludingScale()
{

}

void AMyActor::SnapToTargetIncludingScale()
{
	YAxis->SetWorldLocation(FVector(0.f, 100.f, 0.f));
}

void AMyActor::Selector(uint8 input)
{
	switch (input)
	{
	case 1:
		KeepRelativeTransform();
		break;
	case 2:
		KeepWorldTransform();
		break;
	case 3:
		SnapToTargetNotIncludingScale();
		break;
	case 4:
		SnapToTargetIncludingScale();
		break;
	}
}

AMyActor::AMyActor()
{
	PrimaryActorTick.bCanEverTick = true;
	Initializer();
	Selector(4);
}

void AMyActor::BeginPlay()
{
	Super::BeginPlay();
	//Selector(4);
}

constructor runs before Actor spawns so it has no world location during it.

If the actor has not been spawned yet when the constructor is being executed, why I can invoke YAxis->AddWorldRotation(FRotator(0.f, 90.f, 0.f)); and it works (the effect of rotation is noticeable)?

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms