Difference between coroutines and functions?

Ive started reading up on coroutines but i still dont understand how they work, they just run code? what is the point of using coroutines if its still a single thread at a time?

2 Likes

Hi! I might be wrong here, but let me try to explain how I use them :slight_smile:

If I’m not mistaken (I’m not the best at it either), it allows you to run multiple stuff from your script at the same time.

For example, in a “normal” script you cannot have a loop check every half a minute & listen to events.

For example:

while true do
   task.wait(30)
   print("Check something here")
end

someEvent:Connect(function()
  print("Event was fired!")
end)

Here if the loop is on the wait, the event will not be dealt with by the script as it’s still busy with the loop.

However what you could do is putting the loop in a coroutine so the script can both listen for the event and execute the loop.

Once again I’m not a pro in this at all, but I hope this helps! :slight_smile:

So coroutines let the rest of the script run while it is not doing anything?
Does this happen automatically?

I believe so, yes. However, I’ll leave the “final reply” on this question to my fellow colleagues here on the devforum to be sure! :slight_smile:

1 Like

If I recall, a coroutine is basically just another way of running a separate task from the main script without having to delay it at all

One example would be loops, now say I have a while true do loop:

while true do
    print("Banana")
    wait(1)
end

print("Orange")

Now obviously, this would run infinitely (Unless if the script is disabled) So the Orange print would never run correct? Now, say we use a coroutine:

coroutine.resume(coroutine.create(function()
    --Just imagine this as its own separate script within the actual script
    while true do
        print("Banana")
        wait(1)
    end
end))

print("Orange")

This would allow our coroutine function (Or our while true do loop) to run, while at the same time allowing it to print Orange as well!

I suppose you can basically think of coroutines as like allowing multiple tasks within the same script

3 Likes

Ok, but, does the rest of the code only run during the waits or simultaneously?

The coroutine I referenced is basically like its own entire script, so it should be able to print Orange while at the same time repeatedly printing Banana

--This is the main script, adding delays would delay the coroutine before starting up

coroutine.resume(coroutine.create(function()
    --This is basically its own script if it helps you understand it a bit more, there's no delay apart from within the coroutine itself
end))

--This is the main script
1 Like

In a normal function, everything yields to previous lines. If you put a line in a coroutine, that line will not be yielded to.

1 Like

One more question though,

function iterPageItems(pages)
	return coroutine.wrap(function()
		local pagenum = 1
		while true do
			for _, item in ipairs(pages:GetCurrentPage()) do
				coroutine.yield(item, pagenum)
			end
			if pages.IsFinished then
				break
			end
			pages:AdvanceToNextPageAsync()
			pagenum = pagenum + 1
		end
	end)
end

Does coroutine.yield return anything in this scenario?

Unsure about this, but I think coroutine.yield would basically keep pausing the current coroutine until either coroutine.wrap is returned back, or coroutine.resume is called

If you’re talking about the parameters, I think it’ll automatically resume after each item is found

(This is my guess, don’t take my word for it)

1 Like