1. Hey! Guest! The XXX GMC Jam will take place between August 24th, 12:00 UTC (Friday noon) and August 27th, 12:00 UTC (Monday noon). Why not join in! Click here to find out more!
    Dismiss Notice

GMS 2 [SOLVED]Wierd Infinite While Loop

Discussion in 'Programming' started by brogolem35, Aug 10, 2018.

  1. brogolem35

    brogolem35 Member

    Joined:
    Aug 2, 2018
    Posts:
    9
    Hello, I was trying to make a platform object works like platforms in Terraria. I made a very flawed collision for it for testing. My original collision script:
    Code:
    hrepeat = hsp / 30;
    vrepeat = vsp / 46;
    hrepeat = abs(hrepeat);
    vrepeat = abs(vrepeat);
    hmod = hsp % 30;
    vmod = vsp % 46;
    hmod = abs(hmod);
    vmod = abs(vmod);
    hrep = 1;
    vrep = 1;
    hbool = true;
    vbool = true;
    
    //Horizantal
    if(place_meeting(x,y,oBlock1))
    {
        x = round(x);
        while(!place_meeting(x+sign(hsp),y,oBlock1))
        {
            x += sign(hsp);
        }
        hsp = 0;
    }
    else if(place_meeting(x+hmod*sign(hsp),y,oBlock1))
    {
        x = round(x);
        while(!place_meeting(x+sign(hsp),y,oBlock1))
        {
            x += sign(hsp);
        }
        hsp = 0;
    }
    else
    {
    while(hrep <= hrepeat)
    {
        if(place_meeting(x+((hrep * 30)+hmod)*sign(hsp),y,oBlock1))
        {
            x = round(x);
            while(!place_meeting(x+sign(hsp),y,oBlock1))
            {
                x += sign(hsp);
            }
            hsp = 0;
            hrep = hrepeat + 1;
        }
        else
        {
            hrep += 1;
        }
    }
    }
    
    //Vertical
    if(place_meeting(x,y,oBlock1))
    {
        y = round(y);
        while(!place_meeting(x,y+sign(vsp),oBlock1))
        {
            y += sign(vsp);
        }
        vsp = 0;
    }
    else if(place_meeting(x,y+vmod*sign(vsp),oBlock1))
    {
        y = round(y);
        while(!place_meeting(x,y+sign(vsp),oBlock1))
        {
            y += sign(vsp);
        }
        vsp = 0;
    }
    else
    {
    while(vrep <= vrepeat)
    {
        if(place_meeting(x,y+((vrep * 46)+vmod)*sign(vsp),oBlock1))
        {
            y = round(y);
            while(!place_meeting(x,y+sign(vsp),oBlock1))
            {
                y += sign(vsp);
            }
            vsp = 0;
            vrep = vrepeat + 1;
        }
        else
        {
            vrep += 1;
        }
    }
    }
    
    And this is the flawed vertical code that I made:

    Code:
    //Vertical
    if(vsp > 0)
    {
        falling = true;
    }
    else falling = false;
    if(place_meeting(x,y,oBlock1) || (falling && !key_down && collision_rectangle(x-15, y+23, x+15, y+23+vsp, oPlatform0, false, true)))
    {
        y = round(y);
        while(!place_meeting(x,y+sign(vsp),oBlock1) || (!key_down && !collision_rectangle(x-15, y+23, x+15, y+23+sign(vsp), oPlatform0, false, true)))
        {
            y += sign(vsp);
        }
        vsp = 0;
    }
    else if(place_meeting(x,y+vmod*sign(vsp),oBlock1))
    {
        y = round(y);
        while(!place_meeting(x,y+sign(vsp),oBlock1))
        {
            y += sign(vsp);
        }
        vsp = 0;
    }
    else
    {
    while(vrep <= vrepeat)
    {
        if(place_meeting(x,y+((vrep * 46)+vmod)*sign(vsp),oBlock1))
        {
            y = round(y);
            while(!place_meeting(x,y+sign(vsp),oBlock1))
            {
                y += sign(vsp);
            }
            vsp = 0;
            vrep = vrepeat + 1;
        }
        else
        {
            vrep += 1;
        }
    }
    }
    
    Yes, I know, it is very flawed. But there is a thing. When my character is about to touch the platform the game freezes. I looked at the code with debug mode. It got frozen because of the infinite while loop. When I delete "!place_meeting" it works fine but isn't "or"(||) works when one the statement is true?
    Sorry, my grammar went wild
     
  2. brogolem35

    brogolem35 Member

    Joined:
    Aug 2, 2018
    Posts:
    9
    Okay, I solved the problem. I needed to use "and" instead of "or".
     

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