This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
unobtanium's profile picture

[SOLVED] pcall calling

Started by unobtanium, 01 March 2014 - 02:51 PM
unobtanium #1
Posted 01 March 2014 - 03:51 PM
Well, hello, its me again :S

Since i am working on the "Client for OpenPeripheral's Glasses" i am calling methods with the
.callRemote(<peripheral>, <function>)
function.
It is also possible to call arguments with
.callRemote(<peripheral>, <function>, unpack(<argument_table>))

The problem is that the possibility exists that the player didnt put the correct amount or type of arguments in.
I now wanted the program to detect this and tried it with pcall(), but that didnt worked for me at all.

Is there a solution for such an error handling? Maybe i did something wrong with pcall?

Thanks
unobtanium
Edited on 01 March 2014 - 03:36 PM
CometWolf #2
Posted 01 March 2014 - 04:08 PM
Since you can't pass arguments to the function called by pcall, it would have to be something like this

local noErr,res = pcall(
  function()
    peripheral.callRemote(name,func,args)
  end
)
if noErr then
  --sucessfull
  print(res) --return values
else
  --error
  print(res) --error message
end
I would need to see your actual code before i can give you any more help than that.
unobtanium #3
Posted 01 March 2014 - 04:15 PM
Thank you very much. That did the job :)/>
MKlegoman357 #4
Posted 01 March 2014 - 06:55 PM
I think it would be better to just check what variables are passed to your function:


function test (name, age)
  if type(name) ~= "string" then error("Name must be a string!", 2) end
  if type(age) ~= "number" then error("Age must be a number!", 2) end
end
theoriginalbit #5
Posted 01 March 2014 - 11:51 PM
Hey unobtanium,

I know you've got this solved, but it's time for a correction to be made…

Since you can't pass arguments to the function called by pcall
ummmmm………………


local ok, err = pcall(peripheral.callRemote, name, func, unpack(args))

if not ok then
  print(err)
end
Edited on 01 March 2014 - 10:52 PM
unobtanium #6
Posted 02 March 2014 - 02:35 AM
I think it would be better to just check what variables are passed to your function:


function test (name, age)
  if type(name) ~= "string" then error("Name must be a string!", 2) end
  if type(age) ~= "number" then error("Age must be a number!", 2) end
end

I accually cant do this because i might dont know the type of the argument i need :D/> It is for a client application and i can't tell which method has which arguments (i could in some parts, but not all). So i just need a simple error handling which doesnt let the program crash instantly.

- snip -


local ok, err = pcall(peripheral.callRemote, name, func, unpack(args))

if not ok then
  print(err)
end

Oh, well that makes kinda sense ^^ It also gives a bit more overview. Does it have any improvement over the other version or is it just an other version and i accually doesn't matter?
theoriginalbit #7
Posted 02 March 2014 - 02:56 AM
Oh, well that makes kinda sense ^^ It also gives a bit more overview. Does it have any improvement over the other version or is it just an other version and i accually doesn't matter?
well other than the fact that the way CometWolf did it with an anonymous function is pointless — since pcall does support arguments — it does add another function call onto the call stack. Other than that though, functionally it doesn't change how it works, it just adds another layer of abstraction.

The moral though is that pcall does allow for arguments, anything supplied to it after the first argument (which must be a function pointer) will become an argument when it invokes said supplied function.
CometWolf #8
Posted 02 March 2014 - 11:59 AM
aaah, i was not aware of this. good to know.
theoriginalbit #9
Posted 02 March 2014 - 12:02 PM
aaah, i was not aware of this. good to know.
I figured you didn't given your answer. Hence my correction :)/>