Well, All I am trying to do, is be able to pass the variables from the coroutine.resume() into the functions. So say I want to use coroutine.resume(co1, 5), I would be able to use the number 5 as a variable to change the sleep within co1. apparently, I am not writing something right, because they never change.
That's not really entirely possible without manually yielding the coroutines and checking events yourself. The coroutine.yield function actually returns the arguments given in coroutine.resume. We can use this to our advantage:
function hello(time)
while true do
print('hello ')
print('Sleeping '..time..' seconds')
local timer = os.startTimer(time)
local newTime, ev, p1
repeat
newTime, ev, p1 = coroutine.yield()
until ev == 'timer' and p1 == timer
time = newTime
end
end
function world(time)
while true do
print('world ')
print('Sleeping '..time..' seconds')
local timer = os.startTimer(time)
local newTime, ev, p1
repeat
newTime, ev, p1 = coroutine.yield()
until ev == 'timer' and p1 == timer
time = newTime
end
end
local routines = {
coroutine.create(hello);
coroutine.create(world);
}
local events = {0.1}
while true do
for i=1, #routines do
local ok, err = coroutine.resume(routines[i], unpack(events))
end
if events[2] == 'timer' or not events[2] then
events[1] = events[1] + 0.1
end
events = {events[1], os.pullEvent()}
end
The "repeat until ev == 'timer' …" stuff is basically a replication of what the sleep function does. It starts a timer, then yields until the next event corresponds with its own timer. These lines:
if events[2] == 'timer' or not events[2] then
events[1] = events[1] + 0.1
end
Make sure the event is actually a timer event before adding 0.1.