Delete certain instances of an object?

O

Omar Nevarez

Guest
I was wondering how I can delete certain instances of an object.
I created a wall object, and I wanted it to be deleted whenever my character opens up the menu, (scr_menu), and selects delete (or case 0 from that script).

I was wondering if it was possible to do this after placing the tiles in the room, and not by placing them and identifying them solely through code (as I think this can be a little time consuming).

If not, I would appreciate any other methods you guys would recommend.

Let you know if you guys need any more information.

Thanks!
 
D

dannyjenn

Guest
So you created several instances of the wall, and you want the player to destroy a particular instance through the menu? I'm a little confused... do you want it so that the player gets to choose which instance is destroyed (by e.g. clicking on it when the menu is open)? Or do you want the game to just destroy one at random or something?

Regardless, in order to do destroy an instance (or do basically anything with that instance), you need to somehow get that instance's id. I am not sure how you can get the id without code, though I have heard that you can somehow view the id in the room editor (I personally haven't figured out how). The id will be some number like 100000. (There are several functions which can get the id for you, but which one you should use would depend on what you're trying to do exactly.)

But once you have that number, you just need to call instance_destroy() on it.
 

Joe Ellis

Member
If you want all the walls to be deleted when you open the menu the simplest way would be to make "obj_wall", then just
with obj_wall {instance_destroy()}
but tiles arent instances, unless theres something I dont know, cus I'm still using gms1.4
 
O

Omar Nevarez

Guest
So you created several instances of the wall, and you want the player to destroy a particular instance through the menu? I'm a little confused... do you want it so that the player gets to choose which instance is destroyed (by e.g. clicking on it when the menu is open)? Or do you want the game to just destroy one at random or something?

Regardless, in order to do destroy an instance (or do basically anything with that instance), you need to somehow get that instance's id. I am not sure how you can get the id without code, though I have heard that you can somehow view the id in the room editor (I personally haven't figured out how). The id will be some number like 100000. (There are several functions which can get the id for you, but which one you should use would depend on what you're trying to do exactly.)

But once you have that number, you just need to call instance_destroy() on it.
Yeah. I want a wall to be deleted when I approach it and select delete from the menu. Just to test on how to do this to grab or open doors for later on, I guess.
I only want the closest wall to the character to be deleted. I can't figure this out though in a script. Not sure though.

I guess code is the fastest way, though.
What is the best way? Using a for() statement?
 
O

Omar Nevarez

Guest
If you want all the walls to be deleted when you open the menu the simplest way would be to make "obj_wall", then just
with obj_wall {instance_destroy()}
but tiles arent instances, unless theres something I dont know, cus I'm still using gms1.4
Just an instance of a wall. I surrounded my room with a border of walls, and I only want the closest wall to delete when I select it from the menu.
 

Joe Ellis

Member
Code:
var my_x = x;
var my_y = y;
var nearest = 10000000; (something thats further than everything)
var nearest_id = noone;

with obj_wall
{flag = false}

with obj_wall
{
if !flag && point_distance(my_x, my_y, x, y) < nearest
{
nearest = point_distance(my_x, my_y, x, y)
nearest_id = id
flag = true
}
}

if nearest_id != noone
{instance_destroy(nearest_id)}
This should get the nearest object and delete\destroy it
The "flag" isnt really necessary here but its generally good practice with loops, hmm i'd explain but its abit complicated, Flags are really good though with loops, especially repeating ones, they can cut out loads of work for the cpu
 
Last edited:
O

Omar Nevarez

Guest
Code:
var my_x = x;
var my_y = y;
var nearest = 10000000; (something thats further than everything)
var nearest_id = noone;

with obj_wall
{flag = false}

with obj_wall
{
if !flag && point_distance(my_x, my_y, x, y) < nearest
{
nearest = point_distance(my_x, my_y, x, y)
nearest_id = id
flag = true
}
}

if nearest_id != noone
{instance_destroy(nearest_id)}
This should get the nearest object and delete\destroy it
The "flag" isnt really necessary here but its generally good practice with loops, hmm i'd explain but its abit complicated, Flags are really good though with loops, especially repeating ones, they can cut out loads of work for the cpu
Thanks for the code example!
I tried it out though, and it gave me an error. I put that code in my case[0] in my scr_menu script.
The error says "Error in Script 'scr_menu' at Line 32, Position 41: Wrong number of arguments to function or script". That's the line with "instance_destroy(nearest_id)", i think.

Do you know what's wrong?
 

Joe Ellis

Member
Thanks for the code example!
I tried it out though, and it gave me an error. I put that code in my case[0] in my scr_menu script.
The error says "Error in Script 'scr_menu' at Line 32, Position 41: Wrong number of arguments to function or script". That's the line with "instance_destroy(nearest_id)", i think.

Do you know what's wrong?
oh sorry, yeah if you put the instance id, you have to put whether to execute the instance's destroy event or not, so I'd just put this as false, or 0


{instance_destroy(nearest_id, false)}
 
O

Omar Nevarez

Guest
oh sorry, yeah if you put the instance id, you have to put whether to execute the instance's destroy event or not, so I'd just put this as false, or 0


{instance_destroy(nearest_id, false)}
Hmmm... it's giving me the same error.
Sorry to be bothering you, but I can't seem to figure it out.
The instance_destroy command has been giving me issues lately. I'm sorta new to this coding, so I'm not sure.
 

Joe Ellis

Member
Ah sorry I dont know, it should work,? :S I feel bad, dunno what to do

maybe try:

Code:
if nearest_id != noone
{with nearest_id
{instance_destroy()}}
 
O

Omar Nevarez

Guest
Ah sorry I dont know, it should work,? :S I feel bad, dunno what to do

maybe try:

Code:
if nearest_id != noone
{with nearest_id
{instance_destroy()}}
Hey, I just found it out.
It was my fault. I forgot to replace 'obj_wall' with the actual name of my wall.
After that, it worked.
All I need to do is add an if statement to show text when the player is too far from the wall.

Thanks for all the help!
 

Joe Ellis

Member
Cool, I'm glad it worked : D
I used to do that for ages when i was learning gml, half the time its just cus you make some stupid mistake in the exact code
i've even done that recently, I was wondering what the hell was going on in loading a 3d model and it turned out I'd forgot to set an exact point in a certain array,
but hopefully you'll never have to deal with stuff that complex, If your making a 2d game, gm is the man ; )
 
Last edited:

YellowAfterlife

ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ
Forum Staff
Moderator
The error says "Error in Script 'scr_menu' at Line 32, Position 41: Wrong number of arguments to function or script". That's the line with "instance_destroy(nearest_id)", i think.

Do you know what's wrong?
You might want to update GMS because this error means that your copy of GMS1 is at least two years out of date (instance_destroy arguments were introduced after late 2016), which will without a doubt result in other mysterious issues (that no one else will be able to replicate) and frustration further down the road.
 
Top