• 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!

GameMaker How to do Conditional Destroy Functions

Hello, I have a game where enemy objects can be destroyed in 2 different ways, but I'd like 2 different things to happen depending on how the enemy object was destroyed.

For example,

if an enemy object goes off of screen or out of room the enemy object is destroyed and the enemy objects can be destroyed by the player.
What I'm wanting to do is, that when the enemy object is destroyed by the player, I have in the destroyed function to add 100 points to the score, but if the enemy object is destroyed by going out of the screen or out of the room or any other way that wasn't by the player, I don't want to give the player 100 points. I just want the enemy object to be destroyed.

How can I do this? Having a conditional destroy function?
 
D

DarthTenebris

Guest
How does your object get destroyed exactly? For example, if the object is destroyed when its HP goes to or below zero, then you simply check for that condition.
GML:
if (hp <= 0) {
     instance_destroy();
     with(obj_player) { score += 100; }
}
For the out of room case just use the appropriate event and use instance_destroy() there without adding the player's score afterwards.

Hope I helped!
 
Last edited by a moderator:

TheouAegis

Member
You KNOW when the enemy is getting killed by the player or when moving off-screen -- you're the one coding the game. Do you destroy the enemy in a collision event with a bullet? Then add points. Do you destroy the enemy when its hp is less than 1? Then add points. Do you destroy the enemy when it leaves the room? Then don't add points. This is elementary logic.
 
How does your object get destroyed exactly? For example, if the object is destroyed when its HP goes to or below zero, then you simply check for that condition.
Code:
if (hp <= 0) {
     instance_destroy();
     with(obj_player) { score += 100; }
}
For the out of room case just use the appropriate event and use instance_destroy() there without adding the player's score afterwards.

Hope I helped!
Thanks for your reply!

That's what I had in mind. I guess I was just over complicating things.

Like when the player destroys the object, it's by the hp going to 0.
When the enemy goes out of the room I use the instance_destroy();

But the things is, I was using the global.player_score += 100; in the Destroy function no matter how it was destroyed.
 
How does your object get destroyed exactly? For example, if the object is destroyed when its HP goes to or below zero, then you simply check for that condition.
GML:
if (hp <= 0) {
     instance_destroy();
     with(obj_player) { score += 100; }
}
For the out of room case just use the appropriate event and use instance_destroy() there without adding the player's score afterwards.

Hope I helped!
Here's another question I forgot to ask. If there were 2 or more players and the game mode was to see who can get the highest score, would I need to create different bullet objects for each player (o_bullet_P1, o_bullet_P2, etc) to track from the enemy object who destroyed it? or is there an easier way?
 

TheouAegis

Member
When you create the bullet, set a variable inside of it. The most common way is to store the shooter's id.
Code:
with instance_create(x,y,obj_bullet) owner=other.id;
Then when the bullet kills an enemy, give the points to the bullet's owner.
Code:
other.owner.points+=100;
 
Top