# How to write constructor for custom struct

**URL:** <https://forums.unrealengine.com/t/how-to-write-constructor-for-custom-struct/341749>\
**Category:** C++\
**Tags:** Struct, constructors, unreal-engine, UE4, CPP, question\
**Created:** [December 17, 2015, 6:24pm UTC](https://forums.unrealengine.com/t/how-to-write-constructor-for-custom-struct/341749 "2015-12-17T18:24:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![SiberianExpress](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@SiberianExpress](https://forums.unrealengine.com/u/SiberianExpress)\
**Post date:** [December 17, 2015, 6:24pm UTC](https://forums.unrealengine.com/t/how-to-write-constructor-for-custom-struct/341749/1 "2015-12-17T18:24:06Z")

</div>

I created a custom struct “FIntVector2D” simply a 2D integer vector using the FIntVector template.

file **ObjectLib.h**

```
> #include "Engine/ObjectLibrary.h"
> #include "ObjectLib.generated.h"
>     
> USTRUCT(immutable, noexport, BlueprintType)
> struct FIntVector2D
> {
> UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "IntVector2D", SaveGame)
> int32 X;
>     
> UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "IntVector2D", SaveGame)
> int32 Y;
> };

```

That works fine, I can use it the way I want to in Unreal, but I would like to have a constructor such as

> `FIntVector2d(10, 20);`

I have been trying to copy the code from the default FIntVector but can’t for the life of me figure out the exact code thats needed and where to put it.

Hope someone can help or at least point me int eh right direction.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_41845f4a](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_41845f4a](https://forums.unrealengine.com/u/anonymous_user_41845f4a)\
**Post date:** [December 17, 2015, 10:46pm UTC](https://forums.unrealengine.com/t/how-to-write-constructor-for-custom-struct/341749/2 "2015-12-17T22:46:09Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![elguapador](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@elguapador](https://forums.unrealengine.com/u/elguapador)\
**Post date:** [May 30, 2022, 1:35pm UTC](https://forums.unrealengine.com/t/how-to-write-constructor-for-custom-struct/341749/3 "2022-05-30T13:35:56Z")

</div>

Just to add to this if you end up here and you’re still getting “no default constructor” or linking errors:

It seems that you always need to set default values of all your struct variables in your constructor. In my case, I did one default constructor with no arguments, initialising all variables to something:

```auto
FMyConstructor()
{
     Variable1 = 0;
     Variable2 = nullptr;
     veriable3 = EMyEnum::SomeEnum;
    // etc
}

```

Then the way I wanted to use it in code:

```auto
FMyConstructor(float var1, AActor* var2, TEnumAsByte<MyEnum> var3)
{
    variable1 = var1;
    variable2 = var2;
    variable3 = var3;
}

```

Finally compiles on my end!

---

<div class="post-metadata">

**Author:** ![elguapador](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@elguapador](https://forums.unrealengine.com/u/elguapador)\
**Post date:** [September 22, 2026, 9:03am UTC](https://forums.unrealengine.com/t/how-to-write-constructor-for-custom-struct/341749/4 "2026-09-22T09:03:57Z")

</div>

Update from the future:  
As long as you define defaults in the member declaration block, you can just define a default constructor:

`FMyConstructor() = default;`
