# What's the point of Verse constructors?

**URL:** <https://forums.unrealengine.com/t/whats-the-point-of-verse-constructors/1384647>\
**Category:** Programming & Scripting\
**Tags:** unreal-editor-for-fortnite, fortnite-creative, question, fortnite\
**Created:** [November 12, 2023, 2:42pm UTC](https://forums.unrealengine.com/t/whats-the-point-of-verse-constructors/1384647 "2023-11-12T14:42:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kamyker](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/kamyker/32/1194621_2.png) [@Kamyker](https://forums.unrealengine.com/u/Kamyker)\
**Post date:** [November 12, 2023, 2:42pm UTC](https://forums.unrealengine.com/t/whats-the-point-of-verse-constructors/1384647/1 "2023-11-12T14:42:55Z")

</div>

Design of Verse constructors is a bit weird. Coming from C# if they would be similar we could use them as:

```auto
MyClass := my_class(DataForConstructor:my_data):
    MyClassVar := OtherVar

```

This makes them very easy to use and discover. After typing `my_class(` we get a suggestion list of all possible constructors. Additionally it’s possible to make default constructor private and inaccessible, forcing us to use custom constructor.

Verse constructors seem to not differ much from functions? Other then a weird syntax with let, blocks and specific rules when multiple constructors are called.

I thought maybe they can be used for initializing values for variables but no:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/f/0/5/f05fee0e38a8517f2ed068cf1cf2dd12e4c03889.png)

---

<div class="post-metadata">

**Author:** ![UltimateLambda](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/ultimatelambda/32/916183_2.png) [@UltimateLambda](https://forums.unrealengine.com/u/UltimateLambda)\
**Post date:** [November 17, 2023, 9:17pm UTC](https://forums.unrealengine.com/t/whats-the-point-of-verse-constructors/1384647/2 "2023-11-17T21:17:41Z")

</div>

The major value of Verse constructor functions is their ability to delegate construction to some other constructor function. They are similar to C++'s member initializer list syntax and delegating constructors, but many different constructor functions can be defined for a class external to the class and with arbitrary names. For example,

```auto
class1 := class:
  X:int
  Y:int
  Z:int

MakeSomeClass1<constructor>(Arg1:int, Arg2:int) := class1:
  X := Arg1
  Y := Arg2
  Z := Arg1 + Arg2

MakeSomeOtherClass1(Arg1:int) := class1:
  MakeSomeClass1<constructor>(Arg1, Arg1) # delegate to another constructor
  Z := Arg1 # except override what the other constructor did for Z

```

This delegating applies to inherited classes, as well, with similar syntax.

```auto
class1 := class:
  X:int
  Y:int

ZeroClass1<constructor>() := class1:
  X := 0
  Y := 0

class2 := class(class1):
  Z:int

ZeroClass2<constructor>() := class2:
  ZeroClass1<constructor>()
  Z := 0

```

If you don’t intend to use a constructor function with other constructor functions, you may be better served with a plain function.

---

<div class="post-metadata">

**Author:** ![UltimateLambda](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/ultimatelambda/32/916183_2.png) [@UltimateLambda](https://forums.unrealengine.com/u/UltimateLambda)\
**Post date:** [December 6, 2023, 11:17pm UTC](https://forums.unrealengine.com/t/whats-the-point-of-verse-constructors/1384647/3 "2023-12-06T23:17:54Z")

</div>

There is also another use: easy partial updates of a class without using a `var` field. For example,

```auto
class1 := class:
  X:int
  Y:int

CopyClass1<constructor>(Arg:class1) := class1:
  X := Arg.X
  Y := Arg.Y

var X:class1 = class1{X := 1, Y := 2}
set X = class1:
  CopyClass1<constructor>(X)
  Y := X.Y + 1

```

Whenever a field is added to `class1`, only `CopyClass1` needs updating, and not all the partial updates. This mimics deep mutation available to structs.

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [January 5, 2024, 11:18pm UTC](https://forums.unrealengine.com/t/whats-the-point-of-verse-constructors/1384647/4 "2024-01-05T23:18:54Z")

</div>

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