View Single Post
  #16  
Unread 10-08-2010, 11:59 AM
Digital_Utopia's Avatar
Digital_Utopia Digital_Utopia is offline
The Undying
Interface Author - Click to view interfaces
 
Join Date: Sep 2010
Posts: 207
Quote:
Originally Posted by Kryso
If that class would be singleton (only one instance across whole application), then it would create only one function. However it is still better to use self.Foo = function( self ) so you don't have to remember whether you must call it with "." or ":" and always call every function on instance with ":". Also I'm not saying that you shouldn't create functions with "." operator, I'm talking about creating functions inside of function (which is not always bad.. sometimes it's neccessary, but if you don't have to then don't do it) and using always "self" when that function is going to be member of class.

Code:
Foo = class();
Foo.Constructor = function( self )

end

Foo.Bar = function( self, arg )

end
is exactly the same as
Code:
Foo = class();
function Foo:Constructor = function()

end

function Foo:Bar( arg )

end
and would work same as
Code:
Foo = class();
function Foo:Constructor()
    self.Bar = function( self, arg )

    end
end
or
Code:
Foo = class();
function Foo:Constructor()
    function self:Bar( arg )
        
    end
end
but the last 2 would eat more memory than the first 2 after creating more than 1 instance. The difference is small, but when you put 10000 small things together, they make one big thing so it's good to know how things work (try this http://www.lua.org/pil/16.html#ObjectSec )

Events are different thing. You need to do it like this, because you are changing function on another instance - so function foo:MouseClick( args ) would work too, just "sender" would be named "self" and you would loose "self" reference from parent scope.

Also note that lua has no support for events, so what we call events are just plain functions.
Okay, well that makes sense. Thanks for the explanation
__________________

Lord of the Rings Online
75 Fourohfour | 75 Artemedis | 60 Whiskeytango Foxtrot | 50 Mistah Boombastic | 56 Appetizer | 25 Aggromi
61 Onepointtwentyone Gigawatts


World of Warcraft
90 Downlo 85 Gravetaxi 85 Ümad 85 Artemedis 85 Guthuros
Reply With Quote