GML Trouble with fps_real

Cupid Stunt

Member
I'm having issues using fps_real. If I use it to keep time I get a divide by zero error.
If I try to print it to the debug console every second I get a non_init error as shown below.
I am running the game from the IDE. 2.2.4.474
I would like to use fps_real to keep time in my games in case things get bogged down a bit.

Code:
############################################################################################
FATAL ERROR in
action number 1
of Draw Event
for object obj_clock:
Variable obj_clock.fpd_real(100025, -2147483648) not set before reading it.
 at gml_Object_obj_clock_Draw_0 (line 3) -        show_debug_message(string(fpd_real))
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_obj_clock_Draw_0 (line 3)
 

Cupid Stunt

Member
Now I get a divide by zero error if I use fps in my code. This is in the step event of an object:
Code:
############################################################################################
FATAL ERROR in
action number 1
of Draw Event
for object obj_clock:
DoMod :: Divide by zero
 at gml_Object_obj_clock_Draw_0 (line 2) - if(framecount % fps == 0) {
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_obj_clock_Draw_0 (line 2)
 

CloseRange

Member
fps takes a bit of time to kick in. The first 20 or so frames of play and fps will just read 0.
You can just do this:
Code:
if(fps != 0 && framecount % fps == 0) {
if fps!=0 returns false the code will break and not even bother reading the next part of the condition
 

Cupid Stunt

Member
fps takes a bit of time to kick in. The first 20 or so frames of play and fps will just read 0.
You can just do this:
Code:
if(fps != 0 && framecount % fps == 0) {
if fps!=0 returns false the code will break and not even bother reading the next part of the condition
While this works, I have two issues with it.
First, why am I ever getting zero. by the time this code runs I should be rendering frames.
Second, I'm not a big fan of running that same check through the entire program when it's only needed to correct the first use of fps before the window renders.
The program should compile with the target fps in place until the window starts to render and changes that number. Otherwise it's a clock-consuming workaround.
 

Yal

šŸ§ *penguin noises*
GMC Elder
Use parantheses to bind: (framecount % fps) == 0, I think it runs "fps == 0" first and since it can't be zero, it will become false which evaluates to zero for the modulo operation. So you need to do the modulo operation first explicitly using parantheses and THEN the comparison.
 

Cupid Stunt

Member
Use parantheses to bind: (framecount % fps) == 0, I think it runs "fps == 0" first and since it can't be zero, it will become false which evaluates to zero for the modulo operation. So you need to do the modulo operation first explicitly using parantheses and THEN the comparison.
It's a divide by zero error, so that isn't the issue. I did try it though and got the same error.
Thank you for suggesting it though. I do appreciate all attempts at help.
The issue is that fps is 0 before the window loads, and the objects begin to step before the window loads. That's a small but fundamental error in the GMS 2 design from where I sit.
 

CloseRange

Member
we'll why not take a differant approach then. You are trying to use this to keep track of time?
Perhaps use delta_time instead:
Code:
time += delta_time;
Delta_time is simply the number of microseconds that have elapsed since the previous step.
run that code one time every frame and you will know how much time has passed sense the game ran

or even better used in the built in variable current_time
This read only variable will return the number of milliseconds that have passed since the OS was started.
this is a very accurate way to get the time passed.
If you don't care for such accuracy use room_speed
room_speed is essentially your target fps:
Code:
if(framecount % room_speed == 0) {

}
if you are going for a 30fps game, this code will run once every 30 frames (or approximately every second)
room speed is never 0 unless you yourself decide to set it to 0 but then you game will probably break.
 

Cupid Stunt

Member
we'll why not take a differant approach then. You are trying to use this to keep track of time?
Perhaps use delta_time instead:
Code:
time += delta_time;
run that code one time every frame and you will know how much time has passed sense the game ran

or even better used in the built in variable current_time
this is a very accurate way to get the time passed.
If you don't care for such accuracy use room_speed
room_speed is essentially your target fps:
Code:
if(framecount % room_speed == 0) {

}
if you are going for a 30fps game, this code will run once every 30 frames (or approximately every second)
room speed is never 0 unless you yourself decide to set it to 0 but then you game will probably break.
The point is to get accurate time even when there's been a drop in frames actually rendered.

Your solution of delta_time is perfect, thank you.
 
Top