GDScript variables

GDScript Variables

In GDScript, variables are declared using the var keyword. Here's an example of declaring and initializing a variable in GDScript:

var player_name = "John"

In this example, a variable player_name is declared and initialized with the value "John". GDScript is a dynamically typed language, so you don't need to specify the type of the variable explicitly.

GDScript Variable Scope

Variables in GDScript can have different scopes. If a variable is declared outside of a function, it is considered a member variable and is accessible to all functions within the script. If a variable is declared within a function, it is considered a local variable and is only accessible within that function.

GDScript Variable Naming Convention

It's important to follow a naming convention for variables in GDScript. Variable names should be descriptive and follow the camelCase convention. This means that the first letter of the variable name should be in lowercase, and the first letter of each subsequent word should be capitalized.

GDScript Constants

In GDScript, constants are declared using the const keyword. Constants are variables whose values cannot be changed once they are set. Here's an example of declaring a constant in GDScript:

const MAX_PLAYERS = 4

In this example, MAX_PLAYERS is declared as a constant with a value of 4.

GDScript Variable Data Types

GDScript is a dynamically typed language, which means that variables do not have an explicit type. The type of a variable is inferred from the value assigned to it. However, GDScript does support static typing through type hints, which can be used to specify the type of a variable.

For example, you can use type hints to specify the type of a variable like this:

var health: int = 100

In this example, the variable health is explicitly typed as an integer using the type hint int.