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: 5
Joined: Tue Jan 31, 2017 9:12 pm
Contact:

How do i wait until a certain condiction is given?

Post by arthurgps2 »

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: 2063
Joined: Mon Jun 17, 2013 12:00 pm

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

Post by raidho36 »

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: 262
Joined: Sat Jan 16, 2016 10:30 am

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

Post by 4aiman »

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: 267
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 »

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
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 »

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: No registered users and 16 guests