Godot - Creating and Aligning Multiple Buttons
This code example shows how to create and align multiple Button nodes using GDScript.
The following code example creates multiple Button nodes using Button.new()
and aligns them horizontally.
The `size` property is used to easily specify the position of each Button node.
If you do not use the `size` property, you can calculate the rightmost position by adding `size` and `position`.
# Create multiple Button nodes and align them horizontally
for i in 5:
var button = Button.new()
add_child(button)
button.set_text("Button")
button.set_size(Vector2(100, 50))
button.set_position(Vector2(120 * i, 0))
The following code example creates multiple Button nodes using Button.new()
and aligns them vertically.
# Create multiple Button nodes and align them vertically
for i in 5:
var button = Button.new()
add_child(button)
button.set_text("Button")
button.set_size(Vector2(100, 50))
button.set_position(Vector2(0, 70 * i))