GMS 2.3+ Trying to Destroy Objects

So I have a menu that pops down when a sprite is clicked. The pop-down menu is a parent object to the buttons within. So I made a "Mouse Leave" event on the menu with instance_destroy();
The problem is, now my button objects are being destroyed when the mouse leaves them and I want them to be destroyed when the mouse leaves the menu. How do I make that happen?
 

Japster

Member
If I'm getting you right, I'd just have all of your menu objects have a locally defined 'creator' variable, and set it when you create them. Then, from inside the MENU destroy code, simply have it 'with' loop through any instances and destroy them if the 'creator' ID matches? - Then, you can choose the exact criteria / event to have them be destroyed if met.

with (Obj_buttons)
{
if (Creator == other.id) destroy();
}

I use the same logic in my WIP shooter game - I place Turret Mounts at design time, these then at run-time, spawn Turret bodies, which in turn spawn different rotatable turrets - each sub-object has the same creator ID, so if a player blows up the turret, both component parts are destroyed...

Also, do you need to destroy and re-create? - or maybe just disable and re-enable as needed - might be less work... :)

EDIT: Also, even now I sometimes get confused with referencing my WITH's, OTHERS, etc... maybe it's an omitted 'other.' etc that's causing your issue?
 
First check whether it's a menu or a button before destroying.

Code:
if (isMenu) instance_destroy();
For some reason this isn't working. I tried making a create event declaring the variable "isMenu" and then in the step event:

if (isMenu) {
instance_destroy();
}

But this just destroys the object right away and not when the mouse leaves the menu. Sorry if I'm not picking up what you're saying.
 
If I'm getting you right, I'd just have all of your menu objects have a locally defined 'creator' variable, and set it when you create them. Then, from inside the MENU destroy code, simply have it 'with' loop through any instances and destroy them if the 'creator' ID matches? - Then, you can choose the exact criteria / event to have them be destroyed if met.

with (Obj_buttons)
{
if (Creator == other.id) destroy();
}

I use the same logic in my WIP shooter game - I place Turret Mounts at design time, these then at run-time, spawn Turret bodies, which in turn spawn different rotatable turrets - each sub-object has the same creator ID, so if a player blows up the turret, both component parts are destroyed...

Also, do you need to destroy and re-create? - or maybe just disable and re-enable as needed - might be less work... :)

EDIT: Also, even now I sometimes get confused with referencing my WITH's, OTHERS, etc... maybe it's an omitted 'other.' etc that's causing your issue?
I will try this as soon as I get the chance. Thanks!
 

Mk.2

Member
For some reason this isn't working. I tried making a create event declaring the variable "isMenu" and then in the step event:

if (isMenu) {
instance_destroy();
}

But this just destroys the object right away and not when the mouse leaves the menu. Sorry if I'm not picking up what you're saying.
- add that variable to the create event of all button objects, or that of the button parent if there is one
- use that check in the mouse leave event, no idea why you added it to the step event
- remember to also destroy the button objects before the menu is destroyed
 

rytan451

Member
The pop-down menu is a parent object to the buttons within.
Your menu object is a parent of the button object? That's problematic. Inheritance should only apply to "is a" relationships. So, you could have Boat inheriting from Vehicle, Car inheriting from Vehicle, and Plane inheriting from Vehicle, since Boats, Cars, and Planes are Vehicles (and share the same HavePersonEnter and FillFuel methods). But you shouldn't have Button inheriting from a ButtonContainer (which is basically what a menu is), since a Button is not a ButtonContainer. (However, both Button and ButtonContainer might inherit from a GuiElement.)
 

Japster

Member
This ^^ - good point! - When I'm referring to inherited objects I simply mean that I have a local variable for my instances, which I use for inheriting objects with no defined relationship other than that...

i.e. My mid-boss in my game spawns turrets and armaments upon enabling, andthe player can either chip away at the bosses health, or attack these, to remove chunks of life from it. In the event that the boss is defeated without destroying all of it's armaments, I then simply use the 'Boss_ID I set for them, to tell them to self-destruct - same principle...

There might be a better way of doing it using built-in inheritance etc, but this is a way simpler to maintain solution for my game's needs!... :)
 
I'm having trouble setting isMenu as a variable in the create event. I'm a noob and some of this stuff is a stretch for me. If I write isMenu it has to equal something. It can't just be declared as a label for the object. This is frustrating.
 
This doesn't seem to work.

if !variable_global_exists("isMenu")
{
variable_global_set("isMenu", 0);
}


___________________________________________
############################################################################################
ERROR in
action number 1
of Mouse Event for Mouse Leave
for object obj_spawn:

unable to convert string "isMenu" to bool
at gml_Object_obj_spawn_Mouse_11 (line 1) - if ("isMenu") {
############################################################################################
gml_Object_obj_spawn_Mouse_11 (line 1)
 

Nidoking

Member
Well, in the Mouse Leave event for object obj_spawn, the first line appears to be if ("isMenu"), which is wrong. You might have posted code from somewhere else.
 
Well I figured this one out from Googling my problem. Then Google led me back to this forum where someone else had this same problem before. Thanks to
TheouAegis for helping me solve another problem. Has to be like the third one he's helped me solve. And thanks to everyone else for your help!
 

Mk.2

Member
It seems that you don't understand how the step event and variables work, so I recommend reading the manual about them because they're pretty important to say the least. Glad you have this problem figured out though.
 
Top