Clone a coroutine?

Does anybody know if it’s possible to clone a coroutine?

Maybe this would help?

Just call coroutine.create twice and pass it the same function?

local arguments = {"Hello world!"}
local routines = {}

local function hasArguments(...)
	return select("#", table.unpack{...}) > 0
end

for _ = 1, 2 do
	table.insert(routines, coroutine.create(hasArguments))
end

for index, routine in ipairs(routines) do
	local success, result = coroutine.resume(routine, arguments[index])
	if not success then warn(result) continue end
	print(result)
end

In this code snippet ‘true’ is output on the first pass of the loop and ‘false’ is output on the second pass of the loop.

1 Like