Hello, thanks for reading my post.
- What do I want to achieve?
A better understanding of coroutines, as I am trying to learn how to effectively utilize them. Specifically, when practicing, I am often confused about how task.wait() interacts and/or prematurely yields a coroutine before its corresponding yield call.
- What is the issue?
Really, I am just looking for someone to explain to me the proper usage of task.wait() in coroutines, specifically in conjunction with while loops.
For example, I was just messing around in a Script and came up with this example:
local yieldNumber = 5
local baseNum = 0
local function solveMath()
print("Coroutine is started")
while task.wait(1) do
print("Loop has been entered")
baseNum = baseNum + 1
end
print("first yield about to happen")
coroutine.yield(baseNum)
task.wait(1)
print("Loop finished")
return "Done!"
end
local number = coroutine.create(solveMath)
local success, result = coroutine.resume(number)
print("Success: " , success)
print("Result: " , result)
task.wait(3)
local success, result = coroutine.resume(number)
print("Success: " , success)
print("Result: " , result)
It seemed rather straightforwards to me. Upon calling coroutine create, the function would be called and wait 5 seconds (due to the loop) before the yield call, and the number 5 would be returned.
However, this is very far from the case. When running the above code the output looks like this:
Coroutine is started
Success: true
Result: nil
Loop has been entered (x2)
first yield about to happen
Success: true
result: 2
Loop finished
This is not what I would expect to happen, and in addition to that, I sometimes get a warning that states âtask.wait should not be called on a thread that is already âwaitingâ in the task libraryâ.
I would be lying if I said I understood what this means.
In addition to that, removing the task.wait() and structuring the function like this:
local function solveMath()
print("Coroutine is started")
while baseNum <= yieldNumber do
print("Loop has been entered")
baseNum = baseNum + 1
end
print("first yield about to happen")
coroutine.yield(baseNum)
print("Loop finished")
return "Done!"
end
works exactly as I expected the original attempt to. But I do not understand why putting a task.wait() inside of a while loop, which I thought was common practice, could mess up my interpretation of how coroutines worked.
Lastly, after watching a tutorial on the subject, I saw this chunk:
local wrappedCoActive = false
local runTimes = 0
local newWrapCo = coroutine.wrap(function(message)
runTimes = 0
while wrappedCoActive == true do
runTimes+=1
print(message)
task.wait(1)
if runTimes >= 3 then
wrappedCoActive = false
end
end
coroutine.yield()
end)
wrappedCoActive = true
newWrapCo("My message!")
And everything works exactly as intended. There are no issues using task.wait().
Really I would just appreciate some input about using task.wait() in conjunction with while loops inside of a coroutine, because I am struggling to understand how it all works together.
Thanks in advance