How would I destory an object that spawns first even though there is multiple copies of that object?

C

Cmi

Guest
So I have an object and it changes spirits. When The object spawns and say spirit is on 1 and another spawns and is on spirit 2 how do I make it so that the object that spawned first get destroyed by a key press that I assign it. Here is my code am not sure why it isn't working.

Create Event
Code:
//Randomize
randomize();
// Set total to equal the random number
total = irandom(2);
// Set image index equal total
if (total == 0){

total = 1;

}
image_index = total;
// set the image speed equal 0
image_speed = 0;
// Everytime an object is created add 1 to the global.left
global.left+=1;
Step

Code:
var a = 0;
var b = 0;
var c = 0;

if (total == 1){
a = 1;
}

if (a == 1){
// Declare a variable called obj
var obj;
// Check if D has been pressed
if keyboard_check_pressed(ord('A')){
    //get the first instance of the given object
    obj = instance_find(obj_try, 0);
    // Check if the object exists
    if(obj != noone)
    {
        // Destory all instances of obj = instance_find(obj_d, 0)
        with(obj)
        {
            // Add 1 to the score
            global.score_1++;
            // Destory the object
            instance_destroy();
        }
    }
}
}

if (total == 2){
b = 1;
}

if (b == 1){
// Declare a variable called obj
var obj;
// Check if D has been pressed
if keyboard_check_pressed(ord('B')){
    //get the first instance of the given object
    obj = instance_find(obj_try, 0);
    // Check if the object exists
    if(obj != noone)
    {
        // Destory all instances of obj = instance_find(obj_d, 0)
        with(obj)
        {
            // Add 1 to the score
            global.score_1++;
            // Destory the object
            instance_destroy();
        }
    }
}
}
 

NicoFIDI

Member
if you want to make the last object be the only standing then i whould do something like

in obj_try create
Code:
/// Initilize
last = true;
when you create a obj try make:
Code:
with (obj_try) {
    last = false;
}
instance_create(xx,yy,obj_try)
when you press the key
Code:
with (obj_try) {
    if (!last) {
        global.score_1++;
        instance_destroy();
    }
}
Edit: if you want to make them dissapear in the appear order 1 by 1 then i whould use a queue
 
C

Cmi

Guest
if you want to make the last object be the only standing then i whould do something like

in obj_try create
Code:
/// Initilize
last = true;
when you create a obj try make:
Code:
with (obj_try) {
    last = false;
}
instance_create(xx,yy,obj_try)
when you press the key
Code:
with (obj_try) {
    if (!last) {
        global.score_1++;
        instance_destroy();
    }
}
Edit: if you want to make them dissapear in the appear order 1 by 1 then i whould use a queue
Yeah I want them to disappear based on the order they were create. So example if a spawns, b spawns and c spawns, I should be able to destroy a first and then destroy b but they are the some objects. I want to destroy them using the keyboard_check_pressed(ord('B')).
 

NicoFIDI

Member
obj_queue_try
on create
Code:
/// Initialize
queue = ds_queue_create();
on destroy and on game end
Code:
/// free queue memory
if (ds_queue_exists(queue))
    ds_queue_destroy(queue);
on press B
Code:
/// dequeue
if (!ds_queue_empty(queue)) {
    with (ds_queue_dequeue(queue)) {
        instance_destroy();
    }
}
on obj_try create
Code:
/// Initialize
ds_queue_enqueue(obj_queue_try.queue,id);
 
C

Cmi

Guest
obj_queue_try
on create
Code:
/// Initialize
queue = ds_queue_create();
on destroy and on game end
Code:
/// free queue memory
if (ds_queue_exists(queue))
    ds_queue_destroy(queue);
on press B
Code:
/// dequeue
if (!ds_queue_empty(queue)) {
    with (ds_queue_dequeue(queue)) {
        instance_destroy();
    }
}
on obj_try create
Code:
/// Initialize
ds_queue_enqueue(obj_queue_try.queue,id);
What do I do with obj_queue_try do I have it in the room? So that it keeps track?
 
C

Cmi

Guest
ds_exists(queue) :D
I tried everything you said but my screen just becomes black and I can't even see my objects. For ds_exist I did ds_exists(id,queue) as it takes 2 arguments but nothing is working. I am not sure why there isn't a simple way of doing it as in English it sounds so simple but coding it, it just gives bugs :/
 

NicoFIDI

Member
I'm sorry It didnt work,
i'll Code It myself when i have the time (at night time),
if you are not helped earlier.
 
Last edited:

NicoFIDI

Member
i read the ds_exists guide, sorry to give you the wrong answer,
if (ds_exists(queue, ds_type_queue))

but i never used queues on game maker.
so i will still give it a try tonight.
when i do i'll give you the exact code i use :)
 
C

Cmi

Guest
i read the ds_exists guide, sorry to give you the wrong answer,
if (ds_exists(queue, ds_type_queue))

but i never used queues on game maker.
so i will still give it a try tonight.
when i do i'll give you the exact code i use :)
Thanks, I tried this earlier today but it has some bugs in it thou.

Create event
Code:
//Randomize
randomize();
// Set total to equal the random number
total = irandom(2);
// Set image index equal total
if (total == 0){

letter = 'A';

} else if (total == 1){

letter = 'A';

} else if (total == 2){

letter = 'B';

}
image_index = total;
// set the image speed equal 0
image_speed = 0;
// Everytime an object is created add 1 to the global.left
global.left+=1;
Step event
Code:
// Declare a variable called obj
var obj;
// Check if D has been pressed
if keyboard_check_pressed(ord(letter)){
    //get the first instance of the given object
    obj = instance_find(obj_1try, 0);
    // Check if the object exists
    if(obj != noone)
    {
        // Destory all instances of obj = instance_find(obj_d, 0)
        with(obj)
        {
            // Add 1 to the score
            global.score_1++;
            // Destory the object
            instance_destroy();
        }
    }
}
 

NicoFIDI

Member
Information about object: obj_try_control
Code:
Sprite:
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent:
Children:
Mask:

No Physics Object
Create Event:

execute code:

/// Initialize
queue = ds_queue_create();


Destroy Event:

execute code:

/// Release memory
if (ds_exists(queue,ds_type_queue))
    ds_queue_destroy(queue);

Mouse Event for Glob Left Pressed:

execute code:

/// Create try
instance_create(mouse_x,mouse_y,obj_try);

Other Event: Game End:

execute code:

/// Release memory
if (ds_exists(queue,ds_type_queue))
    ds_queue_destroy(queue);

Key Press Event for <Enter> Key:

destroy the instance
Key Press Event for B-key Key:

execute code:

/// Delete first object
if (!ds_queue_empty(queue)) {
    with(ds_queue_dequeue(queue)) {
        instance_destroy();
    }
}
Information about object: obj_try
Code:
Sprite: spr_nothing
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent: 
Children: 
Mask: 
No Physics Object
Create Event:
execute code:

/// Initialize
ds_queue_enqueue(obj_try_control.queue, id);
i made this and it work out
 
C

Cmi

Guest
Information about object: obj_try_control
Code:
Sprite:
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent:
Children:
Mask:

No Physics Object
Create Event:

execute code:

/// Initialize
queue = ds_queue_create();


Destroy Event:

execute code:

/// Release memory
if (ds_exists(queue,ds_type_queue))
    ds_queue_destroy(queue);

Mouse Event for Glob Left Pressed:

execute code:

/// Create try
instance_create(mouse_x,mouse_y,obj_try);

Other Event: Game End:

execute code:

/// Release memory
if (ds_exists(queue,ds_type_queue))
    ds_queue_destroy(queue);

Key Press Event for <Enter> Key:

destroy the instance
Key Press Event for B-key Key:

execute code:

/// Delete first object
if (!ds_queue_empty(queue)) {
    with(ds_queue_dequeue(queue)) {
        instance_destroy();
    }
}
Information about object: obj_try
Code:
Sprite: spr_nothing
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent:
Children:
Mask:
No Physics Object
Create Event:
execute code:

/// Initialize
ds_queue_enqueue(obj_try_control.queue, id);
i made this and it work out
How would I destroy the object if let's say obj_try has image index 1 and I want to destroy the object with a press A only if the image index is 1 and if the image index is 2 I want to destroy it using a press B? How would I do that? Also does objectName.variable change the variable for all other instances of that object.
 

NicoFIDI

Member
when you press A or B (check the wanted image index)
Code:
/// Delete first object
var detroyable_image_index = 1;
if (!ds_queue_empty(queue)) {
   var delete = false;
   with(ds_queue_head(queue)) {
       delete = (image_index == detroyable_image_index);
       if (delete)
           instance_destroy();
   }
   if (delete) 
       ds_queue_dequeue(queue);
}
 
  • Like
Reactions: Cmi

TheouAegis

Member
with instance_find(obj_try, 0) { global.score_1++; instance_destroy(); }

That is literally all that is needed to destroy the last instance of an object that was created, at least in Studio 1.4. Instance lists are automatically rearranged each time an instance is destroyed.

If that line does not work for you, there are a couple things that could be at fault.

First, you could be spawning multiple obj_try in the same location, so you don't see the first one disappear because it's overlapped by another.

Second, the conditions which dictate when to destroy an instance could not be correct, so nothing within the conditional will run as a result; so the instance_find() doesn't get run and the instance_destroy() doesn't get run because the conditions were never met.
 
Last edited:
C

Cmi

Guest
with instance_find(obj_try, 0) { global.score_1++; instance_destroy(); }

That is literally all that is needed to destroy the last instance of an object that was created, at least in Studio 1.4. Instance lists are automatically rearranged each time an instance is destroyed.

If that line does not work for you, there are a couple things that could be at fault.

First, you could be spawning multiple obj_try in the same location, so you don't see the first one disappear because it's overlapped by another.

Second, the conditions which dictate when to destroy an instance could not be correct, so nothing within the conditional will run as a result; so the instance_find() doesn't get run and the instance_destroy() doesn't get run because the conditions were never met.
That is what am doing, am trying to spawn the some obj_try on another obj_try. Could that be the case of why my conditions aren't being met?
 
C

Cmi

Guest
when you press A or B (check the wanted image index)
Code:
/// Delete first object
var detroyable_image_index = 1;
if (!ds_queue_empty(queue)) {
   var delete = false;
   with(ds_queue_head(queue)) {
       delete = (image_index == detroyable_image_index);
       if (delete)
           instance_destroy();
   }
   if (delete)
       ds_queue_dequeue(queue);
}
I will give it a try, also are you using Queues because they always remove the first object that is added to the queue? So first one added is the first one remove? I just realized that xD. I never knew GM had queues.
 
I don't think you can traverse a queue without removing items from it (inconvenient), so you really couldn't go looking through them for the first instance that has some property (without destroying the data in the queue). You could use instance find inside of a loop, but the last time I checked that, I found it to be unusually slow in comparison to making my own list.
 
S

Silicon Sorcery Studios

Guest
Simple

Use a variable to save the id of the original, then delete all instances of that object that don't match that id.

I would recommend using an array, but you might not need one.
 
C

Cmi

Guest
when you press A or B (check the wanted image index)
Code:
/// Delete first object
var detroyable_image_index = 1;
if (!ds_queue_empty(queue)) {
   var delete = false;
   with(ds_queue_head(queue)) {
       delete = (image_index == detroyable_image_index);
       if (delete)
           instance_destroy();
   }
   if (delete)
       ds_queue_dequeue(queue);
}
Thanks for the help, it seems as everything is working well. Just one question where would I increment the score? Would it be like this
Code:
/// Delete first object
var detroyable_image_index = 1;
if (!ds_queue_empty(queue)) {
   var delete = false;
   with(ds_queue_head(queue)) {
       delete = (image_index == detroyable_image_index);
       if (delete)
           instance_destroy();
          
   }
   if (delete)
        // Add 1 to the score
        global.score_2++;
       ds_queue_dequeue(queue);
      
}
 

NicoFIDI

Member
queue are FIFO structures (first in first out)
stacks are LIFO structures (Last in first out)
if you need any of those, game maker have them
and other cool structures more :D

yes, just use {} on the if
Code:
/// Delete first object
var detroyable_image_index = 1;
if (!ds_queue_empty(queue)) {
   var delete = false;
   with(ds_queue_head(queue)) {
       delete = (image_index == detroyable_image_index);
       if (delete)
           instance_destroy();
        
   }
   if (delete) {
       // Add 1 to the score
       global.score_2++;
       ds_queue_dequeue(queue);
   }
}
 
C

Cmi

Guest
queue are FIFO structures (first in first out)
stacks are LIFO structures (Last in first out)
if you need any of those, game maker have them
and other cool structures more :D

yes, just use {} on the if
Code:
/// Delete first object
var detroyable_image_index = 1;
if (!ds_queue_empty(queue)) {
   var delete = false;
   with(ds_queue_head(queue)) {
       delete = (image_index == detroyable_image_index);
       if (delete)
           instance_destroy();
       
   }
   if (delete) {
       // Add 1 to the score
       global.score_2++;
       ds_queue_dequeue(queue);
   }
}
Yeah, I am new to stacks and queues, by the way what year are you? It says your an engineering student.
 

NicoFIDI

Member
i'm on 3rd year of "informatic engineering" in the "Universidad de Buenos Aires".
:p the 3rd year it's from 6 years of career.
but actually theres not an strict program,
you need obligarty assignments and enought credits to have the tittle.
 

TheouAegis

Member
Thanks for the help, it seems as everything is working well. Just one question where would I increment the score? Would it be like this
Code:
/// Delete first object
var detroyable_image_index = 1;
if (!ds_queue_empty(queue)) {
   var delete = false;
   with(ds_queue_head(queue)) {
       delete = (image_index == detroyable_image_index);
       if (delete)
           instance_destroy();
         
   }
   if (delete)
        // Add 1 to the score
        global.score_2++;
       ds_queue_dequeue(queue);
     
}

Did you completely rewrite your Step Event? In your original code you had a bunch of if total==1 stuff, then in that code it looks like you're just straight going through the queue. What's your complete, new, working Step Event code?
 
C

Cmi

Guest
i'm on 3rd year of "informatic engineering" in the "Universidad de Buenos Aires".
:p the 3rd year it's from 6 years of career.
but actually theres not an strict program,
you need obligarty assignments and enought credits to have the tittle.
Oh how are you liking it so far? I am in Computer Engineer Technology not sure if it's the same, I am going into my 2nd year :)
 
C

Cmi

Guest
Did you completely rewrite your Step Event? In your original code you had a bunch of if total==1 stuff, then in that code it looks like you're just straight going through the queue. What's your complete, new, working Step Event code?
I have nothing in my step event, I deleted that part. The /// Delete first object code is in my press A event. On the controller that creates the queue.
 

TheouAegis

Member
Well then that was most likely the culprit -- the rest of your Step Event.

Oh well, at least you have something working.
 
  • Like
Reactions: Cmi

Anuj Raghav

Member
also try not to destroy and create lot of objects ..pool them . create maximum required+(2 or 3) instances at start that might be visible at one time on screen and deactivate them , save the instance ids of those and when you need to spawn ,activate it and store it in queue ...this way there wont be memory spikes when creating or destroying objects
 
Top