1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

Adjusting variables to control range bounds for configuration.

Discussion in 'Programming' started by csanyk, Nov 4, 2018.

  1. csanyk

    csanyk Member

    Joined:
    Jun 20, 2016
    Posts:
    821
    I have a configuration editor in one of my projects, so players can adjust certain game variables to change the game to suit their skill level and tastes.

    I made a GUI for it, with an object that you can click on to increment a variable up or down.

    One of the game variables is constrained to a range of values between 1 and 9. The game configuration can be edited to change the min/max values of this random range, within the 1-9 range. That is to say, you can make min be any number between 1-9, and max can be any number between 1-9, and min must be <= max. So, the default range is 1-9, but by editing the config, the player may select a different range within those constraints, say 3-7, or 5-5. But never can the range be inverted, for example a range of 7-4 would not be allowed.

    The game variables are stored in a ds_map, and when I finalize the config changes, the ds_map gets converted to json and written to an external file. When the game launches, it checks for this file, and if it finds the file, the json is read out of it, converted back into a ds_map, and the variables are used in the game.

    This all works, with one weird glitch that I don't understand:

    With the range variable I mentioned above, if I increase the minimum bound of the range above the current maximum bound of the range, the way my code works, it automatically increases the maximum range value so that the range is never upside down. The weird part about this is, say min_range and max_range are both currently equal to 6. I click the up button on the min_range value. The expected result of this should be that min_range gets increased by 1, so should now be 7. And since min_range is now higher than max_range, this should therefore bump the max_range variable to be 7 as well. And this does happen, but it takes two clicks. The first click, max_range increases -- even though I'm clicking on the min_range++ button. The second click, min_range now increases. I can't understand why my code does this.

    The relevant snippet of code is as follows:

    Code:
    ///Step Event
    var temp_min = clamp(switches[? "min"].value, 1, switches[? "max"].value);
    var temp_max = clamp(switches[? "max"].value, switches[? "min"].value, 9);
    
    switches[? "min"].value = temp_min;
    switches[? "max"].value = temp_max;
    
    Prior to this version of my code, I was just updating the variables directly, without the temp variables, like this:

    Code:
    ///Step Event
    switches[? "min"].value = clamp(switches[? "min"].value, 1, switches[? "max"].value);
    switches[? "max"].value = clamp(switches[? "max"].value, switches[? "min"].value, 9);
    
    When this ran, it would update the max variable in the same step as the min variable if I incremented min to be higher than max. But if I tried to decrement max, it would halt when its value was equal to min, unable to decrement below min. If I reversed the order of execution of the two lines of code, then it would work in reverse -- I could decrement max below min, and min would update to be equal to the new value of max in the same step, but then I couldn't increase min above max and have max increase.

    What I want is that when I increase min_range, min_range would increase, and max_range should be pushed upwards if need be, both incrementations in the same step, and when I decrease max_range, max_range would decrease, and min_range would get pushed downward if need be.

    The temp variable approach kindof-almost works, I just can't figure out why I have to click the control twice, and why when I click it the first time, the variable controlled by the other button increases first while the variable I'm clicking to change remains where it is.

    I've been thinking about it for two days, and I just can't seem to understand my own code.

    So I'm asking the community:

    1) Why is my code behaving this way?
    2) How can I write the code to behave in the way I want it to?
     
  2. flyingsaucerinvasion

    flyingsaucerinvasion Member

    Joined:
    Jun 20, 2016
    Posts:
    2,221
    I'm pretty sure you need to know which of either values is being changed. Otherwise how would you tell the difference between whether min was increased or max was decreased?
    Code:
    //if changing MIN:
        MIN = clamp(MIN, 1, 9);
        MAX = max(MIN, MAX);
    
    //if changing MAX:
        MAX = clamp(MAX, 1, 9);
        MIN = min(MIN, MAX);
    
     
    Last edited: Nov 5, 2018
    csanyk likes this.
  3. NightFrost

    NightFrost Member

    Joined:
    Jun 24, 2016
    Posts:
    2,005
    When you press your min_range++ button, does it directly alter the switches[? "min"] value? If so, your code runs with the following logic, assuming min and max start at 6, and replacing variables with actual values:
    Code:
    var temp_min = clamp(7, 1, 6); // becomes 6.
    var temp_max = clamp (6, 7, 9) // becomes 7.
    
    switches[? "min"].value = 6;
    switches[? "max].value = 7;
    
     
  4. xot

    xot GMLscripter GMC Elder

    Joined:
    Apr 22, 2016
    Posts:
    116
    Yep, that. If you can't determine which value changed, you can't determine which value takes precedence.
     
    csanyk likes this.
  5. TeamSteeve

    TeamSteeve Member

    Joined:
    Jun 20, 2016
    Posts:
    3
    It's about the order in which these things happen.
    Your first line of code would set temp_min to 6 because you're clamping to the max value which has has not changed to 7 yet.

    Your second lot of code would do a similar thing.

    This should work:

    Code:
    if( switches[? "min"].value < switches[? "max"].value ){
        switches[? "max"].value = clamp(switches[? "max"].value, switches[? "min"].value, 9);
        switches[? "min"].value = clamp(switches[? "min"].value, 1, switches[? "max"].value);
    }
    else{
        switches[? "min"].value = clamp(switches[? "min"].value, 1, switches[? "max"].value);
        switches[? "max"].value = clamp(switches[? "max"].value, switches[? "min"].value, 9);
    };
     
  6. csanyk

    csanyk Member

    Joined:
    Jun 20, 2016
    Posts:
    821
    Thank you, that was the missing key bit of info that I needed. I got it working the way I need it now.
     

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