Advertisement
Guest User

Untitled

a guest
Nov 4th, 2014
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.35 KB | None | 0 0
  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.             table.remove()
  47.             remove = true
  48.         end
  49.  
  50.         if remove then
  51.             world.setProperty(call.handle, nil)
  52.             table.remove(rpc.activeCalls, i)
  53.         end
  54.     end
  55. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement