Is there any way to store the ids of all instances created by an object?

N

Noba

Guest
So I have this code, which creates some explosion objects after an enemy dies.

Code:
///@desc create the bullet objects

if(explosiontype = 1){ //if the type of explosion is the first one...
   
   
    if(explosioncolour = 1){//if the colour is 1 (white)
       
        if(amountcreated < repeatamount){//if the amount of explosion fx created is less than the repeat amount
           
            repeat(repeatamount){//create another instance and add 1 amountcreated till amountcreated is greater than repeatamount
           
                myexplosion = instance_create_layer(x,y,"Instances",oWhite);
                myexplosion.direction = random_range(0,360);
                myexplosion.image_angle = myexplosion.direction + 90;
                amountcreated += 1;
           
            }
       
        }
       
    }
   
   
    if(alarm[0] == -1){
       
        alarm[0] = 70; //use this to destroy all created instances then destroy the base object
    }
   
}
I'm aware I can use particles, but I find them a bit awkward to set up, so.


Anyway, this code works fine, but when the alarm in the main explosion object comes to destroy all the instances created, it only destroys one. I'm assuming that's because of the myexplosion variable, but I'm not sure what to do in it's place. I was wondering if there was a way to store all the ids of instances created by this object, and then using that to destroy them. Is this even possible?
 
D

dannyjenn

Guest
You could keep track of all the oWhite ids (in an array or ds_list), though there might be an easier way.

I take it that each explosion is being generated by a separate explosion controller instance? If that's the case then you could give oWhite a variable called explosion_number (set to -1 in create event) and then modify your code as follows:
Code:
///@desc create the bullet objects

if(explosiontype = 1){ //if the type of explosion is the first one...
 
 
   if(explosioncolour = 1){//if the colour is 1 (white)
   
       if(amountcreated < repeatamount){//if the amount of explosion fx created is less than the repeat amount
       
           repeat(repeatamount){//create another instance and add 1 amountcreated till amountcreated is greater than repeatamount
       
               myexplosion = instance_create_layer(x,y,"Instances",oWhite);
               myexplosion.direction = random_range(0,360);
               myexplosion.image_angle = myexplosion.direction + 90;
               myexplosion.explosion_number = id; // <-- add this line
               amountcreated += 1;
       
           }

       }
   
   }
 
 
   if(alarm[0] == -1){
   
       alarm[0] = 70; //use this to destroy all created instances then destroy the base object
   }
 
}
Code:
// alarm 0 event:
with(oWhite){
    if(explosion_number==other.id){ // if the explosion particle's explosion_number matches the explosion controller instance's id
        instance_destroy(id); // destroy the explosion particle
    }
}
instance_destroy(id); // destroy the explosion controller instance
 
N

Noba

Guest
You could keep track of all the oWhite ids (in an array or ds_list), though there might be an easier way.

I take it that each explosion is being generated by a separate explosion controller instance? If that's the case then you could give oWhite a variable called explosion_number (set to -1 in create event) and then modify your code as follows:
Code:
///@desc create the bullet objects

if(explosiontype = 1){ //if the type of explosion is the first one...
 
 
   if(explosioncolour = 1){//if the colour is 1 (white)
  
       if(amountcreated < repeatamount){//if the amount of explosion fx created is less than the repeat amount
      
           repeat(repeatamount){//create another instance and add 1 amountcreated till amountcreated is greater than repeatamount
      
               myexplosion = instance_create_layer(x,y,"Instances",oWhite);
               myexplosion.direction = random_range(0,360);
               myexplosion.image_angle = myexplosion.direction + 90;
               myexplosion.explosion_number = id; // <-- add this line
               amountcreated += 1;
      
           }

       }
  
   }
 
   if(alarm[0] == -1){
  
       alarm[0] = 70; //use this to destroy all created instances then destroy the base object
   }
 
}
Code:
// alarm 0 event:
with(oWhite){
    if(explosion_number==other.id){ // if the explosion particle's explosion_number matches the explosion controller instance's id
        instance_destroy(id); // destroy the explosion particle
    }
}
instance_destroy(id); // destroy the explosion controller instance
Alright, thanks! I'll try out the first one. What would I replace the alarm code with, by the way? Currently it's just

Code:
instance_destroy();
instance_destroy(myexplosion);
Which as I've said doesn't work.
 
D

dannyjenn

Guest
Try replacing it with
Code:
// alarm 0 event:
with(oWhite){
   if(explosion_number==other.id){ // if the explosion particle's explosion_number matches the explosion controller instance's id
       instance_destroy(id); // destroy the explosion particle
   }
}
instance_destroy(id); // destroy the explosion controller instance
 
N

Noba

Guest
Try replacing it with
Code:
// alarm 0 event:
with(oWhite){
   if(explosion_number==other.id){ // if the explosion particle's explosion_number matches the explosion controller instance's id
       instance_destroy(id); // destroy the explosion particle
   }
}
instance_destroy(id); // destroy the explosion controller instance
Ah sorry! I completely missed that first comment and saw it as a separate piece of code. Thanks!
 
N

Noba

Guest
Try replacing it with
Code:
// alarm 0 event:
with(oWhite){
   if(explosion_number==other.id){ // if the explosion particle's explosion_number matches the explosion controller instance's id
       instance_destroy(id); // destroy the explosion particle
   }
}
instance_destroy(id); // destroy the explosion controller instance
I just tested it and it worked perfectly! Thank you very much! :)
 
Top