• 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!

GML Prevent number from exceeding a number [SOLVED]

OnLashoc

Member
So I have a heat meter that ranges from 0 to 100.

In the Create Event
Code:
heatCurrent = 0;
In the Step Event
Code:
if heatCurrent <= 1{
    heatCurrent = 0;
}

if heatCurrent >= 100{
    heatCurrent = 100;
}
When you fire a weapon the heat increases, different weapons have different amounts the heat increases. Over time the heat if greater than 0, heat automatically dissipates -1 per second. Everything is working until I fire a weapon that causes the heatCurrent to go over 100. What's happening is, it will briefly flash to say 110 then to 100 over and over stuck in a loop.

I understand that because of my code above if heatCurrent >= 100 then heat Current = 100 is causing it to be stuck at 100. However, if i remove that, it allows the heat to go until forever if you keep firing to which I do not want it to display. I want it to go to 100 and then begin to count down -1 every other step and get out of that loop it's currently stuck in.

So I've tried using clamp to prevent it from going above 100 or below 0 but it doesn't seem to work.

Any suggestions?

Onnie
 

OnLashoc

Member
Ok, I didn't actually solve this issue but as a work around, to display the current heat which I didn't include above, I have this in the

Draw Event:
Code:
draw_text(room_width/2 - 395, room_width/2 - 35, string("HC: "));
    draw_text(room_width/2 - 370, room_width/2 - 35, string(str6));
So what I did is in the Step Event is took out the:

Code:
if heatCurrent >= 100{
    heatCurrent = 100;
}
Then in the Draw Event I changed it to this:

Code:
if heatCurrent >= 100{
        draw_text(room_width/2 - 395, room_width/2 - 35, string("HC: "));
        draw_text(room_width/2 - 370, room_width/2 - 35, string("100"));
    } else {
    draw_text(room_width/2 - 395, room_width/2 - 35, string("HC: "));
    draw_text(room_width/2 - 370, room_width/2 - 35, string(str6));
    }
On the weapon object I added to the Create Event:
Code:
weaponActive = 0;
Step Event:
Code:
if oPlayerstatus.heatCurrent >= 100{
  weaponActive = 0;
} else {
   weaponActive = 1;
}
Which allows the weapon to basically be offline until the heat drops to below 100. Still open for suggestions though.

Thanks!
Onnie
 

curato

Member
clamp definitely is the way to go you could do something like the following
Code:
heatCurrent = clamp(heatCurrent + 1, 0, 100);
you could use max and min similarly if you are adding and subtracting to a number, but I like clamp for something like this because the acceptable range is self documented in the code.
 

Amon

Member
clamp definitely is the way to go you could do something like the following
Code:
heatCurrent = clamp(heatCurrent + 1, 0, 100);
you could use max and min similarly if you are adding and subtracting to a number, but I like clamp for something like this because the acceptable range is self documented in the code.
I didn't even know that existed. It's useful to me beyond words. Thanks you.
 

OnLashoc

Member
I tried that but I didn't have the heat current + 1, I had it written

Heat current = clamp(heatCurrent, 0, 100);

Which is probably why it wasn't working. I had to run some errands but I'll definitely try it again when I get home. Thanks!

Onnie
 

chamaeleon

Member
Any suggestions?
Also show the code that actually modifies the heatCurrent variable to increase the "heat", and clearly explain which codes goes in what object types, in case more than one object is involved. For example, given that you posted the step code that does not increment the "heat" but only tries to clamp it, I would assume it's in some other object type. Given then two different instances competing for the updating this variable, it's entirely conceivable your clamping step event runs first for one instance, then your increase "heat" step code for another instance, followed later by the drawing which will not have a clamped value until the next step. Just throwing that thought out there, I don't have enough information to tell me whether it is at all relevant.

Edit: Also start thinking about using debugging methods to keep track of important data during the execution of your game and check it against expected conditional code execution to ensure the code you think should run does run and does what it is supposed to.
 

OnLashoc

Member
Gotcha.

So I have about 10 different weapons. Each weapon has a different animated Sprite, damage, energy consumption, reload times, and heat per shot. And 3 different weapon objects.

oWeapon1 (for projectiles w 10 different types of missiles)
oWeapon2 (for lasers w 10 different types of lasers)
oWeapon3 (for missiles w 10 different types of missiles)

Placed in specific locations around the hud. I'm sure you get the jist.

If missile1 is selected the oWeapon1 displays the appropriate Sprite for that type of missile etc etc for each weapon object. The different weapons are setup on a 2Darray on oWeaponselections with that specific objects reason for being it's to contain the list of weapons and their values.

So I have an oControl object that only contains keystrokes for all the controls in the game. So if oWeapon1 has missile1 as its selected missile type, and let's say ord"A" is pressed, oWeaponselections.item[1,1,1,1,1,1] is called and changes oPlayerstatus.heatCurrent to oPlayerstatus.heatCurrent + let's say 15.

Now because heatCurrent is now > 0, it will now begin to -1 every second until it reaches 0 again.

Sorry I'm still not home so I can't copy and paste the extract code, but that's it in a nutshell. But thank you for your reply because it makes sense and will carry that knowledge for forward! :D

Onnie
 
Last edited:

OnLashoc

Member
clamp definitely is the way to go you could do something like the following
Code:
heatCurrent = clamp(heatCurrent + 1, 0, 100);
you could use max and min similarly if you are adding and subtracting to a number, but I like clamp for something like this because the acceptable range is self documented in the code.
So this worked and works well, I like my work around a little better for heatCurrent specifically, but I applied it to my armor so in combination with my work around when you are reaching the heat cap and if you fire a weapon, the weapon deactivates until your heat drops back below 100 and for how many heat points is cost to fire said weapon, your armor ticks down -1 until heatCurrent is back under 100.

The effect is pretty neat honestly lol!

Thanks again, this helped a lot and all I was missing was the "+ 1" initially lol and I was able to eliminate this:

Code:
if headHealth <=1{
    headHealth = 0;
}

if headHealth >=98{
    headHealth = 100;
}

if torsoHealth <=1{
    torsoHealth = 0;
}

if torsoHealth >=98{
    torsoHealth = 100;
}

if lArmhealth <=1{
    lArmhealth = 0;
}

if lArmhealth >=98{
    lArmhealth = 100;
}

if rArmhealth <=1{
    rArmhealth = 0;
}

if rArmhealth >=98{
    rArmhealth = 100;
}

if lLeghealth <=1{
    lLeghealth = 0;
}

if lLeghealth >=98{
    lLeghealth = 100;
}

if rLeghealth <=1{
    rLeghealth = 0;
}

if rLeghealth >=98{
    rLeghealth = 100;
}
Onnie
 
Top