a

Can I use code to set up the form instead of using the graphic interface?

agvamo

I don't like setting up things by clicking and dragging. How can I use code to set up my scene?

  • Nathan Lovato replied

    You can set up the user interface with imperative code and generally about everything you do in the editor you can do in code in Godot.

    A little warning: it will be way less efficient to do everything with code for UI design. It makes iterating on the placement and look of things much slower, so I would recommend getting comfortable with working with the editor.

    That said, here's an example of how you could set up three UI nodes with code: a Label in a VBoxContainer in a PanelContainer. It's also important to know how to do that for the cases where you need to create new UI nodes at runtime.

    var panel_container = PanelContainer.new()
    add_child(panel_container)
    # This is equivalent of using the Layout menu in the editor toolbar
    panel_container.set_anchors_and_margins_preset(Control.PRESET_WIDE)
    
    var vbox_container = VBoxContainer.new()
    panel_container.add_child(vbox_container)
    vbox_container.set_anchors_and_margins_preset(Control.PRESET_WIDE)
    
    var label = Label.new()
    vbox_container.add_child(label)
    label.text = "Hello World!"

    You create the nodes and add them as children of one another, like how you would do in the editor.

    1 love