View Full Version : LUA: How to get a table name
Marquis
July 24th, 2007, 03:14 AM
Hi folks,
again, I have a question regarding LUA. I am facing the problem that I need to know the name of a table inside a function.
My easy example:
myTable={}
myTable.hello=function()
print('hello, my name is '..tablename)
end
Calling
myTable.hello() should print out "hello, my name is myTable"
I understand that you can print the pointer of a table by using
print(myTable)
And I think I just need the other way around, converting a pointer to the name. How to do that?
BR/Marc
Rob H
July 24th, 2007, 03:25 AM
Tables (and other variables) don't actually have names in Lua.
Do you mean that you want to be able to access the table itself from within the function? If so then you'd do it like this
mytable.hello = function(self)
-- do something with self
end
mytable:hello()
Note the use of the colon syntax here - this is roughly equivalent to
mytable.hello(mytable)
You can also declare the function like this
function mytable:hello()
-- this function has an implied self parameter
end
Marquis
July 24th, 2007, 03:31 AM
Rob,
thanks for your answer, but my approach is really to get the name like it is displayed in the variable inspector. I don't want to have access to the contents of the table (for this special scenario).
I just want to identify tables by name instead of the pointer (or number or reference). I believe this is possible, because you see names in the variable inspector and also by using
table.print(myTable) you see names for sub-tables and other variables.
What I am also wondering about is: Can one sub-table access functions from it's parent table without knowing it's reference? So is there some kind of "parent" object?
BR/Marc
Rob H
July 24th, 2007, 03:57 AM
In the case of the variable inspector it simply iterates over the global table _G
Consider this situation
a = {}
b = a
c = b
What's the name of the table? a, b or c? And if you then do
a = nil
What is it now?
In the case of subtables - all you see there is the key that is used to store them in the parent table.
If you can explain what you're trying to achieve I may be able to help some more.
Marquis
July 24th, 2007, 04:30 AM
Rob,
thanks for your help and your lightspeed answers. Awesome!
Okay, I try to build a table structure that is representing my house. So the first table is "myHome" and this one has many rooms, e.g. myHome.Bathroom. And each room can have several lights or blinds or devices that can be controlled.
So to control the light from the mirror in the bathroom I just type:
myHome.Bathroom.Mirror:on()
To achieve this I have built some objects, so I can add rooms to the house by using
myHouse.Livingroom=rooms:new() and adding a new light by typing
myHouse.Livingroom.mainlight=lights:new()
However, the names of the rooms and lights are the keys of the parent table. This is allowing me easy access to the functions of each object as you can see above.
Why do I need the name? Because for my OSD it should print "Livingroom Mainlight activated" instead of "07588D80 activated" *gg* And I don't want to use seperate keys for the names like
mainlight.name='mainlight'
mainlight.parentname='Livingroom'
So I hoped LUA might just find out what "07588D80" would be in plain text.
And to get access to the parent table (Livingroom) is needed to get the name from there as well. And I am sure there are other useful things you can do, when you are able to drill up the tree - but that's another story *g*
I hope this helps you to understand what I am trying to do.
BR/Marc
mhund
July 24th, 2007, 05:26 AM
I am not shure if my imagination is right but I thought that variable names in LUA are only a question of uncompiled LUA text. When the LUA code comes to the LUA engine, the code will be precompiled and variable names are replaced by pointers. The "real" names of variables are only visible in the variable tree _G, isn't it?
Rob H
July 24th, 2007, 06:53 AM
mhund: That's correct.
Marquis: How about a slight restructuring, so that you would have a method NewRoom() in the house class then you could do something like
myHouse:NewRoom('LivingRoom')
NewRoom might look like this
function House:NewRoom(name)
local temp = rooms:New()
temp.name = name
temp.parent = self
self[name] = temp
return temp
end
The room class would similarly have a method NewLight. How does that sound?
Marquis
July 25th, 2007, 04:10 AM
How does that sound?
Sounds great. I will try to implement like this.
thanks for your help
Marc
Powered by vBulletin® Version 4.1.8 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.