tags:

views:

240

answers:

2

C# has this and VB has ME. What is the Lua equivalent?

I am trying to reference the parent of the script class in Roblox.

+4  A: 

From the Lua documentation section 2.5.9, the self reference is usually self:

The colon syntax is used for defining methods, that is, functions that have an implicit extra parameter self. Thus, the statement

function t.a.b.c:f (params) body end

is syntactic sugar for

t.a.b.c.f = function (self, params) body end
Greg Hewgill
+4  A: 

As Greg pointed out already, the name you are looking for is self.

However, be aware that Lua is not an OOP language any more than it is a purely procedural or functional language. It simply provides all the low level mechanisms to implement an OOP design. One of the design principles has been expressed as to "provide mechanism, not policy". Because of that, there is no way to guarantee that the environment you are running in even uses inheritance, or that you could find a parent for any given object.

It would be a good idea to review the sections of the Lua manual, Programming in Lua, and the Wiki that relate to OOP features:

  • Lua Manual, especially sections 2.5.8, 2.5.9 and 2.8.
  • PiL Chapter 16, linked to the online copy of the first edition, which was written at the time of Lua 5.0. Read the online copy, but be aware that the current version of Lua is different enough that buying the 2nd edition is highly recommended.)
  • Lua Wiki on OOP, especially the tutorial and the article on simple classes.
RBerteig