1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

GMS 2 enemy attack hitboxes, need help please

Discussion in 'Programming' started by morganna, Mar 28, 2019.

  1. morganna

    morganna Member

    Joined:
    Mar 28, 2019
    Posts:
    6
    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)
     
  2. TheSly

    TheSly Member

    Joined:
    Jan 16, 2017
    Posts:
    1,001
    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: Mar 28, 2019
  3. morganna

    morganna Member

    Joined:
    Mar 28, 2019
    Posts:
    6
    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?
     
  4. roozilla

    roozilla Member

    Joined:
    Feb 4, 2017
    Posts:
    111
    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: Mar 28, 2019
  5. Bentley

    Bentley Member

    Joined:
    Jun 18, 2017
    Posts:
    823
    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();
    }
     
  6. TheSly

    TheSly Member

    Joined:
    Jan 16, 2017
    Posts:
    1,001
    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();
    }
    
     
    morganna likes this.
  7. morganna

    morganna Member

    Joined:
    Mar 28, 2019
    Posts:
    6
    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
     
  8. roozilla

    roozilla Member

    Joined:
    Feb 4, 2017
    Posts:
    111
    If you call instance destroy on oStabLadATHBI it will destroy every hitbox instance not just the hitbox instance attached to your current enemy instance
     
  9. morganna

    morganna Member

    Joined:
    Mar 28, 2019
    Posts:
    6
    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)
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice