Scope
The scope is the region of a computer program where a name binding is valid. Name binding is just a fancy word for saying “variables”.
Scopes can vary in range in a programming application.
These can be from small for loops and if statements to entire classes.
Levels of Scope
- Global Scope
- Class/File/Module Scope
- Function Scope
- Code Block Scope
Global Scope
Global scope is a class, variable, or function that can be used anywhere in the
Godot Application.
An example would be the Node class:
extends Node
# The rest of your class
Class Scope
A class scope is a variable or function that can be used anywhere in the class file.
extends Node # Global Scope Class Name Node
# Class variables that can be used anywhere in the class file
var health: String = 100
var speed: float = 10.0
Function Scope
A variable/value that can only be used inside of a function
extends Node # Global Scope Class Name Node
#Class Scope
var health: String = 100
var speed: float = 10.0
func example():
# variable functionVariable can only be used inside of the function `example()`
var functionVariable = 10
Code Block Scope
A variable/value that can only be used inside of a code block.
Example fo code blocks would be the if statement, match statement, for & while loops, etc:
extends Node # Global Scope Class Name Node
# Class Scope
var health: String = 100
var speed: float = 10.0
func example():
# Function Scope
var functionVariable = 10
if(true):
# Code Block Scope
# variable message exists only inside this one if statement
var message = "Hi!"
print(message)
Resources
Github Project:
Godot Tutorials Resources