Page 26 of 34
Re: LUBE (Networking Library)
Posted: Mon May 23, 2011 10:41 pm
by Deif
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
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.
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
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.
Re: LUBE (Networking Library)
Posted: Tue May 24, 2011 4:36 am
by BlackBulletIV
Deif wrote:Code: Select all
-- the following should be in a function but I put it here for easy reading
In-line functions should help:
Code: Select all
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)
Re: LUBE (Networking Library)
Posted: Tue May 24, 2011 8:20 am
by Deif
Oh ok, that helps - thanks; learn something everyday.
Re: LUBE (Networking Library)
Posted: Tue May 24, 2011 9:55 am
by BlackBulletIV
Deif wrote:learn something everyday.
So true.
Re: LUBE (Networking Library)
Posted: Tue May 24, 2011 3:42 pm
by Bambo
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?
Re: LUBE (Networking Library)
Posted: Tue May 24, 2011 4:45 pm
by TechnoCat
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.
Re: LUBE (Networking Library)
Posted: Tue May 24, 2011 4:52 pm
by Bambo
TechnoCat wrote: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?
Re: LUBE (Networking Library)
Posted: Tue May 24, 2011 5:47 pm
by Robin
The separators tell the other side how to split the data. Like punctuation or spaces in human languages.
I'm not sure why BlackBullet called setseperators inside a loop though.
Also: 3100
Re: LUBE (Networking Library)
Posted: Tue May 24, 2011 5:50 pm
by Bambo
Robin wrote:The separators tell the other side how to split the data. Like punctuation or spaces in human languages.
I'm not sure why BlackBullet called setseperators inside a loop though.
Also: 3100
oooh, that makes sense
Re: LUBE (Networking Library)
Posted: Tue May 24, 2011 6:54 pm
by Bambo
ah i think i've got this now, im just having trouble sending a big table back with all the X,Y coords of people.
Code: Select all
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?