How to destroy bullet after it hits enemies?

J

jamie_simpson

Guest
If this code works when using instance_id like that, that's a coincidence at best and wrong at worst.

instance_id is (to quote the manual) a read only array (which) holds all the ids of every active instance within the room.

This could work if you're using a version of GameMaker that refers to the first index of an array when used in variable context and the instance in question happens to be the first one. In all other cases, this will either destroy the wrong instance or crash.


id refers to the instance that is under the current execution scope.
self used to be equivalent to the value -1, which was interpreted as equivalent to id in some functions. Now, it is equivalent to id.
An object's name does not refer to the calling instance, but either to the first instance of an object in a reading context, or to all instances of an object in a writing context. If an object ID is interpreted as the instance you meant to refer to, it is either because that instance is the only instance of that object, or a sheer coincidence.


Use id. Or, better yet, just call instance_destroy() without any arguments - then it will automatically apply to itself.
I've noticed that now ._.
i thought that it worked for me because when a bullet collided with an enemy, not all of the bullets got destroyed (like they did before)
what i didn't notice is that the bullet that hit the enemy didn't get destroyed either and i probably passed a blind eye on that because some of my gun types have bullet penetration.
and also, I've tried all these three as arguments for instance_destroy();
- (self);
- (bulletO);
-(id);
-();
but none of them worked hence why i got so exited when i tried instance_id;
here is my current code for dealing with the bullet and destroying it
bulletO --> step -->
Code:
 if (x < 0) or (x > room_width) or (y < 0) or (y > room_height)
 {
 instance_destroy(id);
 }
 if (penetration = 0) or (penetration < 0)
{
 instance_destroy(id);
}
penetration is subtracted by 1 if it collides with an enemy (collision event in enemy)
any suggestions as to why it doesn't work?
 

TsukaYuriko

☄️
Forum Staff
Moderator
(Split from another topic to not derail)

Don't pass anything to instance_destroy and it will apply to the calling instance. If this appears not to work, you instead have a problem with the condition for this code to execute.

Verify that the value of penetration is actually what you expect it to be.
 

Joe Ellis

Member
I think in the part where penetration is subtracted by 1, instead of doing that, just destroy the bullet in the enemy's collision event: instance_destroy(other.id),
If that doesn't work, I would put the collision event in the bullet object. This is more efficient anyway, cus bullets are temporary instances, ideally you don't want every enemy instance checking for collisions with every type of projectile. If the collision checks are in the projectiles' step event, it's more efficient cus of how >50% of the time, no projectiles exist. I always have characters doing their ai states and physics\collision, while other objects check for collision with the characters. It makes the character's code alot cleaner and makes the projectiles' code more to the point about what happens with them
 
J

jamie_simpson

Guest
I think in the part where penetration is subtracted by 1, instead of doing that, just destroy the bullet in the enemy's collision event: instance_destroy(other.id),
If that doesn't work, I would put the collision event in the bullet object. This is more efficient anyway, cus bullets are temporary instances, ideally you don't want every enemy instance checking for collisions with every type of projectile. If the collision checks are in the projectiles' step event, it's more efficient cus of how >50% of the time, no projectiles exist. I always have characters doing their ai states and physics\collision, while other objects check for collision with the characters. It makes the character's code alot cleaner and makes the projectiles' code more to the point about what happens with them
i will try that thanks
 
Top