[solved]Is there a way to sleep/wait without freezing window?

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
BlackDiamondPL
Prole
Posts: 15
Joined: Tue Aug 08, 2017 8:38 pm

[solved]Is there a way to sleep/wait without freezing window?

Post 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
Last edited by BlackDiamondPL on Tue Aug 22, 2017 6:28 pm, edited 1 time in total.
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Is there a way to sleep/wait without freezing window?

Post 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
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
BlackDiamondPL
Prole
Posts: 15
Joined: Tue Aug 08, 2017 8:38 pm

Re: Is there a way to sleep/wait without freezing window?

Post 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 :huh:
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Is there a way to sleep/wait without freezing window?

Post 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.
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
BlackDiamondPL
Prole
Posts: 15
Joined: Tue Aug 08, 2017 8:38 pm

Re: Is there a way to sleep/wait without freezing window?

Post 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
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: [solved]Is there a way to sleep/wait without freezing window?

Post 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.
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
LexLoki
Prole
Posts: 1
Joined: Thu Sep 07, 2017 7:18 pm

Re: [solved]Is there a way to sleep/wait without freezing window?

Post 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

Post Reply

Who is online

Users browsing this forum: Google [Bot], Semrush [Bot] and 3 guests