Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
4

coroutine.yield() at the beginning of a script?

Asked by
LuaQuest 450 Moderation Voter
9 years ago

So, i know basically what coroutine.yield() does. I know it "pauses" a coroutine function, and picks up where it left off when called again. But I've seen this script that has "coroutine.yield()" on line 1 of their script, literally.

coroutine.yield()

-- Script continues

Just curious, what does this do? Is this valid? What could it be used for? Thank you for taking the time to read this, hope you can help.

1 answer

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

What I believe is happening is that ROBLOX's coroutine scheduler is immediately resumeing any Script threads that get yielded. This sort of makes sense, since there's no way for any script you write to resume another script (there's no way to get the active coroutine).

The yield is actually happening -- it's not just being ignored. I can tell because when I run these two scripts

print("a")
coroutine.yield()
print("b")
print("c")
coroutine.yield()
print("d")

I get

a
c
b
d

as the output. This wouldn't be possible if coroutine.yield didn't actually yield the thread.


You can yield with a value. I wonder whether or not this corresponds to a builtin function like wait. I couldn't get it to happen, though.


I can't imagine a reason for why you would do this. It might help with making sure other scripts "run" "before" this one, but that probably isn't very predictable.

It's definitely strange. I don't think it should be put to any use.

1
Thanks for the explanation, very much appreciated. LuaQuest 450 — 9y
Ad

Answer this question