0 votes

Hi! :)

Currently following a tutorial - https://www.youtube.com/watch?v=sKuM5AzK-uA

I can't seem to work out the syntax (code is GDScript) to write it into C#

What would be the equivalent/translation of this line in C# from GDScript?

for button in $Menu/CenterRow/Buttons.get_children();

Thank you!

in Engine by (53 points)

1 Answer

0 votes
Best answer
foreach (Button button in GetNode("Menu/CenterRow/Buttons").GetChildren())
{}

But it will throw an exception if not all children are a Button, if you need to check if the node is a Button then use this:

    foreach (var obj in GetNode("Menu/CenterRow/Buttons").GetChildren())
    {
        var button = obj as Button;
        if (button != null)
        {
            GD.Print($"{button.Name} is a button");
        }
    }

Another safe assumption is to cast to Node:

 foreach (Node node in GetNode("Menu/CenterRow/Buttons").GetChildren())
{}

It depends if you need to use button functionality or not.

by (978 points)
selected by

Thank you! And thank you for the tips, extra info and tips !:)

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 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 webmaster@godotengine.org with your username.