How to set different frames per second and updates per second and be able to change after loading?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Kreative
Prole
Posts: 4
Joined: Tue Feb 16, 2016 6:17 pm

How to set different frames per second and updates per second and be able to change after loading?

Post by Kreative »

Hello,

Many games, with a best example I can think of being Minecraft, have a specific updates per second usually from 20 to 60 and frames per second unlimited or limited to a specific amount, but this value (only the fps) is also often allowed to be changed by the user during the time the game is being played. This forum probably has an answer to the question of setting a custom frames and updates per second but I never found anywhere how to fully implement this feature. I would like to set a specific updates per second but allow the user to customize their frames per second, or set it to unlimited within a game menu any time they want.

I am fairly new at this game engine but I am jumping ahead to things that I consider very important to whether I should make my game with the game tool, which is Love2D currently, or another... And if I have many questions linking basics to any answer I can learn the basics accordingly!

Kind Regards,
Kreative
User avatar
RonanZero
Citizen
Posts: 90
Joined: Mon Oct 20, 2014 3:33 am

Re: How to set different frames per second and updates per second and be able to change after loading?

Post by RonanZero »

The Source Engine also works this way. It's called "ticks." It keeps track of how many ticks should be done in the amount of time passed, and ticks until it catches up. It's probably a better solution than dt, you won't have things going through walls.

http://greyminecraftcoder.blogspot.com/ ... -loop.html
while true do end;
Kreative
Prole
Posts: 4
Joined: Tue Feb 16, 2016 6:17 pm

Re: How to set different frames per second and updates per second and be able to change after loading?

Post by Kreative »

RonanZero wrote:The Source Engine also works this way. It's called "ticks." It keeps track of how many ticks should be done in the amount of time passed, and ticks until it catches up. It's probably a better solution than dt, you won't have things going through walls.

http://greyminecraftcoder.blogspot.com/ ... -loop.html
Oh yeah I know I know, I tried modding and server modding for the game and played it far too long to ever forget that. But I generally prefer the word update because the meaning I am expressing is synonymous and I generally prefer the word for sounding better, having better looking letters in my mono-font and the same length as launch and render :D.

By default is draw and update unlimited so I could just dupe my code from Java into Lua for my specific game clock or if it's implemented by default is there some easy way to change it? I would also prefer to make the clock very precise to the nanosecond scale.

Kind Regards,
Kreative
User avatar
rougan
Citizen
Posts: 58
Joined: Wed Aug 12, 2015 10:30 am

Re: How to set different frames per second and updates per second and be able to change after loading?

Post by rougan »

Can't say I fully understand your question, but you may find some luck looking into love.run :)
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: How to set different frames per second and updates per second and be able to change after loading?

Post by pgimeno »

I'm not sure if you want to lock your FPS to a chosen value, or to make your game logic update on certain intervals. If you want to lock your FPS to a chosen value, this is one way to do it:

Code: Select all

local FPS = 40
local nextTime = love.timer.getTime() + 1/FPS -- moment of time to wait for

function love.update(dt)
  -- Do this at start
  local timedif = nextTime - love.timer.getTime() -- are we there yet?
  if timedif > 0 then love.timer.sleep(timedif) end -- wait if not
  nextTime = nextTime + 1/FPS -- advance the timer to the next moment on time

  -- Rest of your update here
end

function love.draw()
  love.graphics.print(love.timer.getFPS())
end
It's probably not the best implementation. It would be better to manipulate love.run so that events are not delayed until the next frame. As is, the cycle is: read events - wait - update - draw - present - read events - ... but ideally the wait should be between presenting the screen and reading the events, for maximum responsiveness. That can be done by either manipulating love.run (the clean but less future-proof way) or by manipulating love.graphics.present (the dirty but safer way).

But if you want your game to work with ticks of fixed time, without sleeping so that the rest of the code still responds, it's also possible. Just use the timer to trigger your tick code instead of to sleep.
Kreative
Prole
Posts: 4
Joined: Tue Feb 16, 2016 6:17 pm

Re: How to set different frames per second and updates per second and be able to change after loading?

Post by Kreative »

Are the functions love.update and love.draw limited to run every specific interval to produce a certain number of times each second? Are the updates and frames limited or unlimited? If they are unlimited I'll just use another way of limiting that I have written in Java because it's very efficient! Otherwise, I would like to know how I could either remove the limitation or change it to whatever I desire, and also after the love.load function, and within the love.update function.

Kind Regards,
Kreative
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: How to set different frames per second and updates per second and be able to change after loading?

Post by zorg »

If vsync is off (and you don't force it on löve via other means, like the nvidia control panel for those cards), then love.run's loop will execute as fast as it can (granted, with the sleep command still in it at the end), giving you around 1000 cycles per second to work with.
That way, you can "wrap" some accumulators/counters around the update and draw function calls, making them only be called every x interval. Let's say 60 times per second for the update, and either the current screen's refresh rate, or unbounded, for the draw.
This can all be implemented, along with changing both values.
If you leave vsync off, no matter what you want, it won't be precise because the whole love.run loop is bound by the screen refresh rate.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Kreative
Prole
Posts: 4
Joined: Tue Feb 16, 2016 6:17 pm

Re: How to set different frames per second and updates per second and be able to change after loading?

Post by Kreative »

zorg wrote:If vsync is off (and you don't force it on löve via other means, like the nvidia control panel for those cards), then love.run's loop will execute as fast as it can (granted, with the sleep command still in it at the end), giving you around 1000 cycles per second to work with.
That way, you can "wrap" some accumulators/counters around the update and draw function calls, making them only be called every x interval. Let's say 60 times per second for the update, and either the current screen's refresh rate, or unbounded, for the draw.
This can all be implemented, along with changing both values.
If you leave vsync off, no matter what you want, it won't be precise because the whole love.run loop is bound by the screen refresh rate.
Hmm, say could you edit the run loop to your liking then? And what does love use in any case, the run function seems pretty familiar from Java which gets a lot of it's stuff from C and C++ which Lua is written in. I was thinking of creating a game as manually as possible but importing custom libraries seemed a bit too complicated as I couldn't find any detailed tutorial, that's why I am trying Love but I'm curious to know what Love uses to work so I could maybe use the same but in my own fashion.

Kind Regards,
Kreative
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: How to set different frames per second and updates per second and be able to change after loading?

Post by zorg »

Yes, see: [wiki]love.run[/wiki].
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 5 guests