Coroutine issues

So I’m trying to learn about coroutines. I sort of get what they can be used for, but they honestly seem so pointless to me. I still haven’t really found a major use for them. But whatever, my issue is from what I’ve been reading is that you can pause a chunk of code or I guess it’s referred to as a “thread” ?

And resume it at another time. So I’m creating a coroutine function that is an endless loop. Now I assumed that once you pause the coroutine via “yield” that it would stop running it’s contents until resumed? But my loop continues to run and print out “Looping” even after “yield” is called? Thanks.

x = 0

local Thread = coroutine.create(function()
	while true do
		wait(1)
		x = x + 1
		print("looping")
	end
end)
coroutine.resume(Thread)
print("start")
while true do 
	wait(1)
	print(x)
	if (x == 5) then
		coroutine.yield(Thread)
		wait(5)
		print("Reset")
		x = 0
		coroutine.resume(Thread)
	end
end

Infact it seems like my 2nd while loop outside the coroutine is actually pausing and stops running?

coroutine.yield() will yield the current coroutine until coroutine.resume() is called on the same coroutine.

Yea but I check if x = 5 then yield the coroutine “Thread”, but the contents of the function are still running?

You have a nested while true do loop.

x = 0

local Thread = coroutine.create(function()
	while task.wait(1) do
		x = x + 1
		print(x)
		if x == 5 then
			task.wait(1)
			print("Reset")
			task.wait(1)
			x = 0
			print(x)
			task.wait(1)
			print("Ended")
			coroutine.yield()
		end
	end
end)

print("Start")
task.wait(1)
coroutine.resume(Thread)

The reason I’m using a loop is so that I can increment 5 by +1 and constantly check every second if x == 5. And if it does yield the coroutine.

I can’t do that without a loop?

If you test the above it does just that.

Coroutine.yield() can only be called from within the coroutine itself not from another function or externally.

This is true, you can’t pass an argument to coroutine.yield() to end a specified coroutine, coroutine.yield() yields the active coroutine as I believe I stated in my 1st reply.

coroutine.yield() will yield the current coroutine until coroutine.resume() is called on the same coroutine.

Then what’s the point of a coroutine? Like everything I read it seemed like you could pause code and execute it in another function or further down in your code. But if you can only yield from within the coroutine function then why is this useful or when would it be useful? These tutorials are really bad honestly lol

Or can you resume outside of the function? Is it only yield that can’t be called outside the function? Because then that makes sense. But if you can’t use either then I don’t get it haha

You can resume anywhere, providing the coroutine is reachable (in scope), it’s just yielding that must occur within the function of the coroutine itself (often referred to as the coroutine body).

It’s a relatively advanced Lua concept & you likely won’t need it very often. It’s just one of those things that’s readily available if necessary.

x = 0

local Thread = coroutine.create(function()
	while task.wait(1) do
		x = x + 1
		print(x)
		if x == 5 then
			task.wait(1)
			print("Reset")
			task.wait(1)
			x = 0
			print(x)
			task.wait(1)
			print("Ended")
			coroutine.yield()
		end
	end
end)

--execute a big chunk of code here

print("Start")
task.wait(1)
coroutine.resume(Thread) --resume coroutine from earlier

This should demonstrate its potential application.

1 Like

Ah ok thank you. Fyi this is basically what I wanted to test with.

x = 0

local Thread = coroutine.create(function()
	while true do
		wait(1)
		x = x + 1
		print("looping",x)
		if (x == 5) then
			coroutine.yield()
		end
	end
end)
coroutine.resume(Thread)

while true do 
	wait(1)
	print(x)
	if (x == 5) then
		print("Reset")
		x = 0
		wait(10)
		coroutine.resume(Thread)
	end
end