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

[SOLVED] Help with Enemy Shooting

B

bluGecko

Guest
Hi,

Currently my enemies have an aggression system where when they spawn, they're set to a random aggression. With this system I have it set up where some enemies attack the player or another enemy.

My issue is that if I add a "place_meeting" event with the bullet(weapon) that the enemy shoots with the enemy object, as they shoot they receive damage as well.

Essentially what I need is for the enemy to be able to shoot the same bullet that it takes damage from, but without receiving the damage when the enemy shoots it, so that the enemy being shot at is the one receiving damage

Thank you in advanced.

Current Code in the "obj_npc" step event:
Code:
if place_meeting(x, y, obj_weapon_npc){
    hp -= 5;
    with(obj_weapon_npc){
        instance_destroy();
    }
}
Another issue with this code, is that the bullet(weapon) is destroyed as its shot.
 
B

bluGecko

Guest
You can add a owner variable when created.
Owner=enemy.id

When collision check
If id!=bullet.owner receive damage
Hi, thank you for your response. I'm still a bit new to GameMaker and don't quiet understand how to do what you suggested. Would you be able to write up a small 'demo' code, so i can understand it a little better?
 
B

bluGecko

Guest
You can add a owner variable when created.
Owner=enemy.id

When collision check
If id!=bullet.owner receive damage
After doing some googling, I came across "instance_id" and set something up like this:
obj_npc create event:
Code:
owner = instance_id
step event:
Code:
if place_meeting(x, y, obj_weapon_npc){
    if owner != instance_id{
        hp -= 5;
        with(obj_weapon_npc){
            instance_destroy();
        }
    }
}
}
But it doesn't seem to work. What have I done wrong?

Thank you
 

The-any-Key

Member
Code:
// Create bullet in player object
with instance_create(x,y,obj_weapon_npc)
{
    // Set owner
    owner=other.id;
}
Code:
// Get bullet we collide with
var bullet_=instance_place(x,y,obj_weapon_npc)
// Check if we collide with any bullet
if bullet_>-1
{
    // Check if this is our bullet
    // If not just do nothing
    if bullet_.owner!=id
    {
        // Reduce hp
        hp -= 5;
        // Destroy bullet
        with bullet_ instance_destroy();
    }
}
 
Last edited:
B

bluGecko

Guest
Code:
// Create bullet in player object
with instance_create(x,y,obj_weapon_npc)
{
    // Set owner
    owner=other.id;
}
Code:
// Get bullet we collide with
var bullet_=instance_place(x,y,obj_weapon_npc)
// Check if we collide with any bullet
if bullet_>-1
{
    // Check if this is out bullet
    // If not just do nothing
    if bullet_.owner!=id
    {
        // Reduce hp
        hp -= 5;
        // Destroy bullet
        with bullet_ instance_destroy();
    }
}
You are a genius, Thank you very much for the help. What you provided worked perfectly.
 
Top