When declaring a variable, what does ":=" do ?

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

I’m following the GDQuest course Tactical RPG, and I see that :
export var size := Vector2(20, 20)

I searched through the doc and internet but I couldn’t find what it meant. Any idea ?

:bust_in_silhouette: Reply From: woyosensei

As far as I know := means, in this case, the same as var varname : Vector2 = Vector2(0, 0)
Basically := assign type of variable to it.
Another example can be:
var somevar := 1 will mark variable as int
var somevar := 1.0 will mark var as float
var somevar := true will mark var as bool
Etc etc. You get the point. Sorry for lack of formating. Was typing from phone.
Best regards.

Yes as docs shows

USBashka | 2022-07-16 07:44

:bust_in_silhouette: Reply From: haydenv

As explained in another answer the := is a way of forcing a variable to only take on values of a certain type (such as bool, int, float, String, Vector2, Array, etc).

Since that’s already been explained I’d like to add that there are two primary reasons for using typed variables.

Reason #1 is that it helps catch coding errors, and makes code more understandable (albeit also longer).

Reason #2 is that it allows the engine to run more efficiently, since the engine knows beforehand what type of data each variable will definitely be.