Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

Sharing another snippet of something I had to kinda of workaround.

I have a couple UIPanel widgets that I add as children to a grid. Upon closing the room I call a function to remove all widgets that are 'bound' to the room. Sadly panels that are parented to a Grid don't fully delete themselves. I had to use this workaround to get them to completely delete:

if (_widget.getType() == UI_TYPE.PANEL) {
    _widget.__parent.remove(_widget.getID());
    UI.__destroy_widget(_widget);
}
_widget.destroy()


Full snippet for the logic to register and later remove widgets when the room ends:

global.widgets_to_remove = []
// Registers a widget that will be removed when the room ends
function widget_bound_to_room(_widget)
{
    array_push(global.widgets_to_remove, _widget)    
    return _widget
}
// Called on room end to remove bound widgets
function widgets_remove_room_bound()
{
    for (var _i = array_length(global.widgets_to_remove) - 1; _i >= 0; _i--) {
        var _widget = array_pop(global.widgets_to_remove)
        
        custom_event_call("widget_removing_room_bound", {
            widget_to_remove: _widget    
        });
        
        // Workaround for panels not removing correctly
        if (_widget.getType() == UI_TYPE.PANEL) {
            _widget.__parent.remove(_widget.getID());
            UI.__destroy_widget(_widget);
        }
        
        _widget.destroy()
    }
}


To use it:

1. When creating widgets call widget_bound_to_room on it, e.g:

var _crafting_group = widget_bound_to_room(new UIGroup("CraftingGroup", 0, 0, 250, 0, spr_back, UI_RELATIVE_TO.MIDDLE_CENTER))
_crafting_group.setInheritHeight(true)
_crafting_group.add(widget_bound_to_room(new UIPanel("ParentedPanel", _x, _y, _width, _height, spr_frame_simple)))


2. When the room ends call:

widgets_remove_room_bound();
(1 edit)

Hi again, 

UIPanels are not meant to be nested (at least I didn't design it that way!), so I'm interested in your use case for that and how are you implementing it. I can see you would have problems destroying them, but there might be other things that don't work as you think, just because again, when I thought about UIPanels I always conceived them as top-level widgets.

It would be really useful if you could share your code and/or images to see how you are using them nested. 

Best,

manta ray

(2 edits)

Heya again,

Thanks for explaining that the UIPanels weren't meant for that. I used them to create panels with a background sprite, but now that you said that I was doing something unintended- I double-checked the docs and found that a UIGroup can have a background sprite as well.

I guess the term 'panel' gave me the illusion that they would be similar to WinForms Panels, but they're more akin to Forms there I guess. I've switched to UIGroups for my purposes, but will leave the code snippet up since it may be useful to someone else for other reasons.

Thanks again for your quick support!