get_node() question in the official scripting tutorial

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Godotfan


I’m a beginner for both Godot and C# and I’m following the tutorial of documentation and got some questions.

Here is the codes form tutorial.

using Godot;

// IMPORTANT: the name of the class MUST match the filename exactly.
// this is case sensitive!
public class sayhello : Panel
    public override void _Ready()
        GetNode("Button").Connect("pressed", this, nameof(_OnButtonPressed));

    public void _OnButtonPressed()
        var label = (Label)GetNode("Label");
        label.Text = "HELLO!";

Why the #1 which in the tutorial, it cannot simply work as #2 or #3. Also what’s (Lable) in the var label = (Label)GetNode("Label"); means here?


var label = (Label)GetNode("Label");
label.Text = "HELLO!";


GetNode("Label").Text = "HELLO!";


var label = GetNode("Label");
label.Text = "HELLO!";

Thanks a lot!

:bust_in_silhouette: Reply From: fpicoral

I don’t know for sure since I don’t use C# but it looks like that, in C#, when using GetNode before saying the path to the node, in this case "Label", ypu need to say what is the node that you are getting, in this case, a (Label), resulting in (Label)GetNode("Label");. #2 and #3 don’t work because you did not put the (Label) before GetNode.

Let supose that you have the following scene tree:

-Map (Node2D)
|_Player (KinematicPlayer2D)
    |_PlayerSprite (AnimatedSprite)

And lets also supose that you want to get the node PlayerSprite that is an AnimatedSprite. You would need to do

var player_sprite = (AnimatedSprite)GetNode("Map/Player/PlayerSprite"); 

To save it as a variable and use it multiple time or only

(AnimatedSprite)GetNode("Map/Player/PlayerSprite").property = ...; 

To change a property once.

As I said, this might be wrong, I don’t work with C#.
I would recommend you to try GDScript, it’s powerfull and fast as C# but it’s waaay easier. In GDScript all you would have to do to get the PlayerSprite node from the example above was var player_sprite = $Map/Player/PlayerSprite

Thanks a lot for answering. I think I have figured out why.

GetNode() will have you get a node variable and the variable what you get it’s the node type.

In this piece code (Label)GetNode("Label"); (Lable) means convert the got node type variable into Label type. To do this because Text is a method from “Lable” class, you can not call it from the “Node”. Have a (Lable) at the front just for calling Text method from the Label.

That is also why GetNode("Button").Connect("pressed", this, nameof(_OnButtonPressed)); works without a (Button) at front of GetNode becuase Connect is a method from the “Node” not “Button”.

I guess In python or GDScript everything done automatically.

BTW do you know where I can find C# version API document to look at these class?

Godotfan | 2019-02-13 11:42

:bust_in_silhouette: Reply From: jeancallisti

Nowadays you can do this:
var typedNode = GetNode< NodeType >(“node_name”)

The fancy name is : parametrized method (in this case: parametrized GetNode).

It’s better practice than casting with (NodeType)