Several actions in Godot are triggered by callbacks or virtual functions, so there is no need to write code that runs all the time.
However, it is still common to need a script to be processed on every frame. There are two types of processing: idle processing and physics processing.
This method will be called every time a frame is drawn, so it’s fully dependent on how many frames per second (FPS) the application is running at:
The delta parameter contains the time elapsed in seconds, as a
floating point, since the previous call to
This parameter can be used to make sure things always take the same amount of time, regardless of the game’s FPS.
For example, movement is often multiplied with a time delta to make movement speed both constant and independent from the frame rate.
Physics processing with
_physics_process() is similar, but it should be used for processes that
must happen before each physics step, such as controlling a character.
It always runs before a physics step and it is called at fixed time intervals:
60 times per second by default. You can change the interval from the Project Settings, under
Physics -> Common -> Physics Fps.
_process(), however, is not synced with physics. Its frame rate is not constant and is dependent
on hardware and game optimization. Its execution is done after the physics step on single-threaded games.
A simple way to test this is to create a scene with a single Label node, with the following script:
Which will show a counter increasing each frame.
Nodes can be added to groups, as many as desired per node, and is a useful feature for organizing large scenes. There are two ways to do this. The first is from the UI, from the Groups button under the Node panel:
And the second way is from code. One example would be to tag scenes which are enemies:
This way, if the player is discovered sneaking into a secret base, all enemies can be notified about its alarm sounding by using SceneTree.call_group():
The above code calls the function
player_was_discovered on every
member of the group
It is also possible to get the full list of
enemies nodes by
The SceneTree class provides many useful methods, like interacting with scenes, their node hierarchy and groups of nodes. It allows you to easily switch scenes or reload them, to quit the game or pause and unpause it. It even comes with interesting signals. So check it out if you got some time!
Godot has a system of notifications. These are usually not needed for scripting, as it’s too low-level and virtual functions are provided for most of them. It’s just good to know they exist. For example, you may add an Object._notification() function in your script:
The documentation of each class in the Class Reference shows the notifications it can receive. However, in most cases GDScript provides simpler overrideable functions.
Such overrideable functions, which are described as follows, can be applied to nodes:
As mentioned before, it’s better to use these functions instead of the notification system.
To create a node from code, call the
.new() method, like for any
other class-based datatype. For example:
To delete a node, be it inside or outside the scene,
free() must be
When a node is freed, it also frees all its children nodes. Because of this, manually deleting nodes is much simpler than it appears. Free the base node and everything else in the subtree goes away with it.
A situation might occur where we want to delete a node that is currently “blocked”, because it is emitting a signal or calling a function. This will crash the game. Running Godot with the debugger will often catch this case and warn you about it.
The safest way to delete a node is by using Node.queue_free(). This erases the node safely during idle.
Instancing a scene from code is done in two steps. The first one is to load the scene from your hard drive:
Preloading it can be more convenient, as it happens at parse time (GDScript only):
var scene = preload("res://myscene.tscn") # Will load when parsing the script.
scene is not yet a node. It’s packed in a
special resource called PackedScene.
To create the actual node, the function
must be called. This will return the tree of nodes that can be added to
the active scene:
The advantage of this two-step process is that a packed scene may be kept loaded and ready to use so that you can create as many instances as desired. This is especially useful to quickly instance several enemies, bullets, and other entities in the active scene.