Destroying a specific instance

Back for more help.

I'm trying to destroy a specific instance, here's the info;

There could be many instances of an object. I want to click on one of the objects to bring up a menu. If you click on a destroy button on that menu, you would destroy the instance of the object that was initially clicked to bring up the menu - but not the other instances of that object. I've watched a ton of videos and nothing addresses this specific problem. I'm guessing I have to find the instance id, then somehow pass that on (as a variable?) to the button click of the menu? I know I'm probably just missing something basic but I just can't find it. Thanks!
 
Ok, so you're clicking on the instance you want right? How are you figuring out that you are clicking on it? If you use an instance_* function, it'll return an instance ID. Returning basically means that you can set a variable to a function and the variable will end up holding whatever that function returns. So let's do an example:
Code:
var _inst = instance_position(mouse_x,mouse_y,object); // This'll check mouse_x and mouse_y for an instance of type "object" and return the instance ID of whatever is found...By setting _inst to the function, _inst will now hold whatever instance_position returns, either noone or an instance ID
if (instance_exists(_inst)) { // If there is no instance at mouse_x,mouse_y, the function returns noone, so we check to see if _inst holds a valid instance ID before proceeding
   if (mouse_check_button_released(mb_left)) {
      // Here we know that the mouse is over a valid instance and the left mouse button has been pressed and then released, so we do whatever we need to do when the instance is clicked
      instance_destroy(_inst); // You said you wanted that instance destroyed, so this is where we do it
   }
}
 
I think I understand. However, I'm right clicking on the instance I want to destroy, which brings up another object that is a menu. That object/menu has several buttons. If you click on a button, it destroys the menu, but also destroys the original instance that right clicked on. So, how do I pass on the id of the original object, through the menu object, and destroy just that object and not other instances of it? I'm just learning how to find, change, and destroy instances based on their id's.

Right mouse click on obj_fermenter_3bbl to create the menu object:
instance_create_layer(x + 75, y - 5, "Gui", obj_fermenter_menu);

Then, in that object, if you click on a certain area of the sprite, it should destroy the instance of obj_fermenter_3bbl that was originally right clicked, and the menu itself:
var objx = obj_fermenter_menu.x;
var objy = obj_fermenter_menu.y;

if mouse_check_button_pressed(mb_left) && mouse_x >= objx + 12/2 && mouse_x <= objx + 129/2 &&
mouse_y >= objy + 106/2 && mouse_y <= objy + 123/2 {

instance_destroy(obj_fermenter_3bbl);
instance_destroy(obj_fermenter_menu); }

else if mouse_check_button_pressed(mb_left) {

instance_destroy(obj_fermenter_menu);}

But as you can see, it's going to destroy all of the obj_fermenter_3bbl objects. How do I go back and basically choose an instance to destroy, from another object (not the object itself)?

Thanks for the help!! Beer brewing simulator by the way.
 

Roderick

Member
When you click the item to create the menu, you're doing something that will return the instance ID. After you create the menu, store that ID in a variable on the menu. When you click on a menu option that requires the ID, you'll be able to look it up in that variable.
 

NightFrost

Member
Code that runs in your object events, or is called from events, will always run in scope of the instances in the game room. The current instance id can always be accessed through the id keyword. When creating the menu object instance, you should send it the id of the instance that called for its creation. That way you can reference it in the menu and destroy it if necessary. You have to create a variable in the menu object to store the caller id, catch the menu instance's id from instance_create_layer call, and set the variable to caller's id.
 
Ok, so first of all. You need to completely stop using the object name in reference to instances. They are two completely different things and they have completely different results. The object is a blueprint and the instance is the house built out of that blueprint. Except in this case, destroying the blueprints destroys ALL the houses. I know that you're aware of this, but you've referenced the object name at every point possible in the above post.

I strongly urge you to read the manual entry for each function everytime you try to use it while you are new. There's many nuggets of information in the manual and it's easy to miss a piece on just one reading. It'll also help you build a mental map of what is possible and what you should be trying to use for each scenario.

