Skip to main content
gamedev.net
🔒 Locked

Object-Oriented Lua: self

Started by lzr Jul 31, 2006 at 3:43 PM 6 replies 1.6k views
Original Post
lzr
lzr
In my game, I'm trying to do an object-oriented take to lua. Each game object is an object in Lua. Anywho, I'm getting a bit frustrated typing in "self" everytime I want to access a class variable. Is there anyway to set the scope to a table in either Lua or the C interface? As it is, my code is looking increasingly ugly, and it just doesn't feel quite right...
------------------------My Site: http://lzr.cc
silencer3
silencer3
don't know Lua but if I would have to use it I would write simple preprocessor that would check what members are in class add add "self" to where they appear inside class methods. (maybe there's something like this on the net?)
RichardoX
RichardoX
Lua is not an object oriented language.

The OO stuff you're seeing are array based "hacks" which make the language appear as an object oriented language. There is no way to get rid of writing 'self' every time. Just get over it. Or use another language, which is really object oriented, such as Python.

-Riku
sandelz
sandelz
Have you thought that writing "self" everytime underlines the meaning of it?

If some other person is looking into your code he/she knows in instant that ie. variable that has "self" as prefix is called within the object and it is not a variable that came from the out-of-class scope.

I for example write "this" in Java and C# every time just to make the point I just explained.

And as some poster above sayed, that LUA is not "natively" OO-language. It has so bending syntax that it can be used as one though and that way enabled many OO features. I wouldn't call it a hack though, because it is a feature that comes from the enhanced syntax.
--- sandelz
Kylotan
Kylotan
Quote:
Original post by RichardoX
There is no way to get rid of writing 'self' every time. Just get over it. Or use another language, which is really object oriented, such as Python.


You have to write 'self' each time in Python too!

kordova
kordova
Quote:
Original post by Kylotan
Quote:
Original post by RichardoX
There is no way to get rid of writing 'self' every time. Just get over it. Or use another language, which is really object oriented, such as Python.


You have to write 'self' each time in Python too!
My exact response.

robinei
robinei
I hate it when people bring the non-nativity of Lua's OO up as something negative. To me it's really a testament to the power of it's core features that OO can be implemented so easily, and in such a clean manner.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.