Root node & first script
Configuring the root node
Now you know how to open the project in Godot, its time to do some coding so you get a basic understanding how Godot to connect game logic to your Godot game.
We don't have anything to apply logic to yet however, so first we need to create a script file. The most common place to start a script is the root node. There is only one problem, our root node is a Node3D. If we want a game with some kind of user interface, we need a proper structure.
Node tree based system
If you're unfamiliar with Godot's Node based system, its basically like a hierarchical scene tree with items that can be combined together based on type. Each node has a type is represented by a different icon & can be given a proper name:
Right now however, there is a Node3D as our root node, so even though Godot allows non 3D nodes inside it, it will be awkward to have as root node. That is because a 3D node is supposed to work in 3D space & we don't want our user interface to move in 3D.
To change that right click on Node3D & click change type
& choose Node
.
Now the icon changed, but the node is still called Node3D
. Click on it &
press [F2]
on the keyboard to change its name to Main
.
Attaching logic to the root node
Now the root node is set up, its finally time to add some logic.
Right click on the Main
& click Attach script
:
In the window that appears, fill in the following details & make sure C# is selected as language:
Then click Create
to create the Main.cs script.
Godot will create an unsafed Main.cs & opens it in its own built in editor:
To start simple, we're just going to print a simple log message inside the
_Ready
method. Don't worry right now about the details as we'll cover these
later. For now just update the _Ready
method so it looks like this:
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
GD.
}
Don't worry about the odd code right now. This will be covered later.
For now save the script ([CTRL]+[S]
) & press [F5]
again. This will trigger Godot
to show problems with your source code in the problems tab as the code isn't yet
completed:
After we set up VSCode however, after you type GD.
a list of options should
pop up hinting that you still need to type something:
It will even give an explanation about GD which is Godot's global function class where you can find the most common functions that you need in a Godot game.
What we need now however is something simple so we know our C# logic is working. So change
GD.
into
GD.Print("Main scene started ...");
and press [F5]
again.
This time something will appear in the output section of Godot instead: