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

Discussion in 'Programming' started by Papa Doge, Dec 2, 2018.

  1. Papa Doge

    Papa Doge Member

    Joined:
    Nov 24, 2018
    Posts:
    41
    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?
     
  2. Paskaler

    Paskaler Member

    Joined:
    Jul 4, 2016
    Posts:
    315
    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 likes this.
  3. Papa Doge

    Papa Doge Member

    Joined:
    Nov 24, 2018
    Posts:
    41
    Ah ha! That makes perfect sense and will definitely help me think about things correctly moving forward. Thanks a bunch.
     
  4. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,889
    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.
     
    Papa Doge likes this.

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