This is a small tutorial to understand lua coroutines. So, here is a small useless example function:
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:
The use of coroutine.create instead of coroutine.wrap is also easy to understand.
Creado por webultra y Extraído de:
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?"
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
Creado por webultra y Extraído de: