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

separate hitboxes created for enemy object is only sticking to one of the enemies in a room

Hi everyone,

I've got a separate hitbox I created for enemy headshots, and it's currently only sticking to one of my duplicate enemy objects, or is only being created once for one enemy, even if there are multiple enemy objects in a room.

Code below:

OBJ_enemy Create Event:

instance_create_layer(x,y,"Instances",o_HitBox);

Hitbox Step Event:

x=o_enemy.x
y=o_enemy.y

For whatever reason the hitbox sticks to the first enemy correctly, and only jumps to the second enemy after I've destroyed the first one. Any suggestions are greatly appreciated!
 
This is the reason. o_enemy is presumably the name of the object. How do you expect the game to know which o_enemy you mean?
Thanks for the reply! Yes you're right that it is the name of the object. So I figured that might be it originally, but I don't know how to designate each enemy object. I tried using o_enemy.id in the hitbox step event but that didn't do anything. Do you have a suggestion to differentiate a way for each enemy object to have their own separate hitbox and have that hitbox follow the enemy object that created it?
 

Roldy

Member
Thanks for the reply! Yes you're right that it is the name of the object. So I figured that might be it originally, but I don't know how to designate each enemy object. I tried using o_enemy.id in the hitbox step event but that didn't do anything. Do you have a suggestion to differentiate a way for each enemy object to have their own separate hitbox and have that hitbox follow the enemy object that created it?
You could do it several ways. But I would suggest you just assign instance variable to track the 'id' of the enemy who creates the hitbox: e.g.

GML:
myHitbox = instance_create_layer(x,y,"Instances",o_HitBox);  // enemy instance knows his hitbox instance
myHitBox.myEnemy = id; // the hitbox instance knows the enemy instance he is a hitbox for
Something like that is fairly straightforward.

But in general you may also be interesting in the functions: instance_number and instance_find

 
You could do it several ways. But I would suggest you just assign instance variable to track the 'id' of the enemy who creates the hitbox: e.g.

GML:
myHitbox = instance_create_layer(x,y,"Instances",o_HitBox);  // enemy instance knows his hitbox instance
myHitBox.myEnemy = id; // the hitbox instance knows the enemy instance he is a hitbox for
Something like that is fairly straightforward.

But in general you may also be interesting in the functions: instance_number and instance_find

Thanks for the reply! I'm a little unclear on parts of that code though.

So where you say:

myHitbox = instance_create_layer(x,y,"Instances",o_HitBox); - Is that going in my enemy Create Event?

Then place:

myHitBox.myEnemy = id; In the hitbox Create Event? Then I'd continue to have the following in step-event?

As I've tried that and I can't get it to work, so I'm, clearly misunderstanding where to place what lines of code.
 

Roldy

Member
Thanks for the reply! I'm a little unclear on parts of that code though.

So where you say:

myHitbox = instance_create_layer(x,y,"Instances",o_HitBox); - Is that going in my enemy Create Event?

Then place:

myHitBox.myEnemy = id; In the hitbox Create Event? Then I'd continue to have the following in step-event?

As I've tried that and I can't get it to work, so I'm, clearly misunderstanding where to place what lines of code.

So you said you were doing this:

OBJ_enemy Create Event:
GML:
instance_create_layer(x,y,"Instances",o_HitBox);
Hitbox Step Event:
GML:
x=o_enemy.x
y=o_enemy.y

And I am suggesting to do this:

OBJ_enemy Create Event:
GML:
// Give enemy an instance variable that is the id of its hitbox instance
myHitbox = instance_create_layer(x,y,"Instances",o_HitBox);

// Then give the hitbox instance an instance variable that is the id of the enemy that created it
myHitBox.myEnemy = id;
Hitbox Step Event:
GML:
x = myEnemy.x;
y = myEnemy.y;
 

Roldy

Member
So you said you were doing this:

OBJ_enemy Create Event:
GML:
instance_create_layer(x,y,"Instances",o_HitBox);
Hitbox Step Event:
GML:
x=o_enemy.x
y=o_enemy.y

And I am suggesting to do this:

OBJ_enemy Create Event:
GML:
// Give enemy an instance variable that is the id of its hitbox instance
myHitbox = instance_create_layer(x,y,"Instances",o_HitBox);

// Then give the hitbox instance an instance variable that is the id of the enemy that created it
myHitBox.myEnemy = id;
Hitbox Step Event:
GML:
x = myEnemy.x;
y = myEnemy.y;

Maybe re-read the manual about objects and instances. Or this section (specifically the example 2 with instance_create_layer):
 
Top