How do i wait until a certain condiction is given?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
arthurgps2
Prole
Posts: 3
Joined: Tue Jan 31, 2017 9:12 pm
Contact:

How do i wait until a certain condiction is given?

Post by arthurgps2 » Tue Jan 31, 2017 9:49 pm

I've tried

timer=require'hump.timer'

timer.script(function(wait)
repeat
wait(0)
until condiction
end)

but it didn't worked. Please help me!

User avatar
raidho36
Party member
Posts: 1685
Joined: Mon Jun 17, 2013 12:00 pm

Re: How do i wait until a certain condiction is given?

Post by raidho36 » Tue Jan 31, 2017 9:52 pm

You shouldn’t wait in a "while" loop, because it hangs the program until condition is met. Rather, you should check every update for your condition and if it's not there, don't take action (don't advance animation, don't move pieces, etc.)

User avatar
4aiman
Party member
Posts: 248
Joined: Sat Jan 16, 2016 10:30 am

Re: How do i wait until a certain condiction is given?

Post by 4aiman » Tue Jan 31, 2017 11:20 pm

A very stupid (and not-that-precise) but working example.

Code: Select all

__POSTPONED = {}

function update_timers(dt)
   for k,v in pairs(__POSTPONED) do
       v.ttl = v.ttl-dt
	   if v.ttl<0 then
	      v.func(v.args)
		  __POSTPONED[k] = nil
	   end
   end
end

function after(dt, func, ...)
   __POSTPONED[#__POSTPONED+1] = {ttl = dt, func = func, args = ...}
end

User avatar
xNick1
Party member
Posts: 252
Joined: Wed Jun 15, 2016 8:27 am
Location: Rome, Italy

Re: How do i wait until a certain condiction is given?

Post by xNick1 » Tue Feb 07, 2017 6:18 pm

Easy way:

Code: Select all

timeWaited = 0
timeToWait = 10

function love.update(dt)
    timeWaited = timeWaited + 1
    if timeWaited >= timeToWait then
        --do something here
    end
end
love.update will loop endlessly,
that way you can get to know when a certain event happens.
In that case you wanna do something after 10 seconds
Currently working on a lucid dreaming adventure called Aarstider: http://bit.ly/2mXmgPI

User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: How do i wait until a certain condiction is given?

Post by Robin » Tue Feb 07, 2017 6:33 pm

xNick1 wrote:Easy way:
That will do something after 10 frames, and then once every frame.

To make it go after 10 seconds, add dt instead of 1. To stop it from activating every frame after that, have a boolean flag that stops that from happening:

Code: Select all

timeWaited = 0
timeToWait = 10
timerActive = true

function love.update(dt)
    timeWaited = timeWaited + dt
    if timerActive and timeWaited >= timeToWait then
        timerActive = false
        --do something here
    end
end
Help us help you: attach a .love.

Post Reply

Who is online

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