Apparent inconsistency in class instantiation

Godot Version

v4.3 stable win64

Question

Hello,
I am completely certain that this is a misunderstanding on my part, but I’m confused why this happens. In essence:

This works:

var obj_1 : Object = Object.new()
var vec3_1 : Vector3 = Vector3()

This doesn’t:

var obj_2 : Object = Object()
# Error: Invalid constructor "Object()", use "Object.new()" instead
var vec3_2 : Vector3 = Vector3.new()
# Error : Cannot find constant "new" on base "Vector3"

Why?

Vector3 is not an Object, only Objects have .new()

1 Like

Because Object is a special type of Variant, so requires a different handling from the other variants types. Also all the other variants (except from null, see the list here) have constructors to create a new variant using (), like Vector3() (see the Vector3 docs). So basically, any variant that inherits from the Object class needs the .new(), the others have their constructors to be created with the ().

1 Like

Just wanted to add that neither Object nor Vector3 are Variants. Variant is a container class that can hold Objects and Vector3s and any other Godot type. Check the documentation for info about what Variant is.

1 Like

Hello.
Already been reading up on the docs for a few days, but was just confused about this specific syntax.
Thank you for your help.