0 votes


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!

in Engine by (22 points)

1 Answer

+2 votes
Best answer

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

by (521 points)
selected by

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?

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected]godotengine.org with your username.