Skip to content

Destroying Panels

Once a panel has served its purpose during the game, you should destroy it. gooey has two methods to destroy a panel: the built-in method using a close button, and manually using a widget method.

Destroying a panel recursively destroys all descendant widgets associated with it.

Built-in method: setting up a close button

Panels have specific methods for setting up a close button. Upon clicking this button, the panel will get destroyed.

To do this, you can use setCloseButtonSprite(). Once you set a sprite resource as the close button, gooey will create an button (an actual UIButton) under the hood, that handles destroying the panel when clicked.

    _panel.setCloseButtonSprite(spr_CloseButton);

Default anchor point for the close button

By default, gooey sets TOP_RIGHT as the anchor point for the button, but you can change this by using the setCloseButtonAnchor() method.

Example

Continuing the example from the panel basics article, I now set up a close button for the panel. I use gooey's fluent interface to set up a custom offset for the button (which, since I'm not setting a different anchor point, will be applied starting from the TOP_RIGHT anchor point). I provide a struct with x and y offsets (negative 16px horizontal and positive 16px vertical):

    _panel.setCloseButtonSprite(spr_CloseButton).setCloseButtonOffset({x: -16, y: 16});

The result is the following (click and drag the panel around, and then use the close button to destroy it:

If you enable the INFO message level in the Configuration script (as explained in the Fundamentals article), you can see that, when you destroy the panel, the following info is displayed in the log:

[gooey] <INFO> Destroying widget with ID 'MyPanel' from containing Panel 'MyPanel' on tab undefined
[gooey] <INFO> Destroying widget with ID 'MyPanel_CloseButton' from containing Panel 'MyPanel' on tab -1
[gooey] <INFO> Destroyed widget 'MyPanel_CloseButton'
[gooey] <INFO> Destroying widget with ID 'MyPanel_TabControl_Group' from containing Panel 'MyPanel' on tab -1
[gooey] <INFO> Destroying widget with ID 'MyPanel_TabControl_Group_TabButton0' from containing Panel 'MyPanel' on tab -1
[gooey] <INFO> Destroyed widget 'MyPanel_TabControl_Group_TabButton0'
[gooey] <INFO> Destroyed widget 'MyPanel_TabControl_Group'
[gooey] <INFO> Destroyed widget 'MyPanel'

The first line describes the instruction sent (destroy the panel), and then the system displays messages each time it (recursively) destroys child widgets.

What the heck are the other widgets mentioned?

If you take a closer look, you can see the system is destroying a "tab control" group and a corresponding button, which we did not explicitly create! You can learn more about why this is in the Tabs article.

Manual method: destroying with a method

The other way to get rid of Panels (or any other widget) is to use the destroy() method. You can use this to destroy the panel under a specific game circumstance or on another widget's event callback, for example. The syntax is straightforward:

    _panel.destroy();

The manager object's cleanup() method

The invisible manager object that does gooey work under the hood will automatically call its cleanup() method on GameMaker's Clean Up event. This will destroy all currently created panels, one by one, by internally calling the destroy() method for each of them.

You can read more about the Clean Up event in the GameMaker manual. Keep in mind this means that panels will be automatically destroyed when changing rooms. However, if you keep your code organized and place the creation code on a global function or instance method variable, you will be able to recreate them on demand.