Wait for infinite loop?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
mage11561
Prole
Posts: 2
Joined: Sun Mar 18, 2012 11:25 pm

Wait for infinite loop?

Post 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.
User avatar
TechnoCat
Inner party member
Posts: 1612
Joined: Thu Jul 30, 2009 12:31 am
Location: Milwaukee, WI
Contact:

Re: Wait for infinite loop?

Post 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
User avatar
veethree
Inner party member
Posts: 877
Joined: Sat Dec 10, 2011 7:18 pm

Re: Wait for infinite loop?

Post 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.
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Wait for infinite loop?

Post 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.
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: Wait for infinite loop?

Post 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.
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
mage11561
Prole
Posts: 2
Joined: Sun Mar 18, 2012 11:25 pm

Re: Wait for infinite loop?

Post 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.
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: Wait for infinite loop?

Post 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
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Wait for infinite loop?

Post by Jasoco »

What's a coroutine and how do they work?
User avatar
TechnoCat
Inner party member
Posts: 1612
Joined: Thu Jul 30, 2009 12:31 am
Location: Milwaukee, WI
Contact:

Re: Wait for infinite loop?

Post 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.
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: Wait for infinite loop?

Post 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.
Post Reply

Who is online

Users browsing this forum: Amazon [Bot], Bing [Bot] and 1 guest