[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: RE: Having trouble with tables...
- From: Eric Clark <eclark@...>
- Date: Tue, 1 Feb 2011 17:02:54 +0000
Leo,
Thank you for your response. However, there are a few issues. See below...
> -----Original Message-----
> From: lua-l-bounces@lists.lua.org [mailto:lua-l-bounces@lists.lua.org] On
> Behalf Of Leo Razoumov
> Sent: Tuesday, February 01, 2011 7:45 AM
> To: Lua mailing list
> Subject: Re: Having trouble with tables...
>
> On Mon, Jan 31, 2011 at 15:27, Eric Clark <eclark@ara.com> wrote:
> > 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.
> > Eric
> >
>
> Eric,
> it is really worth its money to buy "Programming in Lua, 2nd edition"
> by Roberto Ierusalimschi. The outdated 1-st edition of the book covering
> previous version Lua-5.0 is available online http://www.lua.org/pil/ for free.
I will look into purchasing this book. I have actually been through much of the online book and thought I knew what I was doing, but nothing seems to work in my case.
>
> > 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?
>
> After reading chapter on tables and metatables many things will become
> clear. E.g. you do not need C/C++ code at all to get/set a table's metatable.
Unfortunately, C/C++ code is a requirement. We use Lua in our program to allow Lua scripting of many methods and functions that reside inside of our framework. Basically, we have an inheritance structure that looks like this:
Object
|
Entity
| |
Model PropertySet
This is a C++ inheritance hierarchy. What we do, is we register many different functions with Lua that return pointers to objects of type Model or PropertySet. Once the script calls one of those functions and obtains a pointer to a Model or ProperytSet, I would like to be able to allow them to call C++ methods on those pointers. For example:
myModel = FindModel("My Model Name")
WriteLine( myModel:Name() )
The FindModel function searches for a model by name in our scene graph and returns a pointer to that model. The WriteLine function is registered in our framework to write to the console window within our GUI. And the Name function is a function that returns the name of the Model. In this case, the Name function is actually inherited from the Object class, which means that a pointer to a PropertySet also has a Name function. All of these functions work; however, in order to get it to work, I am having to register a meta table called PropertySet and a meta table called Model, each of which has to have its own Name function registered. This seems to be very redundant considering the Name function is actually provided in a super class. What I would prefer to do is to have a meta table called Entity, and to place the Name function in that meta table. Then when someone attempts to call the Name function on a PropertySet pointer or a Model pointer, Lua will look in the Model meta table and see there is no function called Name. What I understand is that Lua will then look in the __index entry, and if that entry is another table, it will look for the function in the table that __index points to. Am I correct in this understanding? If so, then it seems to me that the best approach is to have three tables: Entity, PropertySet, and Model. And to have the __index entry in Model and PropertySets meta table point to the Entity meta table. Then, when the script does this:
myModel:Name()
The Name function that is called is the one contained in the Entity meta table. I believe that I have the right idea here, but I am having a very difficult time implementing this sort of hierarchy via meta tables and the __index entry. Using Lua is not an option as this stuff is all done inside our C++ framework. I really would like to know what to do in C/C++. I have googled and googled and everything I get is how to do it in Lua. That does not help my situation. Our program is a framework that is used by many other developers to build their own applications and we provide a scripting interface to the framework using Lua. We also provide many built-in functions and meta tables that the developers can use without having to create them in Lua on their own.
I would be more than happy to show the code that I currently have if anyone is interested in helping.
Thank You,
Eric
>
> --file: mt.lua
> mt= {a=1, b=2}
> mt.__index= mt
> tbl= {}
> setmetatable(tbl, mt)
> print(tbl.a, tbl.b)
> --EOF
>
> sh$ lua mt.lua
> 1 2
>
>
> --Leo--