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

Is there any way to return an unknown amount outside of a protected call?

Asked by
Pyrondon 2089 Game Jam Winner Moderation Voter Community Moderator
8 years ago

The best thing I can compare what I need is variadic functions:

function doStuff(...)
    print(...)
end

But, the other way around, if you will. If I had a function I needed to return the result of, I would do it like so:

return func()

But if I have that in a protected call, and don't know the amount of return values, how would I do that? I tried this:

local r = {}
pcall(function(...)
    r = {remoteFunc:InvokeClient(...)}
end)
return unpack(r)

Which returns nil, whereas without the pcall it returns what I want it to. I'm pretty sure I could do this in a crude way,

local s, e1, e2, e3, e4, e5, e6, e7, eUntilLike100 = pcall(function() end)

But I was wondering if there's already a way to do this I'm not seeing? Other than that, I mean. Thanks in advance.

1 answer

Log in to vote
0
Answered by 8 years ago

Do it the same way you already did.

local r = {pcall(func, ...)};
if r[1] then
  return unpack(r,2)
else
  return error(r[2], 2);
end;
Ad

Answer this question