lua-users home
lua-l archive

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


From: "Peter Shook" <pshook@hotmail.com>
> On the plus side, Lua has first class functions, is lexically scoped, and
> dynamically typed, so you don't need to use all that OO stuff like you do
in
> C++ to get around its shortcomings.
>
> You'll get better mileage if you think of Lua as a legible dialect of
scheme
> with infix operators, and a lot fewer parentheses.

But I'd like some structure, otherwise I won't be able to understand what I
code. I've experienced my code getting more and more complicated and less
and less easy to change to new goals. If you want to know, I am programming
an RPG engine. The engine is nearly finished, and is also very basic. All
the functionality and gameplay is coded in Lua (much more Lua code than
C++). I really like being able to structure my classes into a class tree,
like

Object
    |> Actor [partly native class]
    |    |> Decoration
    |    |    |> Torch
    |    |> Character
    |> Controller
    |    |> PlayerController
    |    |> AIController
    |> Action
         |> ActionSequence

If you can give an example of how to get a nice structure using an approach
like you suggested (dialect of scheme with infix operators?), that would be
very helpful!

> >Look in:
> >1 - Function parameters
> >2 - Locally defined variables
> >3 - Current object variables or methods
> >4 - Globals

I noticed this lookup was not what I meant, I meant more like:

1 - Locally defined variables
2 - Function parameters
3 - Variables/methods of current object (repeated until root superclass)
4 - Globals

1, 2 and 4 are already the way they are now in Lua. So I think if somehow
the table of globals was set to the self reference and the superclass of the
root class wat set to the globals table, it should work. Now of course, it
would be real nice if this could be done automatically.

I've thought up another problem though, how would I go about calling a
method in a superclass? Is there any way to tell in which table (class) the
currently executing function is stored? If so, I could access the superclass
variable inside that table. The problem is that this table is not always
equal to the self reference, so it would need it's own variable.


From: "Tuomo Valkonen" <tuomov@modeemi.cs.tut.fi>
> function make_object_scoped(fn)
>     local env=getfenv(fn)
>     function ind(tab, name)
>         local slf, slfval=debug.getlocal(2, 1)
>         if slf=="self" then
>             if slfval[name] then
>                 return slfval[name]
>             end
>         end
>         return env[name]
>     end
>     setfenv(fn, setmetatable({}, {__index=ind, __newindex=env}))
> end

This looks mighty complicated to me at present, so I'll be investigating
this further to try and understand what you're doing.

Thanks to both for your help!

Bjørn Lindeijer