LUBE (Networking Library)

Showcase your libraries, tools and other projects that help your fellow love users.
Deif
Prole
Posts: 12
Joined: Sun Mar 06, 2011 10:01 pm

Re: LUBE (Networking Library)

Post 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.
Last edited by Deif on Tue May 24, 2011 8:22 am, edited 2 times in total.
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: LUBE (Networking Library)

Post 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)
Deif
Prole
Posts: 12
Joined: Sun Mar 06, 2011 10:01 pm

Re: LUBE (Networking Library)

Post by Deif »

Oh ok, that helps - thanks; learn something everyday. :P
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: LUBE (Networking Library)

Post by BlackBulletIV »

Deif wrote:learn something everyday.
:) So true.
Bambo
Prole
Posts: 41
Joined: Thu Mar 24, 2011 8:23 pm
Location: Warrington, England
Contact:

Re: LUBE (Networking Library)

Post 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?
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Milwaukee, WI
Contact:

Re: LUBE (Networking Library)

Post 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.
Bambo
Prole
Posts: 41
Joined: Thu Mar 24, 2011 8:23 pm
Location: Warrington, England
Contact:

Re: LUBE (Networking Library)

Post 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?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: LUBE (Networking Library)

Post 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
Help us help you: attach a .love.
Bambo
Prole
Posts: 41
Joined: Thu Mar 24, 2011 8:23 pm
Location: Warrington, England
Contact:

Re: LUBE (Networking Library)

Post 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
Bambo
Prole
Posts: 41
Joined: Thu Mar 24, 2011 8:23 pm
Location: Warrington, England
Contact:

Re: LUBE (Networking Library)

Post 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?
Post Reply

Who is online

Users browsing this forum: Bing [Bot], RagingDave and 3 guests