Why we often use Vector2.ZERO for moving an object

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

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?

:bust_in_silhouette: Reply From: klaas

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

:bust_in_silhouette: Reply From: Xrayez

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.

1 Like