-2

I've tried

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

but it didn't work. Please help me!

arthurgps2
  • 101
  • 1
  • 7
  • 1
    that didn't work is not a proper error description. Please read [ask] also it is not clear what you are waiting for and if you want to do something else while waiting or not. you should also provide more code and context. what is timer.script? I can only assume that it is from the HUMP library and if, what do you expect to happen if you do nothing but wait for 0 seconds? – Piglet Jan 24 '17 at 12:15
  • Are you looking for a function to be called on a given event (i.e. a callback function for a keypress that fires every time a key is pressed) or something akin to a do-while loop? – Thelmund Jan 24 '17 at 18:42
  • i used hump.timer and timer.script is a way to use `wait` instead of `timer.after` – arthurgps2 Jan 25 '17 at 00:37

1 Answers1

0

timer.script is not really designed for what you're trying to do, though it might be possible to get it to work.

The LÖVE framework is built around the draw() and update() callbacks and I would recommend learning how to do this task with just these callbacks, before moving on to methods that layer on top of these callbacks. Something like this should run your code only once, when your condition is first met:

local hasHappened = false
function love.update(dt)
  if (condition and not hasHappened) then
    hasHappened = true
    -- respond to condition here
  end
end

Typically, you wouldn't check your condition directly in love.update(). Instead, you would have a table that contains all the objects in your game, and in love.update() you loop through this table of objects and call an update() method on each one. That gives each object a chance to check for different conditions and respond to them.

An alternative approach would be to name your condition and to use an event system like beholder to trigger the event (and any callback functions that are registered) when the condition happens.

Or (provided your timer's update() is being called in love.update()), you could do it with your timer object and the every() method:

local handle = timer:every(0.01, function()
  if condition then
    -- unregister timer, assuming you only want the code to be run once
    timer:cancel(handle)

    -- respond to condition here
  end
end)
Peter Rust
  • 402
  • 4
  • 14
  • How do i use timer.cancel? – arthurgps2 Feb 03 '17 at 13:28
  • arturgps2: A `timer` is actually more like a clock than a timer, since it manages multiple timers simultaneously. In order to cancel a timer, like the `every()` in the code sample above, you need to store a handle (a reference) to that particular `:every()` timer, as is done above, and pass that as the first argument to `cancel()`. Note the use of the colon `:cancel` instead of the dot notation `.cancel`, since this is a method call. That's about all I can think of, if you have more questions, you'll need to be a bit more specific. – Peter Rust Feb 03 '17 at 16:48
  • 1
    Actually i forgot to `timer.update` into `love.update`,but this suggestion helped me too. Thanks for the help. – arthurgps2 Mar 23 '17 at 19:49