Coroutine.Resume Passing Unknown Number Overwriting Original Parameters?

It’s overwriting my Original Parameters, unless I add a wait making sure the thread has yield (suspend) it would print normally.

  • What are these magical numbers?
  • Where/what did they come from?
  • How did they get there?

I couldn’t find any information on it after doing a search on DevHub and DevForum.

Code
-- SERVICE
local RunS = game:GetService('RunService')

-- VARIABLES
local Thread

-- Coroutine
local Coroutine_Create = coroutine.create
local Coroutine_Resume = coroutine.resume
local Coroutine_Yield = coroutine.yield

-- FUNCTIONS
local function Func(...)
	local Tuple = {...}
	while RunS.Heartbeat:Wait() do
		print(unpack(Tuple))
		Tuple = {Coroutine_Yield()}
	end
end

-- SCRIPTS
Thread = Coroutine_Create(Func)
Coroutine_Resume(Thread,"A",1)
Coroutine_Resume(Thread,"B",2)
Coroutine_Resume(Thread,"C",3)
Coroutine_Resume(Thread,"D",4)
Coroutine_Resume(Thread,"E",5)
Coroutine_Resume(Thread,"F",6)

Output

  A 1

  C 3

  E 5

  0.77401149272919 (x2)

  0.054752305150032
2 Likes

Since coroutines run in different threads, Coroutine_Resume resumes the Thread again before runS.Heartbeat is done waiting, and then the thread returns to being suspended, and in my personal experience, coroutines do some very strange things when you resume them before they are suspended.

I think your expectation of this coroutine is:

  1. Resume Thread with "A", 1 passed
  2. New local Tuple containing arguments, wait 1/60 second, print Tuple, becomes suspended again
  3. Resume Thread with "B", 2 passed
  4. New arguments "B", 2 returned to coroutine.yield, creating a new tuple, wait 1/60 second, print Tuple, becomes suspended again
  5. Etc etc etc

The actual logic of the coroutine probably goes like this:

  1. Resume Thread with "A",1 passed
  2. New local Tuple containing arguments, wait 1/60 second
  3. Resume Thread again with "B", 2 passed, except the thread is already running, so no arguments are passed
  4. print (old) Tuple, thread becomes suspended again
  5. Resume Thread with "C",3 passed,…
  6. Etc etc etc

This happens because coroutines run in a separate thread, so they will continue running alongside the thread that called Coroutine_Resume.
Apparently, either two Coroutine_Resumes can be called in 1/60 second, or the script waits for the resume after the first to register before moving on with the code, which wouldn’t make sense but would make for a very interesting quirk of coroutines.

I have no idea what is coming after the first 3 prints, it’s almost like you printed a `wait` there or something…

1 Like

Thank you for explaining the behavior of Coroutine and the insight.

My intention is to reuse the Thread therefore I understand that by using Coroutine_Resume I’m Resuming the same Thread created with Coroutine_Create and not creating a new one, however I would like to find out where or what the magic numbers came from and how they got there.



Thank you for your response.

1 Like