The first time I got them to work I was staggered by how fast and unnoticeable the process was. Boom, the objects appeared! Then I learned my second lesson on coroutines: They die. Okay, I thought after sulking for a night, Virtual Coroutines to the rescue. These functions just needed to return a value which could be passed back to itself when ‘resumed’ so that it knew where to continue from, or pass a flag of ‘parse completed; start from the beginning when called next time’. It worked. At least 150 X’s slower! And the code to call these virtuals to action is also a clunker because each must be called explicitly to pass values and check the ‘ended’ flag, whereas the original coroutines started with their own local values and ended by increasing the array number to run the next coroutine when complete. Instead of a simple: if map moved by distance d, coroutine.resume( findObjects[ kind_of_objects ] ), I needed 50 lines of code.
So, can I coerce Lua to reset the coroutine by wrapping it in a function? Would a reset coroutine use its starting locals? How would such a function pass the yield message back, and how do I resume it when it yields? I think I know how to speed up the virtual coroutines a bit, but I already have something that works fast and elegantly — just only once.
In pseudo code, the originals were like
Code: Select all
makeObjects[1] = coroutine.create( function()
local prev, last, counter, temp.table = get(first), get(last), 0, { }
while not finished do
(add objects to temp.table from database if kind=1 and location are okay)
if item is last of its kind then
finished = true
else update counter
if counter = 100 then coroutine.yield() end
end
end
if finished then
populate live.table with temp.table
destroy temp.table in case it will be shorter next time
set next coroutine_sequence to 2 — another type of object from maybe another database
end end)