Skip to main content

Constructors

In Lua, there is no new keyword, so you cannot construct new instances of classes or structs in your ModScripts. Now I could just give you a function that lets you construct any type, but i don't think that's very safe for the end user.

But on second thought, it's probably fine to do construction of value types (structs), as long as they fall within the whitelist.

So I created a helper function called createInstance that takes a type name, and returns a new instance of that type.

registerType('Player')
local player = getItemsByMemeId("MemeorangMan")[1]
local newScale = createInstance("Vector3")
newScale.x, newScale.y, newScale.z = 0.4, 2, player.transform.localScale.z
player.transform.localScale = newScale

Notice how the values are assigned after construction. All structs in C# have a default parameterless constructor, which is why you can construct from the struct name alone. Pretty much all Unity structs should let you modify their member values after construction.

Some useful structs you may commonly use might include:

  • Vector2
  • Vector3
  • Quaternion
  • Color

...and maybe a lot more. Remember to consult the Unity docs for more information