Menu

GDScript Variables: var, Scope and Member Variables

How to declare variables in GDScript with var, the difference between member and local variables, default values, and the scope rules that catch new Godot programmers.

This page includes runnable editors. Edit, run, and see output instantly.

A variable holds a value under a name. You create one with var:

Ana has 15 coins

Names use snake_case: max_speed, jump_height. They may contain letters, digits and _, and must not start with a digit.

Three Ways to Declare

now a string 10 10

A dynamic variable accepts any value at any time. A typed one is checked: b = "text" stops the script with an error before it runs. The static typing page explains when to use each form. Most Godot code uses := because it is short and still typed.

Member Variables and Local Variables

Where you write var decides how long the variable lives:

100 -> 70
70 -> 45
Health left: 45

health sits at the top of the script, so every function sees it and it keeps its value between calls. before is local to take_damage: each call gets a fresh one, and it is gone when the function returns.

Member variables are also what the editor can show in the Inspector, when you mark them with @export.

Default Values

A variable with no value starts as null when it has no type, or as its type's default when it has one:

<null>
0 0.0 false [] (0.0, 0.0)

Scope

A variable exists from the line that declares it to the end of its block. A variable declared inside an if or a loop is gone after it:

func _ready():
	if true:
		var message := "inside"
		print(message)
	print(message)  # Parse Error: Identifier "message" not declared in the current scope.

Declare the variable before the block when you need it after:

you win

Shadowing a Member Variable

A local variable may reuse a member's name. Inside that function the local one wins, and the member is untouched:

5
100

The Godot editor marks this with a SHADOWED_VARIABLE warning, because it is usually a typo: you meant speed = 5. Use self.speed when you really need the member while a local has its name.

Global Variables

GDScript has no globals. Two patterns replace them:

  • Autoload. Write a script such as game.gd with var score := 0, then add it under Project Settings > Globals > Autoload with the name Game. Every script can read Game.score.
  • Static variables. static var high_score := 0 in a class keeps one value shared by every instance. See static functions.

Frequently Asked Questions

How do I declare a variable in GDScript?

Use var: var score = 0. Add a type to make it a typed variable, var score: int = 0, or let Godot infer the type with :=, as in var score := 0.

What is the default value of a variable in GDScript?

A variable declared with no value and no type is null. A typed variable starts at its type's default: 0 for int, 0.0 for float, false for bool, "" for String, Vector2(0, 0) for Vector2, and null for objects.

How do I make a global variable in Godot?

GDScript has no global variables. Put the value in a script, add that script as an Autoload in Project Settings, and read it from anywhere by its name, for example Game.score. A static var in a class is the other option.

What is the difference between a member and a local variable?

A member variable is declared at the top level of the script, outside any function. Every function of the script can use it, and it lives as long as the node. A local variable is declared inside a function and disappears when the function returns.

Coddy programming languages illustration

Learn to code with Coddy

Get started