0 votes

I've noticed that in some scripts there is sometimes Vector2.ZERO which I don't know what importance it has for moving or any other operation on an object. Can you explain me what is it for?

Godot version 3.2.3
in Engine by (48 points)

2 Answers

+1 vote

Hi,
Vector2.ZERO is just a vector with zero length its the same as Vector2(0,0)
It has no special importance for anything.

by (3,932 points)
0 votes

It's as useful as other Vector2 constants like Vector2.ONE, Vector2.UP etc. These constants should be preferred to use over hardcoded values like Vector2(0, -1) for better code readability.

I personally don't use Vector2 constants for movement, but mostly thing like grid lookups. In this case, Vector2.ZERO could mean things like: "no offset applied", "idle" etc. In any case, not having Vector2.ZERO would make the current set of Vector2 constants inconsistent on mathematical level.

I'm not sure about implementation detail, but in theory using Vector2 constants should also result in a slightly better performance since no Vector2() constructor has to be called in GDScript, but it's quite negligible to worry about in the first place. Better performance would just be a nice byproduct of using constants over hardcoded values, and not as a direct optimization technique.

by (1,356 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.