Coroutine.resume with a loop?

I’m quite confuse how to use coroutine.resume on a loop.

example

local function loop()
      for i, v in pairs(table) do
      --code
      coroutine.yield()
end

local loopfunction = coroutine.create(loop)
coroutine.resume(loopfunction)

So the thing that I’m stuck on, is that when there’s only one coroutine.resume, it would run the code once or twice or smth, that’s the problem, if I loop on a table and I don’t know how much time it would loop though, what do I do with coroutine?

1 Like

it’s pretty simple, you just gotta call the function in the coroutine.


local function loop()
      for i, v in pairs(table) do
      --code
        
    end
end

local loopfunction = coroutine.create(loop)
    loop()
end)
coroutine.resume(loopfunction)
1 Like

I ran into a issue while trying, the loopfunction doesn’t turn into a function, it turns into a variable.

I just made it
local loopfunction = coroutine.create(function(loop)

But now it doesn’t work at all, I tried adding prints to the function but nothing prints out.

function yield(barf)
 coroutine.yield(barf)
end

local foo do
 local function bar()
  for i = 1, 10 do
   print('hello nothing')
  end
  yield(foo)
 end
 foo = coroutine.create(bar)
end

coroutine.resume(foo)
1 Like

also remember, coroutine.yield() is in there.

doesn’t work, it seems like the function only runned once.

it works without the yield but I need the yield because thats the purpose of what I’m trying to achieve, which is yielding.