So you right click on an instance of obj_fermenter_3bbl. This instance then spawns a new instance of obj_fermenter_menu. When you click an area after this, it should destroy both the instances right?

If you read the manual entry on instance_create_layer(), you'll see that it returns the instance ID of whatever instance has just been created. You can store this ID in a variable by setting the variable to equal the function as I explained in my previous post. This also allows you to immediately set some variables for the newly created instance, which is a useful thing to know and we're going to use that functionality here.

Right mouse click on obj_fermenter_3bbl:
Code:
var _menu = instance_create_layer(x + 75, y - 5, "Gui", obj_fermenter_menu); // Here we store the newly created instance's ID in the temp variable _menu
_menu.parent_inst = id;
/* The above line is doing a few things. We are accessing the newly created instance of obj_fermenter_menu by using _menu and then the dot accessor, this allows us to create a variable
in the _menu instance called parent_inst. We then store the id of the instance of obj_fermenter_3bbl that is being clicked, so now the new obj_fermenter_menu instance has a variable called
parent_inst that holds the instance ID of the obj_fermenter_3bbl instance that was clicked. */
Then, in that object, if you click on a certain area of the sprite, it should destroy the instance of obj_fermenter_3bbl that was originally right clicked, and the menu itself:
Code:
//var objx = obj_fermenter_menu.x; These lines are wrong, do not use object names
//var objy = obj_fermenter_menu.y;

/* I'm going to assume that this code is inside of obj_fermenter_menu because that's what it seems like to me. This means that it's completely unnecessary to store the x and y in new
variables because you already have the position stored...In x and y */

if mouse_check_button_pressed(mb_left) && mouse_x >= x+ 12/2 && mouse_x <= x+ 129/2 &&
   mouse_y >= y+ 106/2 && mouse_y <= y+ 123/2    {
    
       instance_destroy(parent_inst); // We destroy the instance of the obj_fermenter_3bbl that we stored in parent_inst
       instance_destroy(); } // And then we just destroy the current instance (which is whatever instance of obj_fermenter_menu that is running this code)

else if mouse_check_button_pressed(mb_left)     {

   instance_destroy();}
 
Last edited:
Back for more help.

I'm trying to destroy a specific instance, here's the info;

There could be many instances of an object. I want to click on one of the objects to bring up a menu. If you click on a destroy button on that menu, you would destroy the instance of the object that was initially clicked to bring up the menu - but not the other instances of that object. I've watched a ton of videos and nothing addresses this specific problem. I'm guessing I have to find the instance id, then somehow pass that on (as a variable?) to the button click of the menu? I know I'm probably just missing something basic but I just can't find it. Thanks!
When you click the object have that click get the Id of that instance e.g. upon clicking create var thisinstance_id=id (it will return the id of the clicked instance and store it as a var. I would use a globalvar thisinstance_id; and define it as an instance that doesn't matter as a default like a dummy instance to avoid "instance doesn't exist" or deleting a random instance that can be read as 0 or whatever. So then when you look at this var you can reset it to it's default worthless value if you delete the instance or decide to close the menu to essentially reset it.
 
Me again. If I destroy the instance, how do I destroy an associated mp_grid instance used for collision?

var _grid = mp_grid_add_instances(global.grid, obj_brewmagic, true);
_grid.parent_inst = ????
 
I don't use the inbuilt pathfinding grid stuff much, but if you read the manual, mp_grid_add_instances doesn't return anything:
Returns: N/A
So there's no point in assigning a variable to it's return value.

If you wanted to remove an "instance" from mp_grid (mp_grids don't actually store instances, they just mark a position on a grid as occupied or not) you would first convert that instances x and y coordinate to grid coordinates (if your mp_grid cells are 32x32, you would do var _grid_x = x div 32; and var _grid_y = y div 32;) and then clear the cell that corresponds to that position with mp_grid_clear_cell(global.grid,_grid_x,_grid_y);.
 
Top