Delay on a loop?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Findo777
Prole
Posts: 36
Joined: Tue Feb 25, 2014 4:09 am

Delay on a loop?

Post 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 :(
Findo777
Prole
Posts: 36
Joined: Tue Feb 25, 2014 4:09 am

Re: Delay on a loop?

Post by Findo777 »

Thanks for anyone willing, I hope this is not complicated
Findo777
Prole
Posts: 36
Joined: Tue Feb 25, 2014 4:09 am

Re: Delay on a loop?

Post 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
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Delay on a loop?

Post 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
Findo777
Prole
Posts: 36
Joined: Tue Feb 25, 2014 4:09 am

Re: Delay on a loop?

Post by Findo777 »

What does dt do?
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Delay on a loop?

Post by davisdude »

GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
Findo777
Prole
Posts: 36
Joined: Tue Feb 25, 2014 4:09 am

Re: Delay on a loop?

Post 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
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Delay on a loop?

Post 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.
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Delay on a loop?

Post 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.
Findo777
Prole
Posts: 36
Joined: Tue Feb 25, 2014 4:09 am

Re: Delay on a loop?

Post by Findo777 »

Can you give an example of that, or remake my code that way?
I am quite confused.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 103 guests