I've been reading about ypcall
on the ROBLOX wiki, and I saw something very interesting that I didn't understand. They were talking about how ypcall is different from pcall
, because you can't "yield" with pcall. Here's the error message you get if you attempt to yield while using pcall:
attempt to yield across metamethod/C-call boundary
... Or so the wiki said.
Just wondering, what exactly does this mean?
And why can't we yield with pcall?
Does this mean I should always use ypcall instead of pcall with ROBLOX Lua?
If anyone can answer any of these questions, I'd really appreciate it. Thanks for your time.
P.S
Got my information from here: http://wiki.roblox.com/index.php?title=Function_dump/Functions_specific_to_ROBLOX#ypcall
Edit
BlueTaslem has brought to my attention that pcall
was indeed changed to be the same as ypcall
. The explanation I gave was for the functions prior to the update. However there still is some potentially interesting stuff in there so I'll leave it there for informative purposes.
Yielding
In RBXLua, it is often important to to use function calls like wait() when you are trying to do things repeatedly, or else the client or server could slow or even freeze altogether. But why do we need to do this?
The reason is Thread Scheduling (which you can read about on the wiki)
When Roblox is processing scripts, It can only process one at a time and just switches between tasks on different scripts using a thread scheduler (this is why it's better to use fewer scripts). So if one task is hogging all the resources, then you can never switch to another task.
You can pause one task and let Roblox process other things by calling a YeildFunction
such as wait()
.
pcall vs ypcall
So why can't pcall
yield when ypcall
can?
In order to figure out why we need to uncover what the meaning behind the error attempt to yield across metamethod/C-call boundary
.
The function pcall
is actually a C function which interprets Lua and will send you an error message. The problem is that yielding (with YeildFunctions
) is Roblox specific and can not be interpreted by the pcall
.
This error is saying that the metamethod used to yield can not be interpreted by the C code.
So this is where ypcall
comes in. This function was made specific for Roblox and can interpret these YeildFunctions
such as wait(). There are some other subtle technical differences that you can read about, but to put things simply it's a pcall
that can yield.
Conclusion
If you need to yield, use ypcall. Otherwise use which ever you'd like as they are both close to identical speeds as far as I've seen.