How To Declare UStruct Constructors

Greetings,

I’ve been working on a number of custom UStructs for a given project. Originally, the purpose of the Structs were only exposition to BP so not having to dive into the constructor was alright. However, as the project has progressed, it is now part of the tasks to construct those custom UStructs inside of other classes.

Which brings me to the question: How does UStruct handle constructor/initializer declaration? I’ve been looking at what Google showed (Wiki [though it’s a tad hard to understand because of the code visualization errors in the wiki itself], AnswerHub, UE4 Forum, and others) and have tried those approaches with little success.

Update Progress

.h

   //Where the Class is Struct FMakeTransformData
    
    //Constructor
    FMakeTransformData()
     {    }
     
     //Desired Constructor with all the variables
     explicit FMakeTransformData(FVector Location, FRotator Rotation, FVector2D UpwardForwardOffset, bool SweepTrace, ETeleportType TeleportPhysics, bool SetRotation, bool StopIfSwept, EWorldLerpType LerpModel, float TransitionTime)
     {    }

This ^ Compiles the Header class.

However, whenever trying to fill the structure inside a constructor file, it doesn’t work. I’ve added print Debug Messages to each constructor and Always, ALWAYS, the default (empty) constructor is called. How should I be declaring the constructor inside the CPP File?

This is currently how I’m calling it (and it compiles)

//In the CPP.

FMakeTransformData(EndVectorTracing[3] + CapsuleOffset, RotatorOutput, UpwardForwardOffset, SweepCollisionWithMovement, ETeleportType::TeleportPhysics, SetRotationWithMovement, false, EWorldLerpType::XYLineZLog, TransitionTime / 2)

If however, I save this in a variable (that is uproperty) and then print let’s say the Location (first item), it’s never actually saved.

replace
FMakeTransformData();
to
FMakeTransformData(){}

Please check Updated Question :slight_smile:

standard constructor without parameters - mandatory for USTRUCT()

FMakeTransformData () {} // defgault constructor before your custom constructor

FMakeTransformData (…) {…}

//Okay. I have this in the header. It compiles:

	FMakeTransformData()
	{	}
	
	FMakeTransformData(FVector Location, FRotator Rotation, FVector2D UpwardForwardOffset, bool SweepTrace, ETeleportType TeleportPhysics, bool SetRotation, bool StopIfSwept, EWorldLerpType LerpModel, float TransitionTime)
	{	}

However, I’m still getting the Cannot convert from Initializer to FStruct whenever I’m trying to, in the CPP, make the FStruct for usage.

Please Check updated Question :slight_smile:

This worked and successfully stored the values.

	explicit FMakeTransformData(FVector Location, FRotator Rotation, FVector2D UpwardForwardOffset, bool SweepTrace, ETeleportType TeleportPhysics, bool SetRotation, bool StopIfSwept, EWorldLerpType LerpModel, float TransitionTime):
		Location(Location), Rotation(Rotation), UpwardForwardOffset(UpwardForwardOffset), SweepTrace(SweepTrace), TeleportPhysics(TeleportPhysics), SetRotation(SetRotation), StopIfSwept(StopIfSwept), LerpModel(LerpModel), TransitionTime(TransitionTime)
	{ 
		if(GEngine)
		{
			GEngine->AddOnScreenDebugMessage(-1, 2.0f, FColor::Yellow, Location.ToString()); 
		}
	}

For Anyone coming to this question:

explicit FYourStructName(YourVariableType VariableName,...):
VariableNameOfUProperty(VariableName), ...., {}