Page 1 of 1

LUBE help! Class Commons [subclass?]

Posted: Tue Dec 24, 2013 2:46 am
by Maxwell
So I'm having a problem using LUBE. I read the article about how to use LUBE, and I understood that it uses a Class Commons library, and I picked the middleclass.lua library and along with middleclass-commons.lua

However it seems I am having a problem with syntax possibly?

-Code from the LUBE documentation guide-

Code: Select all

protocolClient = subclass(lube.Client)
protocolClient._implemented = true
-- define protocolClient
client = protocolClient()
The LUBE guide shows how to make an instance/object of the Client class. It doesn't explain much on what it's doing. I don't understand what the implemented variable is? Also not sure how protocolClient went from being a variable to a function seeing the last line showing protocolClient()
An explanation of the code might be helpful :)!

However when writing this using the Class Commons syntax since the LUBE guide stated it was just using a generic syntax. I don't understand why they're making a subclass inside the lube.Client class? I thought they're suppose to be making an object from it... Anyways my code that I made using the Class Commons guide after installing middleclass was this.

-line of code that gave me error-

Code: Select all

protocolClient = common.instance(lube.Client,'protocolClient')
LOVE responded me with
Can't use a generic client object directly, please provide an implementation
-all of the code-

Code: Select all

protocolClient = common.instance(lube.Client,'protocolClient')
protocolClient._implemented = true
-- define protocolClient
client = protocolClient()
Anyways I hope I gave you guys enough information to help me figure out what the problem is. Thank you :)!

Re: LUBE help! Class Commons [subclass?]

Posted: Tue Dec 24, 2013 5:06 am
by Davidobot
You need to use either lube.udpClient or lube.tcpClient

[]

Posted: Tue Dec 24, 2013 7:57 am
by bekey
-snip-

Re: LUBE help! Class Commons [subclass?]

Posted: Tue Dec 24, 2013 2:43 pm
by bartbes
Once again, because apparently nobody can read, that bit of code is a definition.
Furthermore, your problems stems from both trying to use the class commons api directly (almost never what you want), and confusing subclassing with instancing, indeed you can't make an instance of lube.Client, you can only subclass it. (And you don't want to subclass it either! Use one of the premade subclasses.)

Re: LUBE help! Class Commons [subclass?]

Posted: Thu Dec 26, 2013 12:56 am
by Maxwell
Thanks bartbes and everyone else!

I have another problem now though. I don't know how to set callback functions. In previous versions of LUBE people used server:setCallback function to se up their callback functions..

Code: Select all

server:setCallback(onReceive, onConnect, onDisconnect)
But now that function doesn't exist and on the LUBE 1.0 Documentation it says
You can set your callback functions in the callbacks table, there are 3 callbacks:
and I don't know what a callback table is :/ and Google didn't really help so I'm back here asking you :D thanks!

Re: LUBE help! Class Commons [subclass?]

Posted: Thu Dec 26, 2013 2:15 am
by Roland_Yonaba
I never used LUBE, I am pretty new to networking, but this sounds very explicit, to me.
Here some code written from scratch. I inferred it from the source.

Code: Select all

local function onConnect(id)
  print('Client '..id..' is connected!')
end

local function onReceive(data, id)
  print('Received data from Client '..id..'!')
end

local function onDisconnect(id)
  print('Client '..id..' is connected!')
end

-- Assuming the server is defined
server.callbacks.connect = onConnect
server.callbacks.disconnect = onDisconnect
server.callbacks.recv = onReceive
Hope this helps.

Re: LUBE help! Class Commons [subclass?]

Posted: Thu Dec 26, 2013 3:40 am
by Maxwell
@Roland_Yonaba, it is not working :(
Here is my code.

Client

Code: Select all

-- CLIENT --
require ("middleclass")
require ("middleclass-commons")
require ("LUBE")

menu_screen = true
server_connect = false
response = ""

function love.load()

end

function love.update(dt)
	if server_connect == true then
	
	end
end

function love.draw()
	if menu_screen == true then
	love.graphics.print("Left Click to Join Server!", 100, 100)
	end
	love.graphics.print(response, 200, 200)
end

function love.mousepressed(x, y, button)
	if button == "l" then
	menu_screen = false
	server_connect = true
	Connect_To()
	end
end

function Connect_To()
client = lube.udpClient()
success, err = client:connect('I put my external IP here', 'I put my open port address here')
	if success == true then
	response = "CONNECTED"
	elseif success == false then
	response = "FAIL"
	else
	response = "IDK"
	end
end
Server

Code: Select all

--SERVER--
require ("middleclass")
require ("middleclass-commons")
require ("LUBE")

function love.load()
user_connect = ""
server = lube.udpServer()
server:listen('I put my open port here')
server.callbacks.connect = onConnect
end

function love.draw()
love.graphics.print(user_connect, 200, 200)
end

function love.update(dt)
server:update(0.01)
end

function onConnect(id)
user_connect = "USER CONNECTED"
end