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
[solved]Is there a way to sleep/wait without freezing window?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Prole
- Posts: 15
- Joined: Tue Aug 08, 2017 8:38 pm
[solved]Is there a way to sleep/wait without freezing window?
Last edited by BlackDiamondPL on Tue Aug 22, 2017 6:28 pm, edited 1 time in total.
Re: Is there a way to sleep/wait without freezing window?
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:
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
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
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
-
- Prole
- Posts: 15
- Joined: Tue Aug 08, 2017 8:38 pm
Re: Is there a way to sleep/wait without freezing window?
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
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?
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.
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
-
- Prole
- Posts: 15
- Joined: Tue Aug 08, 2017 8:38 pm
Re: Is there a way to sleep/wait without freezing window?
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:
To this:
idk but i prefer doing this in love.update
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
Code: Select all
if currentSlowFrames >= numberOfSlowFrames then
Re: [solved]Is there a way to sleep/wait without freezing window?
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:
Either way works, it's mostly just stylistic differences between the code.
Code: Select all
local currentSlowFrames, numberOfSlowFrames
function love.load()
currentSlowFrames = 1
numberOfSlowFrames = 1
end
-- OR
-- (outside of love.load)
local currentSlowFrames, numberOfSlowFrames = 1, 1
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
Re: [solved]Is there a way to sleep/wait without freezing window?
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
Who is online
Users browsing this forum: Bing [Bot], Semrush [Bot] and 4 guests