Advertisement
Wyvern67

remoteCall

Oct 29th, 2015
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.35 KB | None | 0 0
  1. function split(str,splitter)
  2.     if not splitter then return end
  3.     if not str then return end
  4.     words = {}
  5.     i=0
  6.     for part in string.gmatch(str, "[^%"..splitter.."]+") do -- get each part
  7.         i=i+1
  8.         words[i] = part
  9.     end
  10.     return words
  11. end
  12. string.split = split
  13.  
  14. function call(func, ...)
  15.     args = {...}
  16.    
  17.     if type(func) == "string" then
  18.         func = getfenv(1)[func]
  19.     end
  20.    
  21.     if type(func) == "function" then
  22.         return func(args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9], args[10])
  23.     else
  24.         error("Non-existant function")
  25.     end
  26. end
  27.  
  28. function remoteInterpret(message)
  29.     args = string.split(message, ",")
  30.     func = getfenv(1)[args[1]] -- getfenv(1) contains all non-local variables (so functions too) so getfenv(1)["test"] returns a function named test, if there's one.
  31.     -- the input looks like that:
  32.     -- functionName,param1,param2...
  33.     -- getfenv(1)[args[1]] returns the function named after the first arg of the function
  34.     return func(args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9], args[10], args[11])
  35. end
  36.  
  37. function remoteCall(ID, func, ...)
  38.     if type(ID) ~= "number" then
  39.         error("expected an ID (number) as a first argument")
  40.     end
  41.     if type(func) ~= "string" then
  42.         error("expected a function name (string) as a second argument")
  43.     end
  44.     args = {...}
  45.     rednet.send(ID, func..","..table.concat(args, ","))
  46. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement