Jump to content

Guide to the Godot game engine/What is a node

100% developed
From Wikibooks, open books for an open world

Nodes are the building blocks of Godot. They can be combined into trillions of different combinations. There are many different types of nodes, like Camera2D and Sprite2D.

The 3 main node types

[edit | edit source]

Every node goes under this one. It is the basic node that defines all others. See it as the parent and the grandparent of Godot and its nodes, and all the games that Godot has been used in. It stores common things like name and script. It can be used instead of CanvasItem or Spatial when making objects that store data, and are not needed to be shown on the screen or have a position.

CanvasItem

[edit | edit source]

Unlike Node, Canvas Items are positional, and implement draw code. They are used for all 2D objects, and have two main node types that inherit it:

Used for the GUI. It contains options like whether to ignore mouse input, receive it and let it pass to other UI, or receive it and stop it reaching other UI. They also allow margins and anchors, allowing a Control node to stay in a certain part of the screen or stretch when the window expands or shrinks.

These are used for 2D game elements, like Sprite2D and PhysicsObject2D. They are drawn on the canvas, which is drawn on top of the 3D view. Well, its actually the other way around. 3D is actually drawn in the background of the canvas, if there is an active Camera3D in the scene.

Used for 3D game elements. They store positional data, rotation, and scale. They do not draw onto the screen. For this, you need to use the VisualInstance node that inherits Spatial. You need an enabled Camera3D in the scene to actually see the 3D world.

Making custom nodes

[edit | edit source]

Firstly, you need to decide what it'll do. Is it some variant of Timer? Or is it a custom Button?

After you decided what it does, you must decide what Node it inherits. This is important. You don't want to use Control if you want a 3D button, or Spatial for a timer.

In my opinion, this is the recommended node for each type of object you want to make:

Node: Use for objects that won't be part of the GUI, 2D or 3D worlds. Existing examples are Timers, HttpRequests and CanvasLayer.

Node2D: Objects for games that are not used in the GUI. There are exceptions, but in general, don't mix up Node2D and Control too much.

Control: For GUI, like menus, HUDs and certain game elements that require anchors and margins.

Spatial: Used only for 3D.

For more on making nodes, please see Making nodes.

Inheritance

[edit | edit source]

Each node has several properties. Like Node with its name property, or Node2D with positional data. Every node that inherits it gains the properties of it. A brief example:

Node: name, script, others
  • Spatial: properties of Node; position; rotation; scale, others
  • CanvasItem: properties of Node; visibility; others
    • Node2D: properties of CanvasItem and of Node; position; rotation; scale; others
    • Control: properties of CanvasItem and of Node; rect position, size and scale; rotation; others
      • ColorRect: properties of Control, CanvasItem and Node; color

SceneTree

[edit | edit source]

Godot has building blocks, yes, but what's the glue that sticks it all together?

The SceneTree is the glue, and it's a strong glue. But it is also a flexible glue. It is an Object (which is the great-great grandfather/mother of all Godot "things").

The SceneTree is where everything happens. Nodes process in a certain order, draw on the screen, and more.

Note:

Throughout tutorials found in this book, I will mention add something as a child of something else. This means adding a node child of another node. A Button may be the child of a Container, which may be the child of a normal Control.

When making your game, each menu, every level, and every object are all scenes. You save them on the file system (or "fs" for short), and can then put those scenes within scenes. You could make an enemy scene, save it, then put it in your "level 1" scene as many times as you like. This is called instancing a scene.

Inheriting a scene can be considered the opposite to instancing. An inherited scene is like a copy of a scene, but with limited freedom to make modifications. This is useful to have a base enemy scene, then inherit it for each enemy you add, and make your modifications. This keeps your enemies consistent, and makes each file easier to understand. This is quite similar to inheriting a node, except that multiple nodes are inherited instead of making a new inherited script.

Note:

Do not confuse inheriting a scene with inheriting a node/script.

If, for example, on "Level 6", you have to fight a mutant version of a common enemy found throughout the game. You want to use it only once in the entire game, so is not worth inheriting the enemy scene and making a whole new file for an enemy used only once. Instead, while on level 6, right click on the enemy you want to modify in the Scene dock. Check the "editable children" box, and make your modifications. It is almost exactly the same as inheriting the scene, except that you are not making a new file.

If you decide that the mutant enemy would do well on other levels, or you use it more than once in a single level, left click on the enemy on the Scene dock and click "save branch as scene". This will save the node as a scene a with your modifications to be reused on any other level.

Warning:

If you rename or move the node with editable children enabled, you will need to re-enable it or changes will be lost!

Changes will be lost also if you disable it manually and forget to enable it before closing the scene or editor.

Adding a node

[edit | edit source]

Okay, so now you know what you can do, how do you do any of it?

Lets create a simple scene. Open the Godot project manager. On the left is a list of buttons. Click New Project. A popup should appear. Press Browse under "Project Path:" and choose what folder to store your game. It's a good idea to have all your games in the same folder so they can be easily accessed. Once that is done, press "Create Folder", type something like "Godot test project", and click OK. Then click "Select Current Folder", and Create & Edit.

Now Godot should open. On the left, there should be a Scene dock with a few buttons on it. Click User Interface. Double click the bit that says "Control" and rename it to Test UI.

Now, press the white "+" icon in that dock, and a popup should appear. In the search bar, type Label. Double click the highlighted menu item for Label.

On the left, in the Inspector dock, look for the property Text. Type Hello world! (Make sure your Label is selected on the Scene dock). At the top of the screen is a Layout button. Click it. A drop down menu will appear with many options. Press Center. The "Hello world!" text appears at the center of the blue box in the 2D viewport (the large bit in the middle of the editor).

The layout button

Now you know how to add a node, how do you delete them if you make a mistake? The fastest way is to undo (press "Control+z" on Windows). If the node is not recently created, instead left-click on it in the Scene dock. Click Delete Node(s). If you delete a node you didn't want to delete, just press undo. If you press undo too many times, you can redo ("Control+Shift+Z" on Windows).

Now save your scene with "Control+S". Don't change the file name. You can also save to a different scene with "Control+Shift+S".

Instancing and editable children

[edit | edit source]

To save a node and its children as a separate scene file, left click on the node and click Save Branch as Scene. For this experiment, do so on your Label. The file name should be Label.tscn. Press "Save".

Find your Label scene but do not open it. Drag and drop it over your "Test UI" node in the Scene dock. At first it appears on the dock, but not on the viewport. This is because both Labels are on top of each-other. Click on your second Label in the Scene dock and click the move button at the top of the screen (or press w). Drag in any direction to see the new Label.

Open the Label.tscn scene and and add another Label as a child of the root node (which should also be a label). Change the text property to "Label number 2". Save the scene. Go back to the Test UI scene and notice how both the new and old labels overlap? Go back to the Label scene, select the second Label and move it down by clicking "Shift+Down arrow" 5 times. Save. Go to the other scene. Both Label scenes display correctly, not just one.

On the second label in your scene, left click and check Editable Children. Click the gray Label button (the child of the second Label), and change it's text property to "Edited child". The other Label and the Label.tscn scene were not changed, but one you changed was.

Right click on the Label with editable children and press Make Local. Now the modified label scene is no-longer linked, you can do some finer tweaking like renaming the child or deleting it. You can play around with your new scenes, or continue on.

What you have learned

[edit | edit source]
  1. What nodes are in Godot and how to use each of the three basic ones.
  2. How to instance a scene into another, and how to save existing nodes into a new Scene.
  3. About node inheritance.
  4. How to start a new project.



Guide to the Godot game engine

Getting started [edit]
Installation
What is a node?
Programming
Resources and importing
Signals and methods
Your first game
Making it work
Debugging
Input
Physics
Saving and loading
Multiplayer
Making it look good
UI skinning
Animation
Advanced help
Servers (singletons)
Platform specific
Optimisation
Encryption
Exporting
Plugins
Miscellaneous
Helpful links
Authors and contributors
Print version


<-- previous back to top next -->