Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- rpc = {}
- rpc.activeCalls = {}
- rpc.slug = "rpc-"
- rpc.ttl = 120
- function rpc.callScriptedEntity(entityId, functionName, args, callback)
- local handle = rpc.getUniqueHandle()
- local call = {
- handle = handle,
- callback = callback,
- created = os.clock()
- }
- table.insert(rpc.activeCalls, call)
- world.callScriptedEntity(entityId, "rpc.recieveScriptCall", functionName, handle, args)
- end
- function rpc.recieveScriptCall(functionName, handle, args)
- local result = _ENV[functionName](unpack(args))
- world.setProperty(handle, result)
- end
- function rpc.getUniqueHandle()
- local handle = ""
- repeat
- local id = math.random(1, 9999999)
- handle = rpc.slug .. id
- until world.getProperty(handle) == nil
- return handle
- end
- function rpc.update()
- for i = #rpc.activeCalls, 1, -1 do
- local call = rpc.activeCalls[i]
- local remove = false
- local result = world.getProperty(call.handle)
- if result ~= nil then
- call.callback(result)
- remove = true
- end
- if os.clock() - call.created > rpc.ttl then
- table.remove()
- remove = true
- end
- if remove then
- world.setProperty(call.handle, nil)
- table.remove(rpc.activeCalls, i)
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement