Page 1 of 3

Delay on a loop?

Posted: Tue Feb 25, 2014 4:56 am
by Findo777
In an easy version of this, there used to be something used to delay: wait()-- number of seconds here


How would I do it in this?

It says nil when I try :(

Re: Delay on a loop?

Posted: Tue Feb 25, 2014 5:07 am
by Findo777
Thanks for anyone willing, I hope this is not complicated

Re: Delay on a loop?

Posted: Tue Feb 25, 2014 5:47 am
by Findo777
I have found this:
... so that a thread can block for a specified amount of time. In love.run, waiting is needed before running the main loop again. Threads can call wait ... M = {} local timers = {} local callbacks = {} function M.start(name, delay, callback) timers[name] = delay callbacks[name] = callback end function ...
by Inny
on Fri Oct 12, 2012 10:32 pm

Forum: Support and Development
Topic: Does love have a 'wait()'?
Replies: 5
Views: 315


Can someone give me a more thorough explanation?
I really have no idea what this is saying.
I started love.lua yesterday and I am quite new

Re: Delay on a loop?

Posted: Tue Feb 25, 2014 8:27 am
by micha
Hi and welcome to our forum.

In the future, please be a bit more patient and wait for an answer a little longer before you post again. You cannot expect us to respond within half an hour. Also, please avoid double posting (you can edit your entries, if you forgot a line).

Now for your question:
If you want to have a delay, then you should not use a wait function. It would stop the whole program and make it unresponsive. Instead, use a timer and dt to measure time. This could be implemented like this:
To begin the delay do this:

Code: Select all

status = 'stopped'
timer = 1
and in the loop that you want to delay you do this:

Code: Select all

if status == 'stopped' then
  timer = timer - dt
  if timer < 0 then
    status = 'running'
  end
else
  -- your normal loop code
end

Re: Delay on a loop?

Posted: Wed Feb 26, 2014 12:38 am
by Findo777
What does dt do?

Re: Delay on a loop?

Posted: Wed Feb 26, 2014 1:25 am
by davisdude

Re: Delay on a loop?

Posted: Thu Feb 27, 2014 5:29 am
by Findo777
Thank you, I have made my script, but now it has an error when I try to run it:


function attack2(dt)
divident = 10
timer = 0
repeat timer = timer + dt/dividend until timer == 0.1
if timer == 0.1 then
timer = 0
Bullet = love.graphics.draw(bullet,xpos,ypos - less)
Bullet = nil
end
timer2 = 0
repeat timer2 = timer2 + dt/dividend until timer == 0.1
if timer2 == 0.1 then
timer = 0
EXP1 = love.graphics.draw(exp1,xpos,ypos - less)
timer2 = 0
end
timer3 = 0
repeat timer3 = timer3 + dt/dividend until timer3 == 0.1
if timer3 == 0.1 then
timer3 = 0
EXP2 = love.graphics.draw(exp2,xpos,ypos - less)
end
timer4 = 0
repeat timer4 = timer4 + dt/dividend until timer4 == 0.1
if timer4 == 0.1 then
timer4 = 0
EXP3 = love.graphics.draw(exp3,xpos,ypos - less)
end
repeat timer5 = timer5 + dt/dividend until timer5 == 0.1
if timer5 == 0.1 then
timer5 = 0
EXP1 = nil
end
timer6 = 0
repeat timer6 = timer6 + dt/dividend until timer6 == 0.1
if timer6 == 0.1 then
timer6 = 0
EXP2 = nil
end
timer7 = 0
repeat timer7 = timer7 + dt/dividend until timer7 == 0.1
if timer7 == 0.1 then
timer7 = 0
EXP3 = nil
end
end



Error says: attempt to perform arithmetic on local 'dt' ( a nil value')


All of your help is appreciated!
Just remember, I am new, so please, pardon my errors

Re: Delay on a loop?

Posted: Thu Feb 27, 2014 7:21 am
by Nixola
Can we see where and how you call attack2? You should call it inside love.update, passing dt to it as argument:

Code: Select all

function love.update(dt)
   ---you can have other things in here too
   attack2(dt)
end
Also, please use the

Code: Select all

 tags and indent your code, otherwise it's really difficult to read.

Re: Delay on a loop?

Posted: Thu Feb 27, 2014 7:38 am
by micha
Your code, though

Code: Select all

timer = 0
repeat timer = timer + dt/dividend until timer == 0.1
if timer == 0.1 then
timer = 0
will not make a delay.
This repeat-loop will just add up some numbers in a split second and you will not notice a delay.

You have misunderstood the concept of making a delay. To make a delay, you should NOT try to change the time it takes to do one game-loop (update + draw), but instead you should keep the game loop running and in each iteration do nothing and then after the time is over, do the next action.

Re: Delay on a loop?

Posted: Fri Feb 28, 2014 12:00 am
by Findo777
Can you give an example of that, or remake my code that way?
I am quite confused.