Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

How to hold fps in a variable?

Little late on my part, but what I want its the Frames Per Second, like when one presses the f2 key.
What i want is to make something a like:

'if game its lagging return'

Thank you :thumb:
 
Graphics.frame_rate IS the current Frames Per Second.

#If game is lagging
if Graphics.frame_rate < 5

I'm not sure about the '5', and you may want to set a counter so you can say:

If the frame rate is below a certain threshold for X number of frames in a row.

Otherwise, a short lag would make it return as well.
 
Oh yeah, I meant to post something but I forgot. Graphics.frame_rate is the value for the frame limiter. I did a quick attempt at making my own low resolution timer that does FPS but was getting weird results and didn't have time to figure out why. I was getting an actual framerate range of 32 to 62 even though the frame limiter was set to 40. I was using Ruby's Time class to get the info, so maybe it's a problem with that. Maybe I'll try it with a DLL and Window's QueryPerformance[Counter|Frequency] but I think I use DLLs for everything now haha.
 
I wrote this code some time ago:
Ruby:
module FramesPerSecond

  

  def self.setup

    @ticks = 0

    @last_tick = self.get_time

    @fps = Graphics.frame_rate

    @frames_this_second = 0

    @average = [@fps]

  end

  

  def self.get_time

    (Time.now.to_f * 1000).round

  end

  

  def self.update

    @frames_this_second += 1

    @ticks += 1

    time = self.get_time

    delta = time-@last_tick

    if delta >= 1000

      @last_tick = time

      @fps = @frames_this_second/(delta/1000)

      @frames_this_second = 0

      @average << @fps

    end

  end

  

  def self.fps

    @fps

  end

  

  def self.average

    n = 0

    @average.each {|i| n += i}

    (n/@average.size).round

  end

  

  def self.milliseconds_since_last_tick

    self.get_time-@last_tick

  end

  

  def self.ticks

    @ticks

  end

  

  self.setup

  

end

 

class << Graphics

  if !method_defined?(:neo_bahamut_fps_alias_method_graphics_update)

    alias_method :neo_bahamut_fps_alias_method_graphics_update, :update

    def update

      neo_bahamut_fps_alias_method_graphics_update

      FramesPerSecond.update

    end

  end

  

  def need_update?

    FramesPerSecond.milliseconds_since_last_tick > 8000

  end

end

You can use FramesPerSecond.fps to get the real FPS (not the limit).
It shows other numbers than RMXP (with F2 in debug mode) sometimes. I think that's because my script counts the number of frames and calculates the FPS each second and RMXP uses another method, possibly 1 / time_passed_since_last_frame.

/edit: Didn't I already post that script inside the Scripts section? :O
 
Yeah, that's more or less what I wrote except I was getting the time per frame, and FPS was only calculated when it was asked for not every 1000ms. If you are trying to make it faster, then what you should have done is make it so that it doesn't even get the time until X frames have passed. The way you have it now, it's actually gonna be slower than mine because you are still calculating time every frame, but every 1000ms you are automatically calculating FPS even when it might not be needed.

EDIT: Oh yeah, don't assume that the Ruby Time class has ms resolution, because it might not. In fact, that's one possible reason why my FPS counter may have been reading 60+FPS when the limiter was at 40. Perhaps the actual resolution of the timer is more coarse.
 
That's what I thought, but I am not sure. I know that it can return microseconds but I am not sure how accurate it actually is. If I do end up making the dll based timer, it'll be microsecond resolution, which will be good but wholly unnecessary considering RMXP runs at 40FPS usually up to a 120FPS max :P
 
@ DeM0nFiRe: Both ways have different advantages. My one should be faster when you ask for the FPS multiple times each frame.
Anyway, using Time.now takes about 0.000002 seconds, so with FPS = 40, you still have 0.99992 seconds a second for other calculations :P
 
The time resolution depends on the computer system clock, normaly 10 ms on most computers, still some have high precision and can be 1 ms. Only really old computers will have lower resolutions.

For getting the diff in ms between 2 time objects, just do time - time which returns a float representing the number of seconds and the fractional part being the microseconds. So, "(time - time) * 1000" = diff in ms.
 
Yeah, 1 - 10 ms resolution is technically enough for RMXP (unless you want to actually use the 100+ frame limit) but still, 1 - 10ms is really low resolution, unless of course you do what neobahamut did, which is average over time (I still say you should average over a number of frames, not over time). If you have a high resolution timer, though, you can get the actual framerate for any given frame, not just the average over time.

Also vgvgf the way you described to use the Time class is already how neo bahamut did it :P

I did it a different way, I retrieved the floating point representation of the unix timestamp and worked with those. Either ways works the same. I haven't looked at the source code but that's actually probably what time - othertime does internally.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top