lua-users home
lua-l archive

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


You've got it right. Just make sure that you are putting the
metamethods in the metatable of Model or PropertySet, not just in the
Model or PropertySet table.

If I were to set up such an inheritance hierarchy as you describe in
pure Lua, I would do something like this:

--begin code
Entity = {}
Entity.__index = Entity

PropertySet = {}
setmetatable(PropertySet, Entity)

Model = {}
setmetatable(Model, Entity)
--end code

I'm very rusty with the Lua C API, so pardon any serious abuses. This
is a rough translation of the above:

/* Create Entity */
lua_newtable(L)
lua_setglobal(L, "Entity")

lua_getglobal(L, "Entity")
lua_setfield(L, -1, "__index")

/* Create Model */
lua_newtable(L)
lua_setglobal(L, "Model")

lua_getglobal(L, "Model")
lua_getglobal(L, "Entity")
lua_setmetatable(L, -2)

Does that make things clearer?

On 12/20/10, Mateusz Czaplinski <czapkofan@gmail.com> wrote:
> On Sun, Dec 19, 2010 at 1:37 AM, Eric Clark <eclark@ara.com> wrote:
>> Hello All,
>>
>> I am a pretty new user to developing with Lua and I am having some issues
>> trying to set up an inheritance hierarchy with Lua. I have created the
>> tables using some code that I found on the internet, but I do not really
>> understand the code. It works, but my understanding says it shouldn’t.
>> Basically, I have 3 tables: Entity, Model, and PropertySet. The Model and
>> PropertySet tables are supposed to inherit the Entity table. From what I
>> understand, if I set the __index metamethod in Model’s table to the Entity
>> table, then if the Model table is invoked with an invalid key, it will
>> look
>> in the Entity table for the same key as long as that table is set in the
>> Model’s __index field. Is this correct? If so, could anyone give me some
>> sample C/C++ code to show how you actually set the __index field to refer
>> to
>> a table?
>
> Hi!
> I'm sorry, I won't be able to give you code, as I don't have enough
> time, but the following links from "Programming in Lua" should help
> you get the answers and confirmations (and there are snippets of code
> available there):
> - chapter "The __index Metamethod" -- http://www.lua.org/pil/13.4.1.html
> - chapter on C API, especially:
>   - "Table Manipulation" -- http://www.lua.org/pil/25.1.html
>   - "Metatables" (should be valid also for regular tables, not only
> for "userdata") -- http://www.lua.org/pil/28.2.html
>
> Hope that helps.
> Greetings
> Mateusz Czapliński
>
>

-- 
Sent from my mobile device