Jump to content

Lua question


Recommended Posts

I was looking at sol's stats logger lua script and couldn't understand one thing, i hope it can be explained by someone. In the extra lib dblib.lua the table containing the functions, each function uses a variable called 'c' what is this?

 

i.e:

DB:Connect("dbname", "dbuser", "dbpassword", "dbhost", 3306)

	['Connect'] = function ( c, DBName, DBUser, DBPass, DBAddr, Port )
		require "luasql.mysql"
		c.env = assert( luasql.mysql() )
		c.con = assert(c.env:connect(DBName, DBUser, DBPass, DBAddr, Port))
	end,

i hope sol doesn't mind me posting snippets

 

what is c doing? some sort of namespace?

Link to comment
Share on other sites

c would be the DB if I am correct.

 

(not sure if this the correct format for rewriting that piece of code, but it might help you understand it)

if you do DB:Connect("dbname", "dbuser", "dbpassword", "dbhost", 3306)
then you are really doing DB.Connect(DB,"dbname", "dbuser", "dbpassword", "dbhost", 3306)

function DB.Connect(self,DBName, DBUser, DBPass, DBAddr, Port )
        -- so self is actually the DB table
	require "luasql.mysql"
	self.env = assert( luasql.mysql() )
	self.con = assert(self.env:connect(DBName, DBUser, DBPass, DBAddr, Port))
end

Disclaimer: I do not fully understand this part of lua yet, so I may have made a mistake.

Link to comment
Share on other sites

'c' argument is something like a class or struct pointer like in 'C'

LUA doesn't have classes but it's something like it, also you can implement prototype of any variable to this kind of structure to keep unique variable name and get access from that self pointer.

Here is the explanation: http://www.lua.org/pil/16.1.html

Edited by solaЯ
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...