GDScript Crash Course Notes

Notes on getting started quickly with Godot’s native programming language — GDScript

This article will be updated long-term (last updated 2026.3.30)

Preamble

A few days ago I published my Godot study notes, with no反响. If they were explosively popular at this stage, that would be abnormal.

Given the importance of programming languages, I’m opening another article here as a GDScript study log.

The video is divided into three parts. For ease of reference, I’ll mark points as “前中后言”.

This article is based on Brackeys’ GDScript快速上手 tutorial. Here’s the Bilibili link.

Helloworld

A new script will contain two basic functions. I’ve already written about these two functions in my Godot notes, so I won’t repeat them here.

Use the print keyword to output content:

1
print("helloworld")

Syntax

There are only a few things to know from this part of the video:

  1. Indentation determines code structure
  2. Case sensitivity

Indentation indicates归属, as shown in the code:

1
2
func _ready() -> void:
$logo.rotation_degrees = 90 # Adding an indent here tells the engine this line belongs to the _ready function

Modifying Nodes 1.0

Just outputting to the console is boring. Nobody playing a game wants to look at your console.

As mentioned in the previous article, nodes are an important part of Godot. We can modify node properties through references in scripts.

There are two ways to bind a node. The first is the manual reference syntax mentioned in the previous article.

The second is to drag the node directly from the node panel into the IDE, which will auto-generate the statement.

A typical reference format looks like this:

1
$Label.modulate = Color.GREEN  # referenced_node.property = modified_property

Key Binding

  1. Create an input map event

First, open Project - Project Settings - Input Map.

Create a new mapping, then press the plus sign to bind a key. Following the example from the video: Create an event named my_action that changes the Label’s color when the spacebar is pressed.

  1. Call the input map event in a script

The system function for input binding is the _input function, formatted as follows:

1
2
3
4
5
6
func _input(event):
if is_action_pressed(my_action): # while key is held
$Label.modulate = Color.RED

if is_action_released(my_action): # when key is released
$Label.modulate = Color.GREEN

Variables 1.0

A variable is a property — the name is self-explanatory, so I won’t elaborate.

The syntax for defining a variable is as follows:

1
var varname = 20

Variables can be changed using operators:

1
2
3
4
5
6
varname = 20        # direct assignment
varname = 20 + 30 # computed assignment
varname += 10 # add and assign
varname -= 10 # subtract and assign
varname *= 2 # multiply and assign
varname /= 2 # divide and assign

Reference the key binding section above to change variable values when performing corresponding actions.

If Statements

Standard Format

Seriously, this overlaps heavily with other languages, so I’ll just write the format:

1
2
if varname <= 0:
print("ur dead")

If you’re a complete beginner who stumbled onto this by accident: in a nutshell, if the condition after if is met, then the corresponding action is executed.

Condition Format

Both conditions must be met, or at least one:

1
2
3
4
5
if varname <= 0 and value = 1:
print("and condition")

if varname <= 0 or value = 1:
print("or condition")

else/elif Format

The “then” in “if…then…”:

1
2
3
4
5
6
if var > 0:
print("type 1")
elif var > 50:
print("type 2")
else:
print("type 3")

Comments

1
2
# Hash symbol, explanation complete
# This really is Python

Variables 2.0

A variable declared inside a function can only be used within that function. But if declared in a broader scope, it can be used throughout the entire script.

The former is called a local variable, the latter a global variable.

Common Data Types

GDScript is a dynamically typed language. Unlike statically typed languages, you don’t need to declare the variable type before assigning a value, and types can be converted directly.

int: integer float: decimal bool: boolean string: string

Data types can be changed by adding the corresponding conversion:

1
2
3
var number = 42
var text = "Meaning of life: " + str(number) # str converts the integer to a string
print(text)

But if you want static typing, you can define it like this:

1
2
var names: int = 114     # explicitly specifying the data type
var names := 1919 # automatic type inference, consistent with static typing

Vector Data Types

Godot uses Vector2 and Vector3 to represent 2D and 3D coordinates, typically used for positions.

1
2
var position = Vector3(1, 2, 3)  # initialize coordinates
position.x += 2 # increment the x coordinate

Constants

The keyword is const:

1
const constant = 10
0条搜索结果。