Jump to content

  • Log in with Facebook Log in with Twitter Log In with Google Log In with Steam Sign In
  • Create Account
Photo

Lua question


  • Please log in to reply
3 replies to this topic

#1
OFFLINE   NoHero

NoHero

    Newbie

  • Members
  • Pip
  • 5 posts
5
A step in the right direction

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?



#2
OFFLINE   Zelly

Zelly

    Regular Member

  • Coders
  • PipPipPip
  • 141 posts
73
Getting Better

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.



#3
OFFLINE   Sol

Sol

    Regular Member

  • Coders
  • PipPipPip
  • 113 posts
28
On a distinguished road
  • LocationPoland

'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Я, 11 March 2014 - 09:25 AM.


#4
OFFLINE   NoHero

NoHero

    Newbie

  • Members
  • Pip
  • 5 posts
5
A step in the right direction

Ah i see thanks, confused me because when i looked at OOP in lua examples the class was constructed differently. I see now that there are a few ways of doing it.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users