• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Destroying other instances from a "main object"?

C

ColeusSanctus

Guest
Hey guys,
i have a problem with destroying a specific instance.
I create an object, called object_enemy_2 and this object creates other objects (light, gun and engine).
The enemy has 10 health and when it has a collision with the bullet from the player, health becomes -1 (relativ).
When it drops to 0 object_enemy_2 gets destroyed but then this error appears.
enemy_2_error_1.PNG

I understand the problem:
The light, gun and engine have step-events where they jump to a point to stay at the enemy_2.
But when enemy_2 doesnt exist anymore they cannot jump, so the error appears.
So how can prevent this and can destroy the three other object if the "main object" gets killed?Enemy_2.PNG

I hope somebody can give me a hint :)

Greetings
Simon

PS: Sorry for my english :D
 
W

Wayfarer

Guest
In the "Destroy" event of the "main object" use something like:

Code:
with (destroyThisInstance1) instance_destroy(); // instance #1
with (destroyThisInstance2) instance_destroy(); // instance #2
with (destroyThisInstance3) instance_destroy(); // instance #3
Replace "destroyThisInstance1", "destroyThisInstance2" and "destroyThisInstance3" with the three object names :)

The "Destroy" event runs when an instance is destroyed.

Edit:
Oh, I see you're using Drag and Drop. In that case, still go to the "Destroy" event, and you should be able to add some kind of "Destroy Instance" icon/thing (not sure how it works in GMS2). Add one of these for each instance.
 
Last edited:
C

ColeusSanctus

Guest
Hey,
thanks for your help!
I just added "execude code" and wrote your lines and it worked !
:)
 
C

ColeusSanctus

Guest
Well, now i have another problem....
If i have two "mother objects" and one gets destroyed, the other three object of the second "mother object" also gets destroyed...
How can i specificate this?
 

sp202

Member
When creating the subsidiary objects you need to store the id they return and use that id in instance_destroy().
 

csanyk

Member
Somewhere (probably in Create Event) for enemy_2, you need to store the id of the light, gun, and engine instances that belong to the enemy_2. Then, in the enemy_2 Destroy Event, you can destroy the light, gun, and engine instances by means of a with (id) instance_destroy() (GMS1.x) or instance_destroy(id) (GMS2.x).

Alternately, if you don't want to destroy those instances (for example, if the enemy should drop his gun as a pickup for the player) you could always have instance_exists() checks to see if the enemy_2 instance that was holding the gun still exists.
 
C

ColeusSanctus

Guest
Hey,
so i got your idea but i am to stupid for programming it ^^.
I always get the error, that the id variable isnt set befor reading it :/
Any idea? Maybe also for D&D?
Would be great :D
Sry for the stupid questions, but i am new to this ;)
 

csanyk

Member
Hey,
so i got your idea but i am to stupid for programming it ^^.
I always get the error, that the id variable isnt set befor reading it :/
Any idea? Maybe also for D&D?
Would be great :D
Sry for the stupid questions, but i am new to this ;)
I haven't used DnD in such a long time, I'm not sure how easily it can be done with DnD commands. But the GML would look something like:

Code:
///Enemy_2 Create Event:

//Create instances of the gun, light, and engine for the enemy_2

gun = instance_create(x, y, obj_gun);
light = instance_create(x, y, obj_light);
engine = instance_create(x, y, obj_engine);
Code:
///Enemy_2 Step Event:

//Keep the gun, light, and engine in place of the enemy_2

gun.x = x; gun.y = y;
light.x = x; light.y = y;
engine.x = x; engine.y = y;
Code:
///Enemy_2 Destroy Event:

//Clean up the gun, light, and engine of the enemy_2

with (gun) instance_destroy();
with (light) instance_destroy();
with (engine) instance_destroy();
 
C

ColeusSanctus

Guest
I haven't used DnD in such a long time, I'm not sure how easily it can be done with DnD commands. But the GML would look something like:

Code:
///Enemy_2 Create Event:

//Create instances of the gun, light, and engine for the enemy_2

gun = instance_create(x, y, obj_gun);
light = instance_create(x, y, obj_light);
engine = instance_create(x, y, obj_engine);
Code:
///Enemy_2 Step Event:

//Keep the gun, light, and engine in place of the enemy_2

gun.x = x; gun.y = y;
light.x = x; light.y = y;
engine.x = x; engine.y = y;
Code:
///Enemy_2 Destroy Event:

//Clean up the gun, light, and engine of the enemy_2

with (gun) instance_destroy();
with (light) instance_destroy();
with (engine) instance_destroy();
Hi,
thank you for your fast help!
Everything is working now :) and maybe i should used to get into the GML ^^
 
Top