best node to hold an array of 2d nodes

jackhenjackhen Posts: 19Member

Is there a 2d node type i can use to hold an array of 2d nodes in a horizontal line that will automatically adjust their positions based on what order they are in the array and keep their separation the same?


  • TwistedTwiglegTwistedTwigleg Posts: 2,810Admin
    edited July 24

    If you were using a Control-based node, I'd say the HBoxContainer node is what you'd need. For Node2D-based nodes, the HBoxContainer won't work though, and I don't think there is anything built in that would handle it. That said, you can always program your own solution.

    For example, something like this (untested):

    extends Node2D
    export (float) var separation_distance = 24
    func _ready():
    # For this example, we'll just sort all of the children nodes.
    # Additionally, we'll place the Node2D nodes starting at this
    # node's position, and expand from there on the horizontal axis.
    func arrange_nodes():
        # some children may not be Node2D-based, so we don't want to
        # move them or account for their position.
        var position_index = 0
        for child_node in get_children():
            if child_node is Node2D: # if it extends Node2D...
                # position the node and increase the position index so
                # the next node is offset correctly.
                child_node.position = Vector2(position_index * separation_distance, 0)
                position_index += 1

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file