GDScript Line Translation into C# help?

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

Hi! :slight_smile:

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!

:bust_in_silhouette: Reply From: davidoc
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.

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

AlMoeSharpton86 | 2019-05-31 00:13