Advertisement

Need help with an endless loop!

Started by November 17, 2016 09:49 PM
6 comments, last by davidqueen 8 years ago

Yesterday, I started working on an endless runner in Game maker for practice. Things are going along pretty smoothly, but there's something I caught with the collision.
2l8eyxx.png

Basically, whenever the player collides with a floor object from the specific position shown, the game will go into an endless loop.

Here is the collision code that I have in the player's step event:


if (place_meeting(x,y+vsp,obj_floor)){
    while(!place_meeting(x,y+sign(vsp),obj_floor)){
        y += sign(vsp);
    }
    vsp = 0;
}
while (place_meeting(x,y,obj_floor)){
    x -= global.playerspeed;
    y += global.playerspeed;
}

Any help or advice would be greatly appreciated! :D

Run the game in debug mode and when you experience the endless loop, place a breakpoint on the lines that you think might be at issue (probably the first while statement) and analyze what the relevant variables & functions are actually doing vs what your expectations are.

I suspect that your first while loop isn't behaving as you expect it to. It kinda looks like it's meant to position the character just above the platform but since he's fallen off you have a situation your code wasn't designed to deal with.

Advertisement

Maybe sign(vsp) returns 0 (if vsp is 0), so y isn't incremented/decremented or place_meeting(...) returns always true (or false in first while).

Run the game in debug mode and when you experience the endless loop, place a breakpoint on the lines that you think might be at issue (probably the first while statement) and analyze what the relevant variables & functions are actually doing vs what your expectations are

Okay, so I ran the debugger, and...

dxieqx.jpg

that definitely doesn't look right lol.

Hmmm, so why isn't the player object moving downward if the y value is increasing?

Two questions, what does "place_meeting" do.. and what is the initial value of y and obj_floor.

Hmmm, so why isn't the player object moving downward if the y value is increasing?


Assuming you're thinking of the sprite, it's because you're staying here...


while(!place_meeting(x,y+sign(vsp),obj_floor)){
    y += sign(vsp);
}

...and thus never rendering the next frame.

Instead of trying to use a loop there, why not just use math to determine the position you want to be in and move there directly?

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.
Advertisement

Maybe sign(vsp) returns 0 (if vsp is 0), so y isn't incremented/decremented or place_meeting(...) returns always true (or false in first while).

sign(x) in GML does indeed return 0 if x is 0.

Two questions, what does "place_meeting" do.. and what is the initial value of y and obj_floor.

place_meeting() is a collision checking function - it returns true if the current object will be in collision with the specified object type when placed at the specified location. obj_floor is the type ID for the floor objects.

-----

Anyway, my best guess as to what's happening, based on the information presented, is that we have a (literal) corner case, with the character being scrolled into a block at the corner opposite their vertical speed (e.g. into a block at head-level while falling) while having a small vertical speed (i.e. abs(vsp)<1), such that place_meeting(x,y+vsp,obj_floor) reports a collision while place_meeting(x,y+sign(vsp),obj_floor), which will result in an endless loop of y += sign(vsp) (Game Maker uses floating-point types for all numbers, so that statement never overflows).

Yesterday, I started working on an endless runner in Game maker for practice. Things are going along pretty smoothly, but there's something I caught with the collision.

Basically, whenever the player collides with a floor object from the specific position shown, the game will go into an endless loop.

Here is the collision code that I have in the player's step event:


if (place_meeting(x,y+vsp,obj_floor)){
    while(!place_meeting(x,y+sign(vsp),obj_floor)){
        y += sign(vsp);
    }
    vsp = 0;
}
while (place_meeting(x,y,obj_floor)){
    x -= global.playerspeed;
    y += global.playerspeed;
}

Any help or advice would be greatly appreciated! :D

please edit your question and add some comments to your code so we know what your functions and objects are. Im guessing that

just a hiccup in your logic you want this function:


while (place_meeting(x,y,obj_floor)){
x -= global.playerspeed;
y += global.playerspeed;

to run every frame. so you need to increment the speed, let that update the characters position then check again. just adding a break statement might fix it but you probably shouldn't use a while loop


.while (place_meeting(x,y,obj_floor)){
x -= global.playerspeed;
y += global.playerspeed;
break;

or


if(place_meeting(x,y,obj_floor)// collision detected
{
    x -= speed; //player moves backwards away from object
    y += speed; // player moves up;
}else{//no collision

x+= speed; // player moves along 
} 

PS. sorry if i made syntax error i mostly use c++, been a while since i used GM

This topic is closed to new replies.

Advertisement