Godot Basics: Nodes, Resources, and Scripts
This article is an introduction to the Godot Game Engine, we’ll cover the editor and some core concepts at a high level. This article is part of the Godot Basics series.
The Godot Editor
The Godot has a relatively simple and friendly editor once you get a bit familiar you’ll really enjoy working with it!
Above is a screenshot of the Godot editor with our snowboarding game Matterhorn open.
Main Area
- Top left: all of the nodes in the current scene tree (see Nodes).
- Bottom left: all assets, saved scenes, scripts, etc.
- Top middle: editable visualisation of the nodes in the scene.
- Bottom middle: console output that displays messages from the editor and the running game via
print(...)
. - Right: inspector displaying configuration for the currently selected node.
Toolbar
- Left: the usual menus for saving, exporting, setting project configuration, etc.
- Middle: buttons to switch between 2D, 3D and Script views.
- Right: play button, etc to run the current scene.
Nodes
Nodes are the building blocks of a game in Godot. There are nodes for sprites, rigid bodies (physics), audio, etc pretty much anything you’d want to do in a game. Nodes are organised into a tree structure for example a player node might contain a sprite node to display a player and a rigid body to handle physics.
Some nodes require resources for example a sprite node needs a texture resource. Nodes can have scripts attached to them to perform additional behaviour.
Saving and Reusing Nodes
Nodes (including their children i.e. a branch of the tree) can a saved to a scene file for reuse. These saved scenes can be added to a scene in the editor or programmatically in a script.
Loading a saved scene in code is achieved using the instance()
method on a PackedScene
for example:
extends Node
export var packed_scene: PackedScene
func init():
var node = packed_scene.instance()
add_child(node)
Resources
Resources are data such as textures, audio, mesh data, etc which are used by nodes. Resources are either a file e.g. an image file or a built-in resource such as a curve resource in a path node.
Scripts
Scripts allow nodes to have additional behaviour for example move left when the player presses left or open a door when an area is entered. A script can be added to a node by right-clicking and attaching a script.
The default scripting language in Godot is GDScript a simple dynamically-type Python-like language. Here’s a little taste of GDScript, the script is the movement logic driving the snowboarder in Matterhorn.
extends KinematicBody2D
export var speed = 100
export var turn_speed = 0.1
var angle = 0
func _process(delta):
if Input.is_action_pressed("ui_left"):
angle -= turn_speed * delta
elif Input.is_action_pressed("ui_right"):
angle += turn_speed * delta
angle = clamp(angle, -PI * 0.4, PI * 0.4)
func _physics_process(_delta):
move_and_slide(speed * Vector2(sin(angle), cos(angle)))