3D Card Spread - How to implement a BoxContainer in a 3D project

New to coding; though I’m picking up on it a lot quicker than I’d expect. But I can’t wrap my head around this one thing and it’s driving me insane.

How on earth can I achieve a BoxContainer-like effect with 3D nodes?

Let’s throw out some placeholder nodes to work with. I have a Node3D named Hand as a child of the Player scene, and several instances of a scene called “Cheese.”

Each of these Cheese instances is going to be parented to the Hand, and while in their resting state they’re going to be visually “in” the hand. Problem is, I’d like them to spread out along the X axis- dynamically- as if they were cards in a card game… and I just don’t get the basics of how I’d achieve this.

I know I’d probably need some variable stored in each separate Cheese instance to determine their spread, and I know I’d change it by accessing the child array of the Hand, but I don’t know:

  • how to reliably get specific children via _get_child() (index access just doesn’t seem practical when children are gonna be coming and going frequently in the finished product)

  • how to establish the hand boundaries

  • for all intents and purposes… pretty much any aspect of this method; I can’t even come up with some rough proto-code, this is just a massive gap in my understanding (or I’m overthinking it)

I mean I’ve tried to research, but using the documentation for any given thing requires a context-specific level of understanding that’s just not there right now, and the only tutorial that exists anywhere for this kinda thing lacks any actual information on the method/code (and is more of a concept demonstration).

The basic way to have some objects aligned to a grid is to place them with equal spacing. So if the spacing is 1 meter, then the objects will be placed at x = 0, x=1, x=2, etc.

You can use a simple formula to do this:

var spacing = 1.0
func place_items():
    for i in range(items.size()):
        var item = items[i]
        item.position.x = i * spacing

The starting location can additionally be added to the position in order to not start the grid at 0.

The index of the item is used to place it. But I’m not sure exactly how you want the items to be ordered. If you want them in a different order, you should sort the array of items to be in the order you want.

Also, if you want a specific child, and the index can’t be used, you could use groups instead by assigning the specific children all to one group.

Thanks. Think I’m starting to understand but as is wouldn’t that push the children out in a line? As opposed to sorting them around the center.

You can offset all positions by half the total size of the children in order to center it.

item.position.x = i * spacing - (items.size() * spacing / 2)