Coroutines

hey guys,

I’d like do a loading screen using coroutines because i need use it in my college job.
i saw this posts :

http://developer.coronalabs.com/forum/2010/11/15/network-image-loader-preloader-using-coroutine

http://howto.oz-apps.com/2011/10/you-asked-for-coroutines.html

but i’m still don’t know how do a loading screen using this.

does anyone can help me to start ?

thanks, [import]uid: 95474 topic_id: 33476 reply_id: 333476[/import]

Hi @Jesseh,
Ask and you shall receive. Here are a few links to get started… but maybe you already found them (easy enough to search for them).

http://lua-users.org/wiki/CoroutinesTutorial
http://www.lua.org/pil/9.1.html
http://www.lua.org/manual/5.1/manual.html#5.2

Coroutines are what I’d consider a highly advanced method in Lua, so unless you have a firm grasp on the language and previous experience in other programming languages, it could be a perilous road. I suppose it depends on your level of fluency in programming.

I’m not aware of any truly “coroutines from the ground up for beginners” kind of guide. If you locate such a guide, please let me know, as it would be useful to mention to others seeking such wisdom.

Best of luck!
Brent
[import]uid: 200026 topic_id: 33476 reply_id: 133035[/import]

Thank you @Brent

I had seen these links, but I still do not know how to use it to make a load screen … :confused:
thank you again.

[import]uid: 95474 topic_id: 33476 reply_id: 133138[/import]

Hi @Jesseh,
I will present this issue to the team and determine if a useful, practical tutorial can be written for it. It’s clearly a powerful method but those guides I linked to are, for the most part, highly advanced and confusing to most developers. I personally would like to see coroutines explained in “layman’s terms”… in other words, using a practical test case that might actually apply in Corona.

Brent
[import]uid: 200026 topic_id: 33476 reply_id: 133152[/import]

hey @Brent

I’d like so much a tutorial from Corona Team more detailed …
Really i think that Coroutines is a highly advanced knowledge.

Thank you so much ^^ [import]uid: 95474 topic_id: 33476 reply_id: 133181[/import]

Hi @Jesseh,
Ask and you shall receive. Here are a few links to get started… but maybe you already found them (easy enough to search for them).

http://lua-users.org/wiki/CoroutinesTutorial
http://www.lua.org/pil/9.1.html
http://www.lua.org/manual/5.1/manual.html#5.2

Coroutines are what I’d consider a highly advanced method in Lua, so unless you have a firm grasp on the language and previous experience in other programming languages, it could be a perilous road. I suppose it depends on your level of fluency in programming.

I’m not aware of any truly “coroutines from the ground up for beginners” kind of guide. If you locate such a guide, please let me know, as it would be useful to mention to others seeking such wisdom.

Best of luck!
Brent
[import]uid: 200026 topic_id: 33476 reply_id: 133035[/import]

Thank you @Brent

I had seen these links, but I still do not know how to use it to make a load screen … :confused:
thank you again.

[import]uid: 95474 topic_id: 33476 reply_id: 133138[/import]

Hi @Jesseh,
I will present this issue to the team and determine if a useful, practical tutorial can be written for it. It’s clearly a powerful method but those guides I linked to are, for the most part, highly advanced and confusing to most developers. I personally would like to see coroutines explained in “layman’s terms”… in other words, using a practical test case that might actually apply in Corona.

Brent
[import]uid: 200026 topic_id: 33476 reply_id: 133152[/import]

hey @Brent

I’d like so much a tutorial from Corona Team more detailed …
Really i think that Coroutines is a highly advanced knowledge.

Thank you so much ^^ [import]uid: 95474 topic_id: 33476 reply_id: 133181[/import]

It was hard but i did !!!

[lua]local load, loadImage

local image = display.newRect(275, 200, 50, 10)
local imageScale = 1

load = function()
local j
for j=1, 100 do
imageScale = imageScale + .1
coroutine.yield()
end
Runtime:removeEventListener(“enterFrame”, loadImage)
end

local task1=coroutine.create(load)

loadImage = function()
coroutine.resume(task1)
image.xScale = imageScale
print(“executando”)
end

Runtime:addEventListener(“enterFrame”, loadImage)[/lua]

Thank you for your support @Brent. [import]uid: 95474 topic_id: 33476 reply_id: 133869[/import]

For examples, I’d suggest something fairly linear but which would normally get snarled in callbacks or become a heavy state machine. Game loops and character AI are fairly fertile ground, here.

You can get pretty far by building around a few specialized wait routines, e.g. for time, something like

[lua]local function WaitMS (ms, update_func)
local ends_at = system.getTimer() + ms
while system.getTimer() < ends_at do
if update_func then
update_func()
end
coroutine.yield()
end
end

– Toy example
timer.performWithDelay(100, coroutine.wrap(function(event)
print(“Start”)

WaitMS(4000, function() print(“Waiting…”) end)

print(“Done!”)

WaitMS(1500)

WaitMS(2000, function() print(“Waiting a little more…”) end)

print(“Done for real.”)

timer.cancel(event.source)
end), 0)[/lua]

Other possibilities include “wait for table field to be true / false”, “wait for function to return true / false”, “wait for transition”, etc.

In one of my snippets I use a coroutine to take a peek at a big long iterative process (incrementally generating a Hilbert curve) and draw it as it goes along:

Hilbert code
Timer code (mostly the same wrap() + performWithDelay() from above
The sample itself
[import]uid: 27791 topic_id: 33476 reply_id: 133886[/import]

thank you @StarCrunch
this will help me more ^^ [import]uid: 95474 topic_id: 33476 reply_id: 133939[/import]

It was hard but i did !!!

[lua]local load, loadImage

local image = display.newRect(275, 200, 50, 10)
local imageScale = 1

load = function()
local j
for j=1, 100 do
imageScale = imageScale + .1
coroutine.yield()
end
Runtime:removeEventListener(“enterFrame”, loadImage)
end

local task1=coroutine.create(load)

loadImage = function()
coroutine.resume(task1)
image.xScale = imageScale
print(“executando”)
end

Runtime:addEventListener(“enterFrame”, loadImage)[/lua]

Thank you for your support @Brent. [import]uid: 95474 topic_id: 33476 reply_id: 133869[/import]

For examples, I’d suggest something fairly linear but which would normally get snarled in callbacks or become a heavy state machine. Game loops and character AI are fairly fertile ground, here.

You can get pretty far by building around a few specialized wait routines, e.g. for time, something like

[lua]local function WaitMS (ms, update_func)
local ends_at = system.getTimer() + ms
while system.getTimer() < ends_at do
if update_func then
update_func()
end
coroutine.yield()
end
end

– Toy example
timer.performWithDelay(100, coroutine.wrap(function(event)
print(“Start”)

WaitMS(4000, function() print(“Waiting…”) end)

print(“Done!”)

WaitMS(1500)

WaitMS(2000, function() print(“Waiting a little more…”) end)

print(“Done for real.”)

timer.cancel(event.source)
end), 0)[/lua]

Other possibilities include “wait for table field to be true / false”, “wait for function to return true / false”, “wait for transition”, etc.

In one of my snippets I use a coroutine to take a peek at a big long iterative process (incrementally generating a Hilbert curve) and draw it as it goes along:

Hilbert code
Timer code (mostly the same wrap() + performWithDelay() from above
The sample itself
[import]uid: 27791 topic_id: 33476 reply_id: 133886[/import]

thank you @StarCrunch
this will help me more ^^ [import]uid: 95474 topic_id: 33476 reply_id: 133939[/import]