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
Note the separators we used to pack - these can be anything you choose, I just chose smily faces and triangles. When you unpack, you just do it the opposite way. Basically you're just packing up the coords of each player separated by a certain char, then packing each player separated by a certain char so you send a single string across the network. When it reaches the other side, you unpack the players, then the coords of the players so you get your tables back.
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
The code is probably not the best and you'll want to make some changes for when a client connects to the server and when a client disconnects.
Last edited by Deif on Tue May 24, 2011 8:22 am, edited 2 times in total.
self.server:send(function()
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)
I heard somewhere that you should never let the player decide its own coordinates? shouldn't the client just request to move then the server does all the moving then sends all the information back to all clients?
Bambo wrote:I heard somewhere that you should never let the player decide its own coordinates? shouldn't the client just request to move then the server does all the moving then sends all the information back to all clients?
I think for now you can forget about preventing cheating. I'd learn the easy way first then do anti-cheat later.
Bambo wrote:I heard somewhere that you should never let the player decide its own coordinates? shouldn't the client just request to move then the server does all the moving then sends all the information back to all clients?
I think for now you can forget about preventing cheating. I'd learn the easy way first then do anti-cheat later.
Ah ok, what do these separators do? do they just cut out anything between two given characters?
connected = {
{10, 10},
{20, 20}
}
function send()
lube.bin:setseperators(string.char(30),string.char(31))
local data = lube.bin:pack( connected )
server:send(data)
end
That is basicly what i'm sending, but for some reason it doesn't allow it?