• 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 enemy attack hitboxes, need help please

M

morganna

Guest
I have made an enemy that attacks a player (with a spear) and i made a hitbox for it to allow for more precise dodging from the player. Problem is, when my player attacks the enemy and the enemy goes in to a stunned sprite the hitbox would still be there till the animation ended... i want this to be functional for multiples of the same enemy in a single room. possibly doing the same attack at around the same time. How do i make the hit box know the enemy (that created it) has been stunned and make it destroy itself? keep in mind that the hit box has to know the one who spawned it has been stunned and not just other objects like it that can spawn the hitbox. (im using gamemaker language on gamemaker studio 2 btw)
 

Slyddar

Member
When you create the attack hitbox instance from within the enemy object, pass a variable which identifies who created it. I call mine "owner" and assign it the id of the enemy. Then in the hitbox code you can only let it run it's damage code if firstly, the owner exists and then the owner's sprite is not the stunned sprite, otherwise it destroys itself.
 
Last edited:
M

morganna

Guest
When you create the attack hitbox object from within the enemy object, pass a variable which identifies who created it. I call mine "owner" and assign it the id of the enemy. Then in the hitbox code you can only let it run it's damage code if firstly, the owner exists and then the owner's sprite is not the stunned sprite, otherwise it destroys itself.
I tried this using instance IDs and messing around with global variables but i cannot seem to make it function as intended... If i may ask, can you show me an example of code which does this please?
 

roozilla

Member
He is saying to pass ot when you create the hitbox. My guess is you create it in your create event. In your step do the same with the logic:

If stunned { destroy hitbox}
Else{var hbox = instance_create(); hbox.owner = id}

Also, you could do without the hitbox object and handle it with a sprite, isStunned bool and collision check in your enemy object
 
Last edited:

Bentley

Member
How do i make the hit box know the enemy (that created it) has been stunned and make it destroy itself?
Code:
// Create attackbox
if (attack)
{
    var attackbox = instance_create_layer(x, y, "invisible", obj_attackbox);
    attackbox.owner = id;
}

// obj_attackbox collision event with enemy
if (owner == other && other.sprite_index == spr_stunned)
{
    instance_destroy();
}
 

Slyddar

Member
You shouldn't need global variables for this. In the enemy, I am assuming you are creating a hitbox instance, which you are then using in a collision with the player to see if the melee swing hit them. When you create it, assign the id of the instance created to a local variable, so you can access it and pass the id of the enemy who created it. Like this:
Code:
var inst = instance_create_depth(x, y, depth, obj_hitbox);
inst.owner = id;  //variable "owner" in this created obj_hitbox will now contain the id of the enemy that created it.
Then in the collision event with obj_hitbox and the player, encompass it all in brackets, and only run it if the enemy is not stunned. You should also check that the owner enemy still exists, if not, we destroy the hitbox.
Code:
if instance_exists(owner) and !owner.stunned { //or what ever variable/state you use to put the enemy in a stunned state
    //deal damage to player code
    ...
} else {
  //destroy the instance here, as enemy is stunned, or does not exist.
  instance_destroy();
}
 
M

morganna

Guest
right, so i did what you two have said with these lines of code:

if sprite_index != sStabLadAT {instance_destroy(oStabLadATHBl); }
else if sprite_index = sStabLadAT && image_xscale = -1 {var hbox = instance_create_layer(x,y,"instances",oStabLadATHBl); hbox.owner = id}

if sprite_index != sStabLadAT {instance_destroy(oStabLadATHBr); }
else if sprite_index = sStabLadAT && image_xscale = 1 {var hbox = instance_create_layer(x,y,"instances",oStabLadATHBr); hbox.owner = id}

(i simplified it to sprite_index to make the code more to the point) but it only prioritised one enemy to use it, so i have tried messing around with it and it has lead to this:

if (distance_to_object (oPlayer) < random_range(50, 45)) && x > oPlayer.x && attack = false {sprite_index = sStabLadAT; hsp = 0; attack = true; var hbox = instance_create_layer(x,y,"instances",oStabLadATHBl); hbox.owner = id}

i then stated elsewhere for it to be destroyed if the animation isn't on the enemy like so:

if sprite_index != sStabLadAT {instance_destroy(oStabLadATHBl)}

even then it still only prioritised one enemy (or in better terms, one of the same object) to use it. what am i doing wrong?

i put two of the same enemy objects in the same room and the hitboxs only spawn when they both do the same attack

edit: oops didnt reload the page immo check out the other posts to see if they work
 

roozilla

Member
If you call instance destroy on oStabLadATHBI it will destroy every hitbox instance not just the hitbox instance attached to your current enemy instance
 
M

morganna

Guest
I've done what the sly just said and it is now working, Big thanks!!!! (don't know how to list who solved it but yeah ill post it down here for anybody else reading)
 
Top