Noob question about variables

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

I’m a total noob and I don’t understand one thing.

Keeping in account this tutorial: Creating your first script

A variable is a label, so var velocity = Vector2.ZERO means I can write “Vector2.ZERO” instead of “velocity”, but when I try to write Vector2.ZERO = Vector2.UP.rotated(rotation) * speed instead of velocity = Vector2.UP.rotated(rotation) * speed I received an error. Why?

Sorry for this very basic question.

:bust_in_silhouette: Reply From: jgodfrey

Overly simplified explanation below…

You can think of a variable as a generic container that can hold some value. That value could be many things, including an integer, a string, or a Vector2 as shown below:

var a = 1
var b = "John Doe"
var c = Vector2.ZERO

However, that Vector2.ZERO reference is a value itself (it’s a shortcut for the value Vector2(0,0)). That is, it’s already a specific value, just like the number 1 and the string John Doe above. You can’t assign a value to something that’s not a variable. For instance, none of these will work:

1 = 2
'John Doe' = 'Todd Smith'
Vector2.ZERO = 'apple'

So, while you can store a value in a variable (like the first examples), you can’t store a value directly in another value - which is what your original code is trying to do.

Thank you. It sounds simple when explained by an expert, but I couldn’t figure it out on my own.

Antonio Nero | 2022-10-08 12:58