Página 1 de 1

lua coroutine tutorial

Publicado: 06 Ene 2012 16:32
por Ceone
This is a small tutorial to understand lua coroutines. So, here is a small useless example function:
function Print(valor1, valor2)
while true do
valor1, valor2 = coroutine.yield(valor1, valor2);
end
end
PrintEx = coroutine.wrap(Print);
local a, b = PrintEx("Hola", "mundo!");
Debug.Print(a..", "..b.."\r\n");--Will print "Hola,mundo!"
a, b = PrintEx("Cómo", "estas?");
Debug.Print(a..", "..b.."\r\n");--Will print "Cómo,estas?"
Let's take the code line by line:

1.-function Print(valor1, valor2) : Creates a new function named Print and it's able to receive 2 arguments

2.-while true do: Starts an infinite loop

3.-valor1, valor2 = coroutine.yield(valor1, valor2);: Yields (pause) the function (in fact the coroutine) returning 2 arguments (the both inside of the parenthesis "yield(arg1, arg2)" ). The value of other 2 arguments before the "=" (valor1, valor2) will be replaced with the values given by the invoker of coroutine.

4.-end: Finishes the loop statement

5.-PrintEx = coroutine.wrap(Print);: Wraps the function into a coroutine and returns another function.

6.-local a, b = PrintEx("Hola", "mundo!");: Executes the function (wraped coroutine) PrintEx with 2 arguments. The result is stored in a, b.

7.- Debug.Print(a..", "..b.."\r\n");--Will print "Hola,mundo!": Nothing to say

8.- a, b = PrintEx("Cómo", "estas?");: Executes the function (wraped coroutine) PrintEx with 2 new arguments. The coroutine was yielded so it will be resumed every time that the PrintEx function is called. The result is stored in a, b.

9.- Debug.Print(a..", "..b.."\r\n");--Will print "Cómo,estas?": Nothing to say

Another example to understand this better:
function Sum(valor1, valor2)
lap = 0;
while true do
lap = lap + 1;
Debug.Print("Lap: "..lap.." = valor1:"..valor1..", ".."valor2:"..valor2);
valor1, valor2 = coroutine.yield(valor1+valor2);
end
end
SumEx = coroutine.wrap(Sum);
local a = SumEx(1,3);
Debug.Print(a);--Will print 4
a = SumEx(3,4);
Debug.Print(a);--Will print 7
The use of coroutine.create instead of coroutine.wrap is also easy to understand.

Creado por webultra y Extraído de:
HIDE: ON
Hidebb Message Hidden Description

Re: lua coroutine tutorial

Publicado: 06 Ene 2012 17:11
por abood1987
Excellent Thank you

;) ;) ;)

Re: lua coroutine tutorial

Publicado: 06 Ene 2012 20:45
por mecivic
Thank you

Re: lua coroutine tutorial

Publicado: 08 Ene 2012 15:45
por bariza-dz
Thank you

Re: lua coroutine tutorial

Publicado: 04 Sep 2015 13:46
por hddutilite
Manda pra mim amigo

Re: lua coroutine tutorial

Publicado: 12 Sep 2017 19:42
por jhonitimer
thnkssssssssssss

Re: lua coroutine tutorial

Publicado: 29 May 2018 21:29
por mrflowers
gracias

Re: lua coroutine tutorial

Publicado: 20 Mar 2021 20:31
por pexabosh
Ceone escribió:
06 Ene 2012 16:32
This is a small tutorial to understand lua coroutines. So, here is a small useless example function:
function Print(valor1, valor2)
while true do
valor1, valor2 = coroutine.yield(valor1, valor2);
end
end
PrintEx = coroutine.wrap(Print);
local a, b = PrintEx("Hola", "mundo!");
Debug.Print(a..", "..b.."\r\n");--Will print "Hola,mundo!"
a, b = PrintEx("Cómo", "estas?");
Debug.Print(a..", "..b.."\r\n");--Will print "Cómo,estas?"
Let's take the code line by line:

1.-function Print(valor1, valor2) : Creates a new function named Print and it's able to receive 2 arguments

2.-while true do: Starts an infinite loop

3.-valor1, valor2 = coroutine.yield(valor1, valor2);: Yields (pause) the function (in fact the coroutine) returning 2 arguments (the both inside of the parenthesis "yield(arg1, arg2)" ). The value of other 2 arguments before the "=" (valor1, valor2) will be replaced with the values given by the invoker of coroutine.

4.-end: Finishes the loop statement

5.-PrintEx = coroutine.wrap(Print);: Wraps the function into a coroutine and returns another function.

6.-local a, b = PrintEx("Hola", "mundo!");: Executes the function (wraped coroutine) PrintEx with 2 arguments. The result is stored in a, b.

7.- Debug.Print(a..", "..b.."\r\n");--Will print "Hola,mundo!": Nothing to say

8.- a, b = PrintEx("Cómo", "estas?");: Executes the function (wraped coroutine) PrintEx with 2 new arguments. The coroutine was yielded so it will be resumed every time that the PrintEx function is called. The result is stored in a, b.

9.- Debug.Print(a..", "..b.."\r\n");--Will print "Cómo,estas?": Nothing to say

Another example to understand this better:
function Sum(valor1, valor2)
lap = 0;
while true do
lap = lap + 1;
Debug.Print("Lap: "..lap.." = valor1:"..valor1..", ".."valor2:"..valor2);
valor1, valor2 = coroutine.yield(valor1+valor2);
end
end
SumEx = coroutine.wrap(Sum);
local a = SumEx(1,3);
Debug.Print(a);--Will print 4
a = SumEx(3,4);
Debug.Print(a);--Will print 7
The use of coroutine.create instead of coroutine.wrap is also easy to understand.

Creado por webultra y Extraído de:
HIDE: ON
Hidebb Message Hidden Description

Re: lua coroutine tutorial

Publicado: 01 Abr 2021 10:16
por sandrogarcia
Oye tengo un problema algo parecido actualmente con coroutine...

Re: lua coroutine tutorial

Publicado: 06 Oct 2021 15:12
por lazkopat
Thank you