lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


----- Original Message ----- 
From: "nerurin" <nerurin@hotmail.com>
To: "Multiple recipients of list" <lua-l@tecgraf.puc-rio.br>
Sent: Thursday, August 15, 2002 3:00 PM
Subject: Creating object simulate C++ object in lua


> Hi all,
> 
> I wonder what is the "easiest" way to create a class defination in 
> Lua, that function like C++ class. The intention is, I want to 
> prototype some idea, but I do not prefer to write in C++ right way.
> 
> Thanks in advance!
> 
> 

In 4.0 the easist way to do this is to set up an index tag 
method for your tables that will look for a super object inside 
of a table looking for properties/functions that were not found
in the first table.  The following is the code I use:

function delegate(tab, item)
    local base = rawget(tab, "inherits")
    if type(base) == "table" then
        return base[item]
    else
        return nil
    end
end

settagmethod(tag{}, "index", delegate)

Now, if the table "tab" has an item called "inherits" the tag 
method will check for that item, if found and it is a table, it
will look for "item" in that table.  This continues up the chain
until the item is found or "inherits" isn't found.

When you write your objects you create them as such:

BaseObject = { myval = "A Value" }

InheritedObject = { inherits = BaseObject, newval = "Another Value" }

print(InheritedObject.myval)

This will cuase the value "A Value" to be displayed.  This
functionally works great with functions as well.  This following
example shows how you might define a set of simplistic GUI objects:

GUIObject =
{
    x = 10,
    y = 10,
    Width = 100,
    Height = 100
}

function GUIObject:OnDraw()
    DrawRect(self.x, self.y, self.Width, self.Height)
end

function GUIObject:OnClick()
    -- Do something.
    print("GUIObject clicked!")
end

-- Then...

StaticText = 
{
    inherits = GUIObject,
    Caption = "Static Text"
}

function StaticText:OnDraw()
    DrawText(self.x, self.y, self.Caption)
end

Now, when you call StaticText's OnDraw() method, you'll see the
caption being displayed; however, the "x" and "y" values come from
the inherited GUIObject class.  Presumably the user wishing to
create an instance of a StaticText object would override the "x"
and "y" values to set the correct possition of the object.

If you were to call StaticText's OnClick() method, the result
would be that of the OnClick() defined in the GUIObject base class.

The colon in the function definitions passes a hidden "self"
parameter to the function.  This is akin to the C++ "this" pointer.

There are a few limitations to this setup:

There are no private/protected methods and variables, everything 
can look inside the guts of your object and possibly play with the 
values.

This is possibly not the most efficent way to do inheritance.  I 
don't know of a better method inside of Lua though, I'd be
intrested in hearing about other ways to do this.

I hope this helps you out and that I explained it clearly enough.

Kelmar K. Firesun (IRL: Bryce Simonds)