Skip to main content

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

AutoUi for GameMaker

A UI system for GameMaker for responsive, dynamic interfaces in seconds · By ome6a1717

Switching from one window to another cleanly?

A topic by PsychicParrot created 4 days ago Views: 28 Replies: 5
Viewing posts 1 to 3

Hi,

Something I've been struggling with is how to go from one UI window to another. As an example, the main menu to the options menu.

As far as I can tell, we have every different UI window as a different object - so I'm spawning the new UI object (w/ instance_create) when I need to jump to another window, and then trying to destroy the object for the previous UI.

When I try to use instance_destroy() in the UI I want to close, it often causes an error like (below I was trying to open a new UI and close the current one):

ERROR in action number 1 
of  Step Event0 for object obj_autoGUIMenu_parent: not an instance  
at gml_Script_editLib@gml_Object_obj_DayResults_Create_0 (line 38) -         
instance_destroy(); 
############################################################################################ 
gml_Script_editLib@gml_Object_obj_DayResults_Create_0 (line 38) 
gml_Script_anon@211@gml_Object_obj_DayResults_Create_0 (line 9) - global.options[0].set_click_func(function() { editLib(); }); 
gml_Script_zz_private_check_if_hovering@anon@17422@AutoUiElement@AutoUiElement (line 426) 
gml_Script_step@anon@7389@AutoUiButton@AutoUiButton (line 179) -         zz_private_check_if_hovering(); 
gml_Script_step@anon@13630@AutoUiFlexContainer@AutoUiFlexContainer (line 386) -                 _item.step(_xx,_yy); 
gml_Script_step@anon@13630@AutoUiFlexContainer@AutoUiFlexContainer (line 384) -                 _item.step(__xx,__yy);     
gml_Object_obj_autoGUIMenu_parent_Step_0 (line 1) - flex.step(display_get_gui_width()/2, display_get_gui_height()/2); 
gml_Object_obj_DayResults_Step_0 (line 2)

So, I guess this is not the correct way to do it. What is the best way to 'shut down' a UI? Do I need to destroy the containers first? 

I tried having instance_destroy() on a timer for 1 tick but that made no difference either. Instead, I've been adding code to the 'next' UI to close the previous one, but it's a bit messy so I'm hoping there's a cleaner way to do it?

Thanks for your help!
Jeff.

Developer

Are you calling instance destroy in set_click_func?  If so, generally the scope will be the element and not the base object, so you’ll have to pass in the obj_autoGUImenu id as a parameter and destroy that. 

Gah! I've been bitten by scope haha thanks Ome6a. I have a function that gets called by set_click_func so yeah.. that's it! I'll use the id instead :)

Developer

I've found scope to be very difficult to figure out in GML when using script_execute, to be honest.  For instance, what you did will work fine for windows/macos export, but if you export to HTML5, it will sometimes throw an error that it is unable to find your element.


It's generally just best practice to pass in all objects you use or reference for safety:

my_button = auto_button(/* data */);
my_button.set_click_func(function(_obj, _btn) {
    _btn.color = c_white; // eg.
    _obj.variable = 2; // eg.
}, [self, my_button]);

Awesome!

My set click now looks like this:

options[0].set_click_func(function(objid) { edit(objid); }, [self]);

And that's passing in the object so I can use instance_destroy to close it right in the function. Perfect!

Just incase you're wondering, I'm using a function in the function so I can call the same code for both controller and mouse. My controller code calls the function from Step, so I didn't want to duplicate that code in set_click_func as well as there.

Anyhoo - thanks again for your help I really appreciate it :)

Developer

Of course!