#Getting wrong global_position from Panel object array within GridContainer

1 messages · Page 1 of 1 (latest)

placid elk
#

I created a UI element for a Resident Evil 4-like inventory system. Inside it is a GridContainer that holds all the slots that are added as children in code using a for loop. For some reason when I code in an icon to occupy a slot position's global_position it keeps going to origin of the GridContainer instead of the slot. When I print each slots' global_position within the main loop it is showing the origin of the GridContainer despite being arranged properly within the GridContainer. If I interact with the any slot with a mouse click and print the global_position it displays the correct position. Could this be related on how things are drawn/rendered in Godot, and if so, how would I go about fixing it?

Any ideas?

#
# Generates Slots within GridContainer
func generate_slots():
    #--------------------------------------------------------------------------
    # Creates a 2-Dimensional Array and initializes Slot data
    for row in range(rows):
        slots.append([])
        slots[row] = []
        for column in range(columns):
            slots[row].append([])
            slots[row][column] = slot_class.instantiate()
            grid.add_child(slots[row][column])
            slots[row][column].name = ("Slot[" + str(row) + "][" + str(column) + "]")
            if grid.get_child_count() == rows * columns:
                all_slots_init = true
                print("All slots initialized.")```
#
# Moves ItemIcon to GridContainer Slot's global position
func set_icon_position(icon: ItemIcon, slot: Slot, offset: Vector2):
    var icon_global_pos_end = icon.global_position + icon.size
    var grid_global_pos_end = grid.global_position + grid.size
    #--------------------------------------------------------------------------
    # Moves ItemIcon then checks for Container overhang
    icon.global_position = slot.global_position + offset
    if icon_global_pos_end.x > grid_global_pos_end.x:
        icon.global_position.x = (grid_global_pos_end.x + 5) - (icon.grid_size.x * 50)
    if icon_global_pos_end.y > grid_global_pos_end.y:
        icon.global_position.y = (grid_global_pos_end.y + 5) - (icon.grid_size.y * 50)```
craggy rock
#

I have this issue as well. I make a chessboard using the GridContaienr then assign pieces to the globalpositions of the squares. Unfortunately, since I'm instantiating pieces the same frame I load board squares, sometimes this issue makes all my pieces pile in the top corner.

I kinda made it work by awaiting a few frames before querying my positions (that worked because I do it once on startup and it isn't noticible to the player); I also made it work by registering all the pieces to the item_rect_changed event of each square and repositioning when the squares were sorted--but that's pretty hacky too and causes other issues.