Toribash
Original Post
[HELP]Seconds delay?
function delay_s(delay)
	delay = delay or 1
	time_to = os.time + delay
	while os.time() < time_to do end
end

echo("hello.")
delay_s(5)
echo("goodbye. Well, if the damn delay worked.")
As you can see, the function delay_s takes a number and assigns it to the variable delay, or if you don't give the function a number it will assign it 1 by default. Then the variable time_to gets the time and adds the delay to it, and then waits till it reaches that time.

After returning a "hello", it fails to execute the delay function. The same script works outside of toribash, if you change "echo" to "print".

Is there another way to do this?
os commands are disabled in toribash, try using get_world_state().frame_tick (then multiply delay by 1000 as it's in milliseconds)

Here's a basic timer class if you wanted an example:
Timer = { }
Timer.__index = Timer
Timer.Count = 0

function Timer.New(Time, Recurring,Function)
    Timer.Count = Timer.Count + 1
    sT = get_world_state().frame_tick
    local timer = { }
    setmetatable(timer,Timer)
    timer.Time = Time+sT
    timer.N = Timer.Count
    if(Recurring) then
        timer.Interval = Time
    else
        timer.Interval = -1
    end
    timer.Function = Function
    add_hook("draw2d","timer "..Timer.Count,function() timer:Tick() end)
    return timer
end

function Timer:Tick()
    local sT = get_world_state().frame_tick
    if(self.Time <= sT) then
        self.Function()
        if(self.Interval ~= -1) then
            self.Time = get_world_state().frame_tick+self.Interval
        else
            self:Dispose() --Dispose.
        end
    end
end

function Timer:Dispose()
    remove_hook("draw2d","timer "..self.N)
    self = {}
end
example:
A = Timer.New(1000,0,new function() echo("Moo") end)
i = 0
B = Timer.New(2000,1,new function() echo("Ticked: " .. i); i = i + 1 end)
Last edited by Blam; Aug 31, 2009 at 11:31 PM.
:D