GMS 2 My game freezes after i kill enemy

Discussion in 'Programming' started by Hepero, Jun 12, 2019 at 7:11 PM.

  1. Hepero

    Hepero Member

    Joined:
    Wednesday
    Posts:
    11
    So when i click play (f5) and start walking in my platformer game and kill enemy the game maker studio 2 runner freezes and i have to close it with task manager.
     
    Kezarus likes this.
  2. YoSniper

    YoSniper Member

    Joined:
    Jun 24, 2016
    Posts:
    394
    My first thought: infinite loop.

    Check your "for" and "while" loops, particularly in any collision code with the enemy object.

    If you still can't find it, I recommend posting your code.
     
    Kezarus likes this.
  3. Bentley

    Bentley Member

    Joined:
    Jun 18, 2017
    Posts:
    721
    You likely have an infinite loop. Do you have any loops that run when you kill the enemy?
     
    Kezarus likes this.
  4. Hepero

    Hepero Member

    Joined:
    Wednesday
    Posts:
    11
    I got 2 "whiles" in enemy object (step event)
     
  5. Hepero

    Hepero Member

    Joined:
    Wednesday
    Posts:
    11
    i dont know. How do i check that?
     
  6. BattleRifle BR55

    BattleRifle BR55 Member

    Joined:
    Jun 24, 2016
    Posts:
    930
  7. Hepero

    Hepero Member

    Joined:
    Wednesday
    Posts:
    11
    So this is my Enemy Object step event:

    vsp = vsp + grv;

    //Horizontal collision
    if (place_meeting(x+hsp,y,oWall))
    {
    while (!place_meeting(x+sign(hsp),y,oWall))
    {
    x = x + sign(hsp);
    }
    hsp = -hsp;
    }
    x = x + hsp;

    //Vertical collision
    if (place_meeting(x,y+vsp,oWall))
    {
    while (!place_meeting(x,y+sign(vsp),oWall))
    {
    y = y + sign(vsp);
    }
    vsp = 0;
    }
    y = y + vsp;

    //Animation
    if (!place_meeting(x,y+1,oWall))
    {
    sprite_index = sEnemyA;
    image_speed = 0;
    if (sign(vsp) > 0) image_index = 1; else image_index = 0;
    }
    else
    {
    image_speed = 1;
    if (hsp == 0)
    {
    sprite_index = sEnemy;
    }
    else
    {
    sprite_index = sEnemyR;
    }
    }

    if (hsp !=0) image_xscale = sign(hsp);
     
  8. YoSniper

    YoSniper Member

    Joined:
    Jun 24, 2016
    Posts:
    394
    So the problem does not occur until you kill the enemy, correct? When the enemy is idly walking around, there is no issue?

    Please post the code that gets executed when you kill the enemy object, and if applicable, the enemy's Destroy Event.
     
  9. Hepero

    Hepero Member

    Joined:
    Wednesday
    Posts:
    11
    So this is enemy object death (step event):

    if (done == 0)
    {
    vsp = vsp + grv;

    //Horizontal collision
    if (place_meeting(x+hsp,y,oWall))
    {
    while (!place_meeting(x+sign(hsp),y,oWall))
    {
    x = x + sign(hsp);
    }
    hsp = 0;
    }
    x = x + hsp;

    //Vertical collision
    if (place_meeting(x,y+vsp,oWall))
    {
    if (vsp > 0)
    {
    done = 1;
    image_index = 1;
    }
    while (!place_meeting(x,y+sign(vsp),oWall))
    {
    y = y + sign(vsp);
    }
    vsp = 0;
    }
    y = y + vsp;
    }

    and this is bullet object (collision with enemy object)

    with (other)
    {
    hp--;
    flash = 3;
    hitfrom = other.direction;
    }

    instance_destroy();
     
  10. Hepero

    Hepero Member

    Joined:
    Wednesday
    Posts:
    11
    and no there is no issue when enemy is walking, only when i kill enemy
     
  11. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,537
    So are you changing sprites when the enemy dies or are you changing to a different object that uses a different Sprite than the one the enemy normally uses? if so, it sounds like your origin-to-bounds measurements are not equal, so when you kill the enemy it's turning into a Sprite that is automatically putting it into the ground. This forces an endless loop because if either of your speeds are 0, it can't move outside of the collision.
     
  12. Hepero

    Hepero Member

    Joined:
    Wednesday
    Posts:
    11
    yeah i use 2 objects for enemy. One is oEnemy that is just walking around and then when i kill it it changes to oDead (oDead has 2 sprites)
     
  13. YoSniper

    YoSniper Member

    Joined:
    Jun 24, 2016
    Posts:
    394
    Thought:

    Remove the code that changes the enemy into oDead upon death, and instead just destroy the instance (for now.)

    If this removes the freezing problem, then the problem code is in the oDead object. If it does not, then the problem is elsewhere in the oEnemy code.
     
    Hepero likes this.
  14. Hepero

    Hepero Member

    Joined:
    Wednesday
    Posts:
    11
    im not sure how to remove that bcs im newbie in coding and game developing, but im following the youtube tutorial from Shaun Spalding, maybe that will help you or smth
     
  15. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,537
    It's your sprites. Your dead object sprite is set up in such a way that it is forcing a collision. Set the dead object's sprite to <No Sprite> and see if that makes the freeze go away.
     
    Yal and Hepero like this.
  16. Hepero

    Hepero Member

    Joined:
    Wednesday
    Posts:
    11
    Ok so it worked but how do i make the death object?
     
  17. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,537
    Post the Sprite info for all of the sprites that the dead object uses and Sprite that the enemy uses.
     
  18. Hepero

    Hepero Member

    Joined:
    Wednesday
    Posts:
    11
    Whitch info? You mean like size or what :D?
     
  19. Hepero

    Hepero Member

    Joined:
    Wednesday
    Posts:
    11
    Ok i have this code

    if (hp <= 0)
    {
    with (instance_create_layer(x,y,layer,oDead))
    {
    direction = other.hitfrom;
    hsp = lengthdir_x(3,direction);
    vsp = lengthdir_y(3,direction)-2;
    if (sign(hsp) != 0) image_xscale = sign(hsp) * other.size;
    image_yscale = other.size;
    }
    instance_destroy();
    }

    And when i removed the code i had this code:

    if (hp <= 0)
    {

    instance_destroy();
    }

    And it worked, but now i dont have the dead animation for the enemy.
     
  20. Yal

    Yal Member GMC Elder

    Joined:
    Jun 20, 2016
    Posts:
    3,389
    You can change the COLLISION MASK independently of the SPRITE, set the collision mask of the death object to either the normal enemy sprite (since it didn't have collisions) or to a 1x1 pixel sprite whose only pixel is 100% transparent (a null-mask that will never collide with anything). Former lets you have it collide if you want, latter ensures it won't get stuck but could look uglier, but both lets you keep the death sprite without it getting permanently stuck.
     

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