Jump to content

Lua OOP


Recommended Posts

Example usage ^^

  
class 'CWorldText' 
  
function CWorldText:CWorldText( sText ) 
    -- TODO 
    self.sText  = sText 
    self.nR     = 255 
    self.nG     = 255 
    self.nB     = 255 
    self.nA     = 255 
end 
  
function CWorldText:Color( nR, nG, nB, nA ) 
    if type( nR ) == 'number' and type( nG ) == 'number' and type( nB ) == 'number' then 
        -- TODO 
        self.nR     = nR 
        self.nG     = nG 
        self.nB     = nB 
        self.nA     = type( nA ) == 'number' and nA or 255 
    end 
    return self.nR, self.nG, self.nB, self.nA 
end 
  

Text = CWorldText 'Some text' 
Text:Color( 255, 0, 0, 175 ) 

Link to comment
http://www.lua.org/pil/16.html This is where I learnt from to create my bank resource.

wow so useful :):)

EDIT : I was reading that link, when I got a doubt :

function Account.withdraw (self, v) 
      self.balance = self.balance - v 
    end 

"self" is like the word "this" on C#?

Second question :

function Account:withdraw (v) 

This is like a inheritance property?

Example:

function Account:withdraw ( v ) 
    if ( v ) then 
        self.balance = self.balance - v; -- self is 'Account' in function name, 'Account' should be a table of account data ( like account pointer ) 'cause you're calling index 'balance' on self 
    end 
end 
  

Link to comment
http://www.lua.org/pil/16.html This is where I learnt from to create my bank resource.

wow so useful :):)

EDIT : I was reading that link, when I got a doubt :

function Account.withdraw (self, v) 
      self.balance = self.balance - v 
    end 

"self" is like the word "this" on C#?

Second question :

function Account:withdraw (v) 

This is like a inheritance property?

function Account:withdraw( v ) 
-- is almost the same as: 
function Account.withdraw( acountObject, v ) 

The difference is that when you call the later one, you have to pass your object to the call, lets say you create an account object:

account = Account: new( ); -- default balance of 0 
print( account.balance ); -- should print 0 

Once you have an object account, you can do either:

Account.withdraw( account, 100 ); -- as you can see, you have to pass object argument 
-- or simply 
account: withdraw( 100 ); -- this will pass account object to Account.withdraw's first parameter for you 

Here is a simple color class which I just wrote now for you:

Color = { } -- classes are tables 
Color.__index = Color; 
  
function Color: new( r, b, b, a ) 
    local color = { 
        r = r and r or 255, -- set default value to 255 if r is not passed in the call 
        g = g and g or 255, 
        b = b and b or 255, 
        a = a and a or 255, 
        mtaColor = tocolor( r, g, b, a ); 
    } 
    setmetatable( self, color ); 
    return color; 
end 
  
function Color: set( r, g, b, a ) 
    -- first param is the class object (not specified in the params list) = self; same as "this" in C# 
    self.r = r; 
    self.g = g; 
    self.b = b; 
    self.a = a and a or self.a; -- make "alpha" an optional param 
    -- it's same as:   if a then self.a = a end 
    self.mtaCol = tocolor( self.r, self.g, self.b, self.a ); -- update mtaCol as well 
end 
  

Here is an example use of this class:

 myColor = Color: new( 255, 255, 0 ); -- remember alpha is set to 255 by default 
dxDrawRectangle( 10, 300, 10, 10, tocolor( myColor.r, myColor.g, myColor.b ) ); 
  
myColor: set( 200, 200, 200 ); 
dxDrawRectangle( 10, 350, 10, 10, myColor.mtaCol ); 
  

  
-- here is the same function like above but you have to pass object to the call: 
function Color.set( col, r, g, b, a ) 
    col.r = r; 
    col.g = g; 
    col.b = b; 
    col.a = a and a or col.a; 
    col.mtaCol = tocolor( r, g, b, col.a ); 
end 
  
--[[ EXAMPLE: 
Color.set( colorObject, 100, 150, 0 ); 
or simply:  
colorObject.set( colorObject, 100, 150, 0 ); 
BOTH are valid but I never use it any more.. I keep my code cleaner 
]] 

I just hope it helped you a little. I know it seems confusing at first, but if you keep using it and try to learn it, you will get used to it. I've created tens of classes (if not hundreds) already and I can make them in different ways and they will still work because I understand the mechanism behind it.

Don't just read the information from the link I gave you. Try to write the code and you will understand it quicker as you go deeper into classes.

Link to comment

Man its not confusing, a very good explanation. Thanks you very much 50p. :) :) I asked because, I started to study with C#, and well I got how objects works, the problem was the syntax of objects at Lua, but I think that I understood it (Is really similar to C# xD).

Anyway, if I have any doubt, I will ask.

Link to comment

Just some fixes of your code, 50p:

Color = { } 
Color.instances = { } 
Color.__index = Color; 
  
function Color:new ( r, g, b, a ) 
    Color.instances[#Color.instances+1] = setmetatable( 
        { 
            r = r or 255, 
            g = g or 255, 
            b = b or 255, 
            a = a or 255, 
            hexColor = tocolor ( r, g, b, a ); 
        },  
        self 
    ); 
    return Color.instances[#Color.instances]; 
end 
  
function Color:set ( r, g, b, a ) 
    self.r = r; 
    self.g = g; 
    self.b = b; 
     
    if ( a ) then 
        self.a = a; 
    end 
     
    self.hexColor = tocolor ( self.r, self.g, self.b, self.a ); 
    return true; 
end 

Link to comment
In my code, Edikosh998?

setmetatable sets metatable of self and return the table ( in this case, the table that have the colors ).

This table is like an instance.

There is no need for that anyway. It's good to have some sort of table of "instances" if you need to draw the objects in onClientRender, eg. to draw text, shapes, etc.

Link to comment
In my code, Edikosh998?

setmetatable sets metatable of self and return the table ( in this case, the table that have the colors ).

This table is like an instance.

There is no need for that anyway. It's good to have some sort of table of "instances" if you need to draw the objects in onClientRender, eg. to draw text, shapes, etc.

Anyway, I didn't change only that thing in your code, check both codes ;)

Link to comment
Now I've got a new doubt.

I watched a resource (textLib) as example to understand OOP :

  
dxText = {} 
dxText_mt = { __index = dxText } 
  

Which is the difference between that code and this code :

  
  
dxText = {} 
dxText.__index = dxText 
  
  

That's a separate metatable, check the rest of the code to find its uses.

Link to comment

So, instead of doing this :

  
  
  
function dxText:create( text, x, y, relative ) 
    assert(not self.fX, "attempt to call method 'create' (a nil value)") 
    if ( type(text) ~= "string" ) or ( not tonumber(x) ) or ( not tonumber(y) ) then 
        outputDebugString ( "dxText:create - Bad argument", 0, 112, 112, 112 ) 
        return false 
    end 
    local new = {} 
    setmetatable( new, self) 
        self.__index = self -- Here 
  
    --Add default settings 
    for i,v in pairs(defaults) do 
        new[i] = v 
    end 
    idAssign = idAssign + 1 
    new.id = idPrefix..idAssign 
    new.strText = text or new.strText 
    new.fX = x or new.fX 
    new.fY = y or new.fY 
    if type(relative) == "boolean" then 
        new.bRelativePosition = relative 
    end 
    visibleText[new] = true 
    return new 
end 
  
  

He uses a separate metatable, right?

EDIT : New mini-question.

I was looking for your script ( bank system) :

function Account:open( username, balance ) 
    --local acc = executeSQLSelect( bankSQLInfo.tab, bankSQLInfo.username..", ".. bankSQLInfo.balance, bankSQLInfo.username.." = \"".. username.."\"", 1 ) 
    local acc = getAccount( username ); 
    if acc then  
        return Account:new( username, tonumber( balance ) );-- Here its the same to put  : 
--  return self; 
    end 
    return false 
end 
  

Sorry if I'm bothering with these questions.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...