GMS 2 [Solved] While loop crashing when storing condition as a variable

Papa Doge

Member
I'm working on a proof of concept, wall climbing platformer that has some pretty basic collision checks, one of which is to check if a player is about to jump over a wall — and if he/she is — move the player sprite so that it stops at the peak instead of jumping over the peak.

The issue I'm having is that for whatever reason, my code works if I'm not storing my collision check in a variable. Even though from my perspective the results should be identical, the variable version of the collision check crashes the game.

I'll start by showcasing the two bits of code.

The code that does works...
Code:
// if the player is scrambling up the wall and approaching the peak...
if (key_jump) && (approachingPeak) {
    while (!place_meeting(x,y-1,obj_peak)) {
        y -= 1;
    }
}

The code that does not work...
Code:
// if the player is scrambling up the wall and approaching the peak...
if (key_jump) && (approachingPeak) {
    while (!onPeak) {
        y -= 1;
    }
}
And here are the corresponding variable definitions that I am using in both code examples...
Code:
key_jump = keyboard_check_pressed(vk_space);

approachingPeak = place_meeting(x,y+wallScramble,obj_peak);

onPeak = place_meeting(x,y-1,obj_peak);
As you can see, my variable onPeak is simply the stored version of the exact same collision check in the code that works. At this point I am happy to continue without the stored variable approach but not understanding why the second example is failing does not sit right with me.

What am I missing?
 

Paskaler

Member
There is a difference here, and that is, in the first example you execute place_meeting every time you subtract from y, and this has the result of giving you a new value(the collision gets calculated at the new position). While in the second example you only calculate the collisions once and in the loop it just uses the same value over and over again.
 

Papa Doge

Member
There is a difference here, and that is, in the first example you execute place_meeting every time you subtract from y, and this has the result of giving you a new value(the collision gets calculated at the new position). While in the second example you only calculate the collisions once and in the loop it just uses the same value over and over again.
Ah ha! That makes perfect sense and will definitely help me think about things correctly moving forward. Thanks a bunch.
 

TheouAegis

Member
Your original code would have been fine if onPeak was a script, not a variable. It may have even been fine as a macro, but I'm not sure about that.
 
Top