How to write constructor for custom struct

Hello SiberianExpress,

When it comes to doing a constructor for a struct, you can do it just like you would for a normal class. All you need to add is:

 USTRUCT(immutable, noexport, BlueprintType)
 struct FIntVector2D
 {
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "IntVector2D", SaveGame)
	int32 X;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "IntVector2D", SaveGame)
	int32 Y;

	FIntVector2D();

	FIntVector2D(int32 NewX, int32 NewY)
	{
		 X = NewX;
		 Y = NewY;
	}

 };

Edit: I changed it a bit after noticing that you want to have a constructor that takes values, not one with predefined values. You just need to also make the default constructor or the compiler won’t like it.

Hope this helps!

2 Likes