daily pastebin goal
84%
SHARE
TWEET

Untitled

a guest Nov 7th, 2014 347 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. rpc = {}
  2.  
  3. rpc.activeCalls = {}
  4. rpc.slug = "rpc-"
  5. rpc.ttl = 120
  6.  
  7. function rpc.callScriptedEntity(entityId, functionName, args, callback)
  8.     local handle = rpc.getUniqueHandle()
  9.     local call = {
  10.         handle = handle,
  11.         callback = callback,
  12.         created = os.clock()
  13.     }
  14.  
  15.     table.insert(rpc.activeCalls, call)
  16.     world.callScriptedEntity(entityId, "rpc.recieveScriptCall", functionName, handle, args)
  17. end
  18.  
  19. function rpc.recieveScriptCall(functionName, handle, args)
  20.     local result = _ENV[functionName](unpack(args))
  21.     world.setProperty(handle, result)
  22. end
  23.  
  24. function rpc.getUniqueHandle()
  25.     local handle = ""
  26.     repeat
  27.         local id = math.random(1, 9999999)
  28.         handle = rpc.slug .. id
  29.     until world.getProperty(handle) == nil
  30.    
  31.     return handle
  32. end
  33.  
  34. function rpc.update()
  35.     for i = #rpc.activeCalls, 1, -1 do
  36.         local call = rpc.activeCalls[i]
  37.         local remove = false
  38.  
  39.         local result = world.getProperty(call.handle)
  40.         if result ~= nil then
  41.             call.callback(result)
  42.             remove = true
  43.         end
  44.  
  45.         if os.clock() - call.created > rpc.ttl then
  46.             remove = true
  47.         end
  48.  
  49.         if remove then
  50.             world.setProperty(call.handle, nil)
  51.             table.remove(rpc.activeCalls, i)
  52.         end
  53.     end
  54. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top