--didyield(func,...args)--returns true/false denoting whether func yielded when called with args
print(didyield(wait,1))--true
print(didyield(print,'hi'))--false
print(didyield(didyield,didyield))--idk, what should it be?
If you want it to return immediately (and have the called function run in a new thread) in the case where it did yield:
function didyield(func, ...)
local evt = Instance.new("BindableEvent")
local args = {...}
local argCount = select("#", ...)
local reachedEnd = false
evt.Event:Connect(function()
func(unpack(args, 1, argCount))
reachedEnd = true
end)
evt:Fire()
return not reachedEnd
end
â...â only exists directly within the scope of the function itâs an argument of, you canât use the parentâs ... in the inner lambda function like you can normal local variables. You would get this just calling func(...):
11:18:37.624 - :7: Cannot use ââŚâ outside a vararg function
The reason for the arg count explicitly, is that if you pass a nil argument at the end of the argument list, unpack will lose it unless you explicitly record the count:
Not sure if this is even useful but you can probably use tick() to count the time the function took to execture and if itâs too much (like more than a second) you return true as in it yielded.
function didyield(func)
local start = tick()
func()
local finish = tick()-start
if finish > 1 then
return true --yielded
else
return false -- didn't yield
end
end
Update: tested it and seems to be working
function cool ()
wait(5)
end
print(didyield(print)) --false
print(didyield(cool)) --true
That looks more elegant, but itâs really more correct to use Roblox primitives for this. Mixing Roblox engine managed coroutines (spawn / delay / wait / yielding methods) with user managed coroutines (coroutine.create / resume / yield) is tenuous since it has had varying behavior at different points in the engineâs history. Itâs pretty safe at this point with how much people use coroutine.wrap for thread spawning, but thatâs why I went with the object solution instead.
There are some cases where mixing the coroutine library with Roblox engine threads has surprising âbrokenâ looking results.