This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
kylergs's profile picture

[Lua] Getting the table a function is called from

Started by kylergs, 28 November 2012 - 05:01 AM
kylergs #1
Posted 28 November 2012 - 06:01 AM
So, I am practicing creating an API and I was wondering how you get the name of the table that a function is called from.
The way it works is supposed to be similar to the fs API so you can do:

ThingA = newMatrix()

where new matrix returns a table containing a bunch of functions

the you could do

ThingA:newRow()

where newRow is a function in the table "ThingA"

But, newRow requires the name of "ThingA" to be able to insert a new row into it.

How do get the name of the table "ThingA" without having to type

ThingA:newRow(ThingA)

?
Lyqyd #2
Posted 28 November 2012 - 06:07 AM
The colon notation like you have there passes the table immediately before the colon (in your example, ThingA) as the first parameter to the function. This is usually caught in a variable named self. So:


function doStuff(self, entry)
  if not self.values then self.values = {}
  table.insert(self.values, entry)
end
kylergs #3
Posted 28 November 2012 - 06:10 AM
Awesome thanks