Page 1 of 2

Wait for infinite loop?

Posted: Mon Mar 19, 2012 2:58 am
by mage11561
Code: http://pastebin.com/bkDMWr89

.love http://www.mediafire.com/?tyseo07y763leay (Renamed .zip)

What it's supposed to do: Move an image, wait 1000 milliseconds, and repeat.

What it does: Grey screen and I have to force quit.

I searched around the forum, and got no solutions that worked for me.

If I remove the loop part, it works, but if I add it in, it just freezes and I have to force quit.

love.wait(milliseconds) would be convenient, I've seen it on another game that uses Lua and it works pretty well, but whatever.

I'm doing this on a Mac if that means anything.

Any help here?

Thanks.

Re: Wait for infinite loop?

Posted: Mon Mar 19, 2012 3:31 am
by TechnoCat
I fixed your code.
BUT, you're doing it wrong. love.timer.sleep() should almost never be used.
https://love2d.org/wiki/dt

Code: Select all

function love.load()
	bell = love.graphics.newImage("Files/Bell.png")
	love.graphics.setBackgroundColor(50,50,150)
	mx = 0
	my = 0
end

function move()
	mx = mx + 1
	my = my + 1
	cox = mx
	coy = my
	love.timer.sleep(1000)
end

function love.update(dt)
	move()
end

function love.draw()
	love.graphics.draw(bell,cox,coy)
end

Re: Wait for infinite loop?

Posted: Mon Mar 19, 2012 4:44 am
by veethree

Code: Select all

function love.load()
   bell = love.graphics.newImage("Files/Bell.png")
   love.graphics.setBackgroundColor(50,50,150)
   mx = 0
   my = 0
   waittime = 1 --in seconds
end

function move(dt)
	waittime = waittime - dt
	if waittime < 0 then
	   mx = mx + 1
	   my = my + 1
	   cox = mx
	   coy = my
	   waittime = 1
	end
end

function love.update(dt)
   move(dt)
end

function love.draw()
   love.graphics.draw(bell,cox,coy)
end
This is how you can do it without love.timer.sleep(). It's in seconds though.

Also, cox and coy don't seem necessary, You can just use mx and my to draw the picture.

Re: Wait for infinite loop?

Posted: Mon Mar 19, 2012 8:03 am
by Jasoco
timer.sleep() should probably only be used once in love.run() which is only applicable to you if you use a custom run function. It's not really a real timer. Rather is just used to control framerate and allow the processor to do other things on the computer while the game code waits. i.e. in-between the frames.

Re: Wait for infinite loop?

Posted: Mon Mar 19, 2012 10:08 am
by vrld
There's also hump.timer and cron.lua.
The usage code is pretty much the same:

Code: Select all

-- hump.timer
timer.addPeriodic(1, function() mx, my = mx+1, my+1 end)

-- cron.lua
cron.every(1, function() mx, my = mx+1, my+1 end)
Also, you shouldn't do infinite loops in either of the love callbacks, since this blocks the other callbacks from ever executing. Internally, the main loop works about like this:

Code: Select all

while true do
    process_events()
    dt = calculate_frame_time()
    love.update(dt)
    love.graphics.clear()
    love.draw()
end
So if you have an infinite loop in love.update(), love.draw() will never have a chance to be run.

Re: Wait for infinite loop?

Posted: Mon Mar 19, 2012 9:55 pm
by mage11561
I fixed your code.
BUT, you're doing it wrong. love.timer.sleep() should almost never be used.
https://love2d.org/wiki/dt
Thanks, it worked :D

Now to get it so that the image's position changes to what the mouses position is... Oh well..

Thanks for the help.

Re: Wait for infinite loop?

Posted: Mon Mar 19, 2012 10:42 pm
by Inny
If you want to make your business code single-threaded and not event-driven, while still retaining the love event-loop, you can use coroutines.

Code: Select all

function wait( seconds )
  local dt = 0
  while dt < seconds do
    dt = dt + coroutine.yield(true)
  end
end

function main()
  print("Test one")
  wait(3)
  print("Test two")
  wait(3)
end

function love.load()
  thread = coroutine.wrap( main )
end

function love.update(dt)
  if not thread() then
    love.event.push('q')
  end
end

Re: Wait for infinite loop?

Posted: Tue Mar 20, 2012 4:07 am
by Jasoco
What's a coroutine and how do they work?

Re: Wait for infinite loop?

Posted: Tue Mar 20, 2012 4:27 am
by TechnoCat
Jasoco wrote:What's a coroutine and how do they work?
The are similar to threads. Difference is they take turns being executed instead of running in parallel. They are a feature of Lua.

Re: Wait for infinite loop?

Posted: Tue Mar 20, 2012 4:30 am
by Inny
Sorry for not linking this: http://www.lua.org/pil/9.html

Coroutines provide multiple threads of execution without the complexities of concurrency. In threading, you have two or more executions of your program running with their own function call stacks, operating in tandem. Coroutines are cooperative, in that only one thread is active at any moment, and the switching between threads is controlled by your code.

They're powerful stuff, and newbies and oldbies alike should learn how to use them.