• Hey! Guest! The 39th GMC Jam will take place between November 26th, 12:00 UTC and November 30th, 12:00 UTC. Why not join in! Click here to find out more!

Legacy GM Flooring does not work properly

Crossoni

Member
I was wondering why flooring does not work with this code:
Code:
//Step event
//Replenish skill bar
skill_bar_restore[1] = 0.004;
if (skill_bar[1] < 10) then skill_bar[1] += skill_bar_restore[1];

//Draw event
if (skill[1] != "empty") then draw_sprite(spr_skill_bar,floor(skill_bar[1]),xx_left+margin,yy_top+space*1.5);
For some reason when my skill_bar[1] returns 10, the floor(skill_bar[1]) returns 9. Here is couple ways that fixes it though:
Code:
//Change < to <=
if (skill_bar[1] <= 10) then skill_bar[1] += skill_bar_restore[1];

//Change 10 to 10.01
if (skill_bar[1] < 10.01) then skill_bar[1] += skill_bar_restore[1];
I want to understand why it does this, so can someone tell me what is the problem here?
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Moderator
http://floating-point-gui.de/

Most values in GMS are floats, and so you get floating point errors associated with them. Your value "10" may actually be 9.9999999999999999999999999999999999999998 or something, so flooring it sets it to 9.
 
T

TimothyAllen

Guest
You could also do something like:
Code:
value = max_value * frame / frame_duration;
frame = min(frame + 1, frame_duration);
Or you could make it time based instead of frame based.
 

Crossoni

Member
http://floating-point-gui.de/

Most values in GMS are floats, and so you get floating point errors associated with them. Your value "10" may actually be 9.9999999999999999999999999999999999999998 or something, so flooring it sets it to 9.
Aah well that makes sense, thanks for the article!
You could also do something like:
Code:
value = max_value * frame / frame_duration;
frame = min(frame + 1, frame_duration);
Or you could make it time based instead of frame based.
Hmm, I have a hard time understanding your code, what parts of my code are they supposed to replace?
 
T

TimothyAllen

Guest
Aah well that makes sense, thanks for the article!

Hmm, I have a hard time understanding your code, what parts of my code are they supposed to replace?
It was a generalization for increasing a value from zero to max in a specific duration.
 
Top