View Single Post
  #12  
Unread 05-29-2014, 11:41 PM
Thurallor's Avatar
Thurallor Thurallor is offline
The Undying
Interface Author - Click to view interfaces
 
Join Date: May 2013
Posts: 202
Coroutines do work, sort of. The following code
Code:
local func = coroutine.create(function(num)
    num = coroutine.yield("a" .. tostring(num));
    num = coroutine.yield("b" .. tostring(num));
    num = coroutine.yield("c" .. tostring(num));
    return "d" .. tostring(num);
end);

result, value = coroutine.resume(func, 1); Turbine.Shell.WriteLine("result = " .. tostring(result) .. "; value = " .. tostring(value));
result, value = coroutine.resume(func, 2); Turbine.Shell.WriteLine("result = " .. tostring(result) .. "; value = " .. tostring(value));
result, value = coroutine.resume(func, 3); Turbine.Shell.WriteLine("result = " .. tostring(result) .. "; value = " .. tostring(value));
result, value = coroutine.resume(func, 4); Turbine.Shell.WriteLine("result = " .. tostring(result) .. "; value = " .. tostring(value));
result, value = coroutine.resume(func, 5); Turbine.Shell.WriteLine("result = " .. tostring(result) .. "; value = " .. tostring(value));
produces the following output:
result = true; value = a1
result = true; value = b2
result = true; value = c3
result = true; value = d4
result = false; value = cannot resume dead coroutine
However, I don't think you can call any API functions within a coroutine (e.g. Turbine.Shell.WriteLine doesn't work), so it seems pretty useless. You can access global variables, though. So if you had a big number crunching task, you could put it in a coroutine that is resumed in an Update() event handler, and spread out the computations over time until complete. I haven't tried this, but it seems like it would work.

Edit: I suppose you could pass a function and arguments to coroutine.yield(), and call the function in Update() when it's returned by coroutine.resume().

Last edited by Thurallor : 05-29-2014 at 11:50 PM.
Reply With Quote