DarkLink Posted June 1, 2012 Share Posted June 1, 2012 I readed somewhere that is now possible to code using OOP ? If so, why dont find any advices over wikis pages Thanks in advance. Link to comment
Anderl Posted June 1, 2012 Share Posted June 1, 2012 (edited) Lua is function-oriented language, not object-oriented language. But you can use metatables and create your class system or use the class system that Kenix gave us in this topic: http://pastebin.com/vRpVbcDy It's a "simulation" of C++ class system. // If something is wrong, correct me please. Edited June 1, 2012 by Guest Link to comment
Kenix Posted June 1, 2012 Share Posted June 1, 2012 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
DarkLink Posted June 1, 2012 Author Share Posted June 1, 2012 I though it was native in LUA, but is nice anyway, thanks Link to comment
CapY Posted June 1, 2012 Share Posted June 1, 2012 Use the OpenFrame class by Orange viewtopic.php?f=108&t=27970&p=352998&hilit=OpenFrame#p352998 Link to comment
50p Posted June 1, 2012 Share Posted June 1, 2012 http://www.lua.org/pil/16.html This is where I learnt from to create my bank resource. Link to comment
Edikosh998 Posted June 2, 2012 Share Posted June 2, 2012 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? Link to comment
DarkLink Posted June 2, 2012 Author Share Posted June 2, 2012 Thanks for ur replys, i got it Link to comment
Kenix Posted June 2, 2012 Share Posted June 2, 2012 DarkLink You can read this too http://lua-users.org/wiki/ObjectOrientedProgramming Link to comment
Anderl Posted June 2, 2012 Share Posted June 2, 2012 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
50p Posted June 3, 2012 Share Posted June 3, 2012 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
Edikosh998 Posted June 3, 2012 Share Posted June 3, 2012 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# ). Anyway, if I have any doubt, I will ask. Link to comment
Anderl Posted June 3, 2012 Share Posted June 3, 2012 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
Alpha Posted June 3, 2012 Share Posted June 3, 2012 There is no need for the Color.Instances table. Link to comment
Anderl Posted June 3, 2012 Share Posted June 3, 2012 There is no need for the Color.Instances table. And? lol Link to comment
Edikosh998 Posted June 3, 2012 Share Posted June 3, 2012 I don't get why did you put a new table, is like a sub-class? Link to comment
Anderl Posted June 3, 2012 Share Posted June 3, 2012 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. Link to comment
50p Posted June 3, 2012 Share Posted June 3, 2012 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
Anderl Posted June 3, 2012 Share Posted June 3, 2012 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
Edikosh998 Posted June 3, 2012 Share Posted June 3, 2012 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 Link to comment
50p Posted June 3, 2012 Share Posted June 3, 2012 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
Edikosh998 Posted June 3, 2012 Share Posted June 3, 2012 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
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now