Legacy GM Destory only one instance of many

C

Cmi

Guest
So I am making this little game, where you there is 5 objects of the same and when you press a letter it destroys it. My problem is when I like the letter that is pressed it destroys every other object. I have tries the

Code:
with (obj_Enemy)
   {
    instance_destroy();
   }
But that isn't work nor is the destroy self function working. Can anyone help me?
 
C

Cmi

Guest
I just tried this code and it doesn't work either
Code:
if keyboard_check_pressed(ord('A'))
   {
   global.score_1 +=1;
   instance_destroy();
   }
 
You are not referring to a specific object, instead you are referring to the name of the object, which is shared by all copies of that object.
You need to have some sort of way to refer to a specific reference of an object. There are many ways to do this, but it all depends on what sort of project you are creating and how you want it to be structured.
 
C

Cmi

Guest
You are not referring to a specific object, instead you are referring to the name of the object, which is shared by all copies of that object.
You need to have some sort of way to refer to a specific reference of an object. There are many ways to do this, but it all depends on what sort of project you are creating and how you want it to be structured.
I just want when you press keyboard A it destroys one object so users can't just click A and it destroys all. What other ways is there if you have a link can u link me?
 
I just want when you press keyboard A it destroys one object so users can't just click A and it destroys all. What other ways is there if you have a link can u link me?
If you want to destroy one object regardless of its importance in the room, you can do something like:

Code:
if(instance_number(obj_enemy) > 0){
  with(instance_find(obj_enemy, 0)){
    instance_destroy();
  }
}
instance_number is a function that returns the total amount of active instances of an object in the room : https://docs.yoyogames.com/source/dadiospice/002_reference/objects and instances/instances/instance functions/instance_number.html
instance_find will return one of those objects, depending on the index you provide : https://docs.yoyogames.com/source/dadiospice/002_reference/objects and instances/instances/instance functions/instance_find.html

So what we are saying is, if there are more than zero instances of that object in the room, using the first active object of that type, destroy it.

Here's a different version with variable names to make it more clear what's going on:
Code:
var _totalNumberOfEnemiesInRoom = instance_number(obj_enemy);
if(_totalNumberOfEnemiesInRoom > 0){
  var _firstActiveEnemyInRoom = instance_find(obj_enemy, 0);
  with(_firstActiveEnemyInRoom){
    instance_destroy();
  }
}
 

Kululu17

Member
Its hard to give more specific advice without seeing more code, but you need to first select the instance (not object) that you want to destroy. Trying looking up instance commands in the manual to see which one fits your game best, for example if you have some kind of pointer object, you could use "instance_nearest" to select the one you want. But it depends on the rest of the code...
 
C

Cmi

Guest
If you want to destroy one object regardless of its importance in the room, you can do something like:

Code:
if(instance_number(obj_enemy) > 0){
  with(instance_find(obj_enemy, 0)){
    instance_destroy();
  }
}
instance_number is a function that returns the total amount of active instances of an object in the room : https://docs.yoyogames.com/source/dadiospice/002_reference/objects and instances/instances/instance functions/instance_number.html
instance_find will return one of those objects, depending on the index you provide : https://docs.yoyogames.com/source/dadiospice/002_reference/objects and instances/instances/instance functions/instance_find.html

So what we are saying is, if there are more than zero instances of that object in the room, using the first active object of that type, destroy it.

Here's a different version with variable names to make it more clear what's going on:
Code:
var _totalNumberOfEnemiesInRoom = instance_number(obj_enemy);
if(_totalNumberOfEnemiesInRoom > 0){
  var _firstActiveEnemyInRoom = instance_find(obj_enemy, 0);
  with(_firstActiveEnemyInRoom){
    instance_destroy();
  }
}
The code kinda worked but not how I described it, do I have to place both codes in my key pressed?
 

Yal

šŸ§ *penguin noises*
GMC Elder
Or you could abort the loop in the first iteration...
Code:
with(obj_enemy){
  instance_destroy()
  break
}
 

TheouAegis

Member
if keyboard_check_pressed(ord(myLetter)) {
global.score_1++;
io_clear();
instance_destroy();
}


The io_clear() will prevent other keyboard_check_pressed() calls from detecting it.This code could go inside the lettered objects themselves (myLetter would be the letter the player needs to press to destroy it). However, this will destroy a seemingly random object. If you want a specific object, you either need to fetch the ID of it or set up your game so the object that should be destroyed first gets handled first.


Method 1) Handle the desired object first by altering its depth

By default, I think the first instance created (the oldest) is the processed first. You can test this yourself with the code I posted at the start. However, that's not always the case (I did some tests one time and the results took the newest instance). An alternate means of making sure the desired object is handled first is to have your objects update their depth and then put the code I shared in the Draw event instead of the Step event.

If the blocks are falling from the top of the screen, then you can set in the step event
Code:
depth = y;
If they are coming from off the right side of the screen, you can set in the step event
Code:
depth = x;
Note that this is a little different than normal depth-setting code you'll see floating around, which is -y. In that code, you want the objects toward the bottom of the screen to draw last so they are on top. With the method I described, though, the objects toward the bottom will draw first, so they are below overlapping objects. So in some cases, this might not be a desirable trade-off (shouldn't be a problem for left-right scrolling, though).

Another method of altering the depth is to have a global variable counting down every time an instance is created and set depth based on that.
Code:
depth = global.depth_change--;
So the first instance would be created at depth 0, then the next would be created at -1, then -2, and so on.
 
C

Cmi

Guest
if keyboard_check_pressed(ord(myLetter)) {
global.score_1++;
io_clear();
instance_destroy();
}


The io_clear() will prevent other keyboard_check_pressed() calls from detecting it.This code could go inside the lettered objects themselves (myLetter would be the letter the player needs to press to destroy it). However, this will destroy a seemingly random object. If you want a specific object, you either need to fetch the ID of it or set up your game so the object that should be destroyed first gets handled first.


Method 1) Handle the desired object first by altering its depth

By default, I think the first instance created (the oldest) is the processed first. You can test this yourself with the code I posted at the start. However, that's not always the case (I did some tests one time and the results took the newest instance). An alternate means of making sure the desired object is handled first is to have your objects update their depth and then put the code I shared in the Draw event instead of the Step event.

If the blocks are falling from the top of the screen, then you can set in the step event
Code:
depth = y;
If they are coming from off the right side of the screen, you can set in the step event
Code:
depth = x;
Note that this is a little different than normal depth-setting code you'll see floating around, which is -y. In that code, you want the objects toward the bottom of the screen to draw last so they are on top. With the method I described, though, the objects toward the bottom will draw first, so they are below overlapping objects. So in some cases, this might not be a desirable trade-off (shouldn't be a problem for left-right scrolling, though).

Another method of altering the depth is to have a global variable counting down every time an instance is created and set depth based on that.
Code:
depth = global.depth_change--;
So the first instance would be created at depth 0, then the next would be created at -1, then -2, and so on.
:) it worked the code you gave me
Code:
if keyboard_check_pressed(ord(myLetter)) {
global.score_1++;
io_clear();
instance_destroy();
}
Worked I had to change myletter to the keyboard letter I was using. Thank you so much
 
Top