• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Variable doesn't seem to change even though I'm using "variable -= otherVariable"

S

SpiceEdge

Guest
My code is supposed to draw a white rectangle and an orange rectangle on top. The white rectangle is supposed to get bigger in the y-direction when you hover over it, and go back to normal when you stop hovering over it. But when not hovering over the rectangles, the white rectangle doesn't seem to go back to normal, even though im clearly using "resize -= resizeSpeed;" with resizeSpeed being a constant.
This is my code:
GML:
resizedBottomYPos = bottomYPos + clamp(resize, 0, (2 * global.theoryRectangleHeight));

if (touchingRectangles) {
    resize += resizeSpeed;
    draw_rectangle_color(leftXPos, topYPos, rightXPos, resizedBottomYPos, c_white, c_white, c_white, c_white, false);
    draw_rectangle_color(leftXPos, topYPos, rightXPos, bottomYPos, global.theoryRectangleColor, global.theoryRectangleColor, global.theoryRectangleColor, global.theoryRectangleColor, false);
} else {
    if (resize > 0) {
        resize -= resizeSpeed;
        draw_rectangle_color(leftXPos, topYPos, rightXPos, resizedBottomYPos, c_white, c_white, c_white, c_white, false);
        draw_rectangle_color(leftXPos, topYPos, rightXPos, bottomYPos, global.theoryRectangleColor, global.theoryRectangleColor, global.theoryRectangleColor, global.theoryRectangleColor, false);
    } else {
        resize = 0;
        draw_rectangle_color(leftXPos, topYPos, rightXPos, bottomYPos, c_white, c_white, c_white, c_white, false);
        draw_rectangle_color(leftXPos, topYPos, rightXPos, bottomYPos, global.theoryRectangleColor, global.theoryRectangleColor, global.theoryRectangleColor, global.theoryRectangleColor, false);
    }
}
The variable "touchingRectangles" is a variable that is true or false depending on if you're hovering over the rectangles and is set in another object. It's like a function that checks if you're hovering over the rectangle. This variable is not the problem as the rectangle clearly gets bigger if you're hovering over it, so the "function" that checks for the hover clearly works (And I'm just using an "else" for if the mouse isn't hovering so it can't be the check for "when not hovering" in said "function" that is working incorrectly)

Does anyone know what the problem is? I don't see any problem in the code so I don't know if you're going to find one either.
 

Nidoking

Member
Are you sure this isn't an order of operations issue? You're computing resizedBottomYPos based on resize, then changing the value of resize, and then using the value of resizedBottomYPos to draw the rectangle without recomputing it. How exactly is touchingRectangles computed? Does it take the resizing into account? Also, WHEN is it calculated? Did you set it one time and then not re-assign the value when it should change? Variables are not closures in GML.

Also, while it appears that you're clamping the value of resize when using it in the calculation, you're not actually clamping the value of the variable itself. Have you used the debugger or a debug draw to monitor the value of resize? Maybe what's happening is that resize is getting much, much bigger than theoryRectangleHeight and while it's decreasing when you're not hovering, it decreases so slowly that you're not waiting for it to get down to the size where it would actually shrink the drawn rectangle.
 
How long are you hovering for? resize could just be a gargantuan number and is taking a while to come back down, but you can't tell because you're clamping its drawn value. Considering the additional repetition throughout the entire block, I'd just rewrite it all from scratch.
 
S

SpiceEdge

Guest
Are you sure this isn't an order of operations issue? You're computing resizedBottomYPos based on resize, then changing the value of resize, and then using the value of resizedBottomYPos to draw the rectangle without recomputing it. How exactly is touchingRectangles computed? Does it take the resizing into account? Also, WHEN is it calculated? Did you set it one time and then not re-assign the value when it should change? Variables are not closures in GML.

Also, while it appears that you're clamping the value of resize when using it in the calculation, you're not actually clamping the value of the variable itself. Have you used the debugger or a debug draw to monitor the value of resize? Maybe what's happening is that resize is getting much, much bigger than theoryRectangleHeight and while it's decreasing when you're not hovering, it decreases so slowly that you're not waiting for it to get down to the size where it would actually shrink the drawn rectangle.
Yes the order of operations is fine as even though on the first frame it doesn't do anything, it'll change on the frame after. TouchingRectangles does take resizing into account and is calculated in the end step event of another object.
After debugging the resize variable I can see that it keeps getting bigger forever and never goes down, after hovering over it for a little, which could mean that touchingRectangles is being calculated wrong, but I don't think that is the case, as I have written on the thread. How would you suggest for me to "clamp" or set a max value for resize, if, as you said, it is true that it doesnt clamp the value of the variable? Do I do something like "resize = clamp(resize)" before calculating resizedBottomYPos or does that also not work? I don't really know how the clamp function works, and the F1 documentation doesn't really help.
 
S

SpiceEdge

Guest
After doing what I wrote in my reply (resize = clamp(resize.....) resize does not go over the max value I set for it, though it never goes down and doesn't stop going up even if I stop hovering over it mid "movement". Should I add the "touchingRectangles" code for if that is the thing that is causing the problem? It's really scuffed but looking at it I would be lead to think it works, as it does recognize that im hovering over the rectangle. It just maybe doesn't recognize that I have stopped hovering, or maybe it doesn't set it to false correctly.
 
S

SpiceEdge

Guest
Nvm lmao. I looked at the "touchingRectangles" code again and I forgot to make it turn the value to false if im not hovering. Im truly stupid. It works now. Thanks anyways for telling me that the clamp of the variable didn't work like that. Atleast I still learned something with my mistake
 
Top