Page 1 of 1
[solved]Is there a way to sleep/wait without freezing window?
Posted: Tue Aug 22, 2017 1:48 pm
by BlackDiamondPL
love.timer.sleep() makes window freeze but to agree most people need have sort of function that does the same but not freezing widow.
Any way to do that?
In my case i need to "slow down" my for loop
Re: Is there a way to sleep/wait without freezing window?
Posted: Tue Aug 22, 2017 2:00 pm
by davisdude
To do this you would need to only update every x frames, where x is the amount by which you want to slow down. For instance, if you want your game to move at 1/2 speed, you would do something like this:
Code: Select all
-- main.lua
local numberOfSlowFrames = 2
local currentSlowFrames = 1
function love.update( dt )
if currentSlowFrames == numberOfSlowFrames then
-- Regular game update
-- Reset the counter
currentSlowFrames = 1
else
currentSlowFrames = currentSlowFrames + 1
end
end
Of course you would need code to control whether or not you're actually slowing the time down or not (unless you want time slowed down all the time, that is). This is just a general outline
Re: Is there a way to sleep/wait without freezing window?
Posted: Tue Aug 22, 2017 2:26 pm
by BlackDiamondPL
Well it's working but for some reason my code doesn't run.
I have put two 'print`s' and output shows "1 2 1 2 ..."
but my code is not running
Re: Is there a way to sleep/wait without freezing window?
Posted: Tue Aug 22, 2017 4:38 pm
by davisdude
Are you sure you're incrementing the variable correctly and have not mistyped the assignment? That's the only thing I can think of without looking at your code.
Re: Is there a way to sleep/wait without freezing window?
Posted: Tue Aug 22, 2017 6:28 pm
by BlackDiamondPL
After fidling around my code.
I got it working, needed to make things read-able for program in order.
I was using local variables in love.load.
Thank you for help!
One thing. I would change this:
Code: Select all
if currentSlowFrames == numberOfSlowFrames then
To this:
Code: Select all
if currentSlowFrames >= numberOfSlowFrames then
idk but i prefer doing this in love.update
Re: [solved]Is there a way to sleep/wait without freezing window?
Posted: Tue Aug 22, 2017 7:02 pm
by davisdude
Variables are local to their scope, which is loosely "their indentation level" assuming you indent your code normally. You can still make the variables local by declaring them outside of love.load, in one of two ways:
Code: Select all
local currentSlowFrames, numberOfSlowFrames
function love.load()
currentSlowFrames = 1
numberOfSlowFrames = 1
end
-- OR
-- (outside of love.load)
local currentSlowFrames, numberOfSlowFrames = 1, 1
Either way works, it's mostly just stylistic differences between the code.
Re: [solved]Is there a way to sleep/wait without freezing window?
Posted: Thu Sep 07, 2017 7:26 pm
by LexLoki
Since a sleep function usually take sleep duration (miliseconds or seconds) as arguments, a way to do it, just "sleeping" the update callback with a timer, is:
Code: Select all
local sleepTimer = 0
function setSleep(duration) --call setSleep(2) to "sleep" update for 2 seconds
sleepTimer = duration
end
function love.update(dt)
if sleepTimer > 0 then --Is sleeping
sleepTimer = sleepTimer - dt
else
-- Do your normal update loop
end
end