Coroutine.status always returning suspended, even when running

Don’t know if this’s a bug or something that I don’t understand about Lua, but coroutine.status is always returning “suspended”, even though the coroutine is clearly running.

local f = coroutine.create(function()

while true do

wait()

print("running")

end

end)

coroutine.resume(f)

while true do

wait()

print(coroutine.status(f))

end

What

3 Likes

You’re printing the status of the coroutine from outside of it, in a different thread. In order for that code to run, the coroutine it’s checking the status of has to be suspended. If you print its status inside itself while it’s running then it will say it’s running.

3 Likes

So how could I check the status of the coroutine outside of itself? Do I need to know its specific thread?

1 Like

You are checking the status outside of itself already. Its status is suspended. If it weren’t suspended then code outside of itself wouldn’t be running.

2 Likes

Oh thought it functioned similarly to spawn(), is there any way to make both pieces of code run along side each other, or is that not possible?

spawn behaves the same, just with a frame delay at the start. There isn’t a way to run simultaneous threads in parallel, which is why there are coroutines. wait suspends the thread and then resumes it later.

3 Likes

Hmm thanks and one more question, is there any way that you can check a status of a coroutine outside of itself, or must it be inside?

You are already checking the status of the coroutine outside of itself, its status is suspended.

2 Likes

Like any ways to check its status while it’s running instead of suspending it then checking it?

The only code that runs while it’s running is inside of it. If code outside of it is running then it’s not running, it’s suspended (or normal if it resumed another coroutine itself).
https://www.lua.org/manual/5.1/manual.html#5.2

2 Likes

Alright, thank you for being patient with me, need to fix my misconception throughout my scripts I guess.