When to use .new()?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By René Ruppert

I’m a long term programmer with lots of experience with statically typed languages - maybe that explains why I’m (still) struggling width Godot Script - but I’m getting there :slight_smile:

This said, here’s what I haven’t really understood: when to use .new()?

Why is it

var vec = Vector2(10, 20)


var img = Image.new()


The answer doesn’t make me fully happy, so I try to make my question clearer. These are the example cases I’m struggling with:

1: var vec = Vector2() - works
2: var img = Image() - does not work - considers “Image()” to be a function
3: var img = Image.new() - works, creates a uninitialised (?) image
4: var img = Image.create(10, 20, false, ...) - works, creates an initialised image
5: var vec = Vector2(1, 2) - works
6: var vec = Vector2.new() - does not work, “new()” not present in built-in type

Why are cases 1 and 2 treated differently? I think this is what it comes down to. Is it correct to say that new()is like a factory method for a reference type where as Vector2 is like an int and thus does not need to be “created”? Or should new() be compared to the new operator of other languages, like C#?

I’m not entirely clear on using new() either. However, i think it’s used in instantiating new classes. When I create my own class, and when I need to use it in a script, I need to use the new() function. Also note that Vector2 is a built-in type, and using new() may be unnecessary for creating a Vector2.

Ertain | 2019-01-01 22:09

Vector2() is a function member of class with the same name, that creates and returns an object of type Vecotr2. That’s why you don’t call new.

p7f | 2019-01-02 11:27

Thank you for the clarification.

Ertain | 2019-01-02 19:11

So why is Vector2() a function member then but Image() is not? And while we’re at it: I do not see Vector2() as a member - just Vector(x, y). This is getting more confusing by the second :slight_smile:

René Ruppert | 2019-01-02 19:17

I know the feeling, René. :smiley:

Ertain | 2019-01-02 21:36

Why one is member function and anotherone isn’t must be a matter of implementation, and we should ask developers. I guess that Vector2 is used a lot, and making a Vector2 function is a shortcut for programmers. Vector2(x,y) is the member function, but x and y are default 0, so passing Vector2(0,0) is the same as Vector2() just like default arguments in C++, python, and others.

p7f | 2019-01-03 00:37

:bust_in_silhouette: Reply From: p7f

new() is used to create a node from the class. Thats why you must call var img = Image.new(), cause on the contrary, you would be trying to assign Image built in data type to the variable img. Why you do not call new in Vector2 then? Cause, acording to docs Vector2 is also a member function from the Vector2 built in type, that constructs a Vector2 object and returns it. So you dont call new because Vector2() is doing it for you. When you write var vec =Vector2(10,20) you are executing Vector2() function. Image does not have similar member function, so you must call new.

At least, this is what i always understood