Code: Select all
function Server:update(dt)
self.server:send(function() -- the following should really be in a separate function but I put it here for easy reading (taken from BlackBullet below)
local data = {}
for n,p in pairs(self.player) do
lube.bin:setseperators(string.char(1),string.char(2))
data[n] = lube.bin:pack(p:getdata()) -- p:getdata() is where you return your x and y coords as a table
end
lube.bin:setseperators(string.char(30),string.char(31))
local datatable = lube.bin:pack(data)
return datatable
end)
end
Code: Select all
function Client.receive(data)
lube.bin:setseperators(string.char(30),string.char(31))
local datatable = lube.bin:unpack(data)
local playertable = {}
lube.bin:setseperators(string.char(1),string.char(2))
for n,p in pairs(datatable) do
playertable[n] = lube.bin:unpack(p)
if state.player[n] == nil then -- if player doesn't exist in the client table, then we need to make one
state.player[n] = Player.create()
end
if n ~= state.index then -- otherwise we just update the coords
state.player[n]:setdata(playertable[n]) -- setdata() function the exact opposite of getdata() we saw earlier
end
end
end