Results 1 to 9 of 9

Thread: LUA: How to get a table name

  1. #1
    Join Date
    Nov 2006
    Posts
    116

    Default LUA: How to get a table name

    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:

    Code:
    myTable={}
    myTable.hello=function()
      print('hello, my name is '..tablename)
    end
    Calling
    Code:
    myTable.hello()
    should print out "hello, my name is myTable"

    I understand that you can print the pointer of a table by using
    Code:
    print(myTable)
    And I think I just need the other way around, converting a pointer to the name. How to do that?

    BR/Marc

  2. #2
    Join Date
    May 2004
    Location
    Cardigan, UK
    Posts
    9,278

    Default

    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

    Code:
    mytable.hello = function(self)
       --  do something with self
    end
    
    mytable:hello()
    Note the use of the colon syntax here - this is roughly equivalent to

    Code:
    mytable.hello(mytable)
    You can also declare the function like this
    Code:
    function mytable:hello()
      -- this function has an implied self parameter
    end
    --Rob

  3. #3
    Join Date
    Nov 2006
    Posts
    116

    Default

    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
    Code:
    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
    Last edited by Marquis; July 24th, 2007 at 05:44 AM.

  4. #4
    Join Date
    May 2004
    Location
    Cardigan, UK
    Posts
    9,278

    Default

    In the case of the variable inspector it simply iterates over the global table _G

    Consider this situation

    Code:
    a = {}
    b = a
    c = b
    What's the name of the table? a, b or c? And if you then do
    Code:
    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.
    --Rob

  5. #5
    Join Date
    Nov 2006
    Posts
    116

    Default

    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:
    Code:
    myHome.Bathroom.Mirror:on()
    To achieve this I have built some objects, so I can add rooms to the house by using
    Code:
    myHouse.Livingroom=rooms:new()
    and adding a new light by typing
    Code:
    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
    Code:
    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

  6. #6
    Join Date
    May 2004
    Location
    Darmstadt Germany
    Posts
    358

    Default

    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?

  7. #7
    Join Date
    May 2004
    Location
    Cardigan, UK
    Posts
    9,278

    Default

    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
    Code:
    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?
    --Rob

  8. #8
    Join Date
    Nov 2006
    Posts
    116

    Default

    Quote Originally Posted by Rob H View Post
    How does that sound?
    Sounds great. I will try to implement like this.

    thanks for your help
    Marc

  9. #9
    Join Date
    Dec 2014
    Posts
    1

    Default

    you can print the pointer of a table by using metatables and metamethods like
    Code:
    myTable = setmetatable( {} , {__tostring = function() return "myTable" end} )

    then:
    Code:
     myTable.hello=function(self)
      print('hello, my name is '..tostring(self))
    end
    and
    Code:
    myTable:hello()
    should return "hello, my table is myTable"
    Last edited by Morphix; December 13th, 2014 at 06:54 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •