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.
Wait for infinite loop?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- TechnoCat
- Inner party member
- Posts: 1612
- Joined: Thu Jul 30, 2009 12:31 am
- Location: Milwaukee, WI
- Contact:
Re: Wait for infinite loop?
I fixed your code.
BUT, you're doing it wrong. love.timer.sleep() should almost never be used.
https://love2d.org/wiki/dt
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?
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
Also, cox and coy don't seem necessary, You can just use mx and my to draw the picture.
- Jasoco
- Inner party member
- Posts: 3727
- Joined: Mon Jun 22, 2009 9:35 am
- Location: Pennsylvania, USA
- Contact:
Re: Wait for infinite loop?
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?
There's also hump.timer and cron.lua.
The usage code is pretty much the same:
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:
So if you have an infinite loop in love.update(), love.draw() will never have a chance to be run.
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)
Code: Select all
while true do
process_events()
dt = calculate_frame_time()
love.update(dt)
love.graphics.clear()
love.draw()
end
Re: Wait for infinite loop?
Thanks, it workedI fixed your code.
BUT, you're doing it wrong. love.timer.sleep() should almost never be used.
https://love2d.org/wiki/dt
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?
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
- Jasoco
- Inner party member
- Posts: 3727
- Joined: Mon Jun 22, 2009 9:35 am
- Location: Pennsylvania, USA
- Contact:
Re: Wait for infinite loop?
What's a coroutine and how do they work?
- TechnoCat
- Inner party member
- Posts: 1612
- Joined: Thu Jul 30, 2009 12:31 am
- Location: Milwaukee, WI
- Contact:
Re: Wait for infinite loop?
The are similar to threads. Difference is they take turns being executed instead of running in parallel. They are a feature of Lua.Jasoco wrote:What's a coroutine and how do they work?
Re: Wait for infinite loop?
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.
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.
Who is online
Users browsing this forum: Ahrefs [Bot], Amazon [Bot] and 13 guests