Indigo Rose Software

Professional Software Development Tools

 
Results 1 to 14 of 14

Thread: Lua Tables

  1. #1
    Join Date
    Jun 2007
    Location
    Delphi II
    Posts
    1,534

    Lua Tables

    I found a very good reference for tables and thought I'd share it.
    http://www.gammon.com.au/forum/?id=6036

    table.foreach is a very useful function.
    Action Plugins
    AllOn | Box | Class | Code | Cursor | DXML | Error | Frames | GlobalPaths | Group | INIPlus |KeyBind | KeyLock | MathEx | Menu | Name | Project | Resize | StatusBar
    Download

  2. #2
    Join Date
    May 2004
    Location
    Belgium
    Posts
    129
    to the the power of LUA Tables Thnx

  3. #3
    Join Date
    Apr 2007
    Location
    Raalte, OV, Netherlands
    Posts
    3,287
    Nice Thread

    I should write a metatable tutorial one day, not just for OOP coding in Lua,
    but for customizing the indexes/operators/pairs functions etc.

    There's so much you can do with Lua tables, there is no real limit actually.
    Bas Groothedde
    Imagine Programming :: Blog :: Familiar people here

    My AMS Plugins:

  4. #4
    Join Date
    Oct 2009
    Location
    Merton, United Kingdom
    Posts
    684
    Quote Originally Posted by Imagine Programming View Post
    Nice Thread

    I should write a metatable tutorial one day, not just for OOP coding in Lua,
    but for customizing the indexes/operators/pairs functions etc.

    There's so much you can do with Lua tables, there is no real limit actually.
    I'd like to see that, I'm currently working with a script that uses metatables and it's quite fascinating what it can do.

  5. #5
    Join Date
    Jun 2007
    Location
    Delphi II
    Posts
    1,534
    So I'll be the n00b and ask...what's a metatable?
    Action Plugins
    AllOn | Box | Class | Code | Cursor | DXML | Error | Frames | GlobalPaths | Group | INIPlus |KeyBind | KeyLock | MathEx | Menu | Name | Project | Resize | StatusBar
    Download

  6. #6
    Join Date
    Apr 2007
    Location
    Raalte, OV, Netherlands
    Posts
    3,287
    Quote Originally Posted by Centauri Soldier View Post
    So I'll be the n00b and ask...what's a metatable?
    The metatable contains information about an object, for example, a luatable. You can set functions
    and data here that belongs to just the object and can't be accessed without calling set/getmetatable.

    You can set the type, the getter/setter functions (for tables) and even change how operators react
    on certain objects.

    For example, you can write code in Lua that causes 'szString * 3' to repeat that string 3 times.
    Or e.g.
    Code:
    local newString = ("LoL")*100;
    This is a bit complicated stuff, but as Sakuya said, there's so much you can do with it.
    This is the reason why, in my Object plugin, you can't loop through the Object table,
    you can't change anything in it and why you can use hObjectHandle.Properties.Text = "Test"; etc.

    All that is triggered in the metatable
    Bas Groothedde
    Imagine Programming :: Blog :: Familiar people here

    My AMS Plugins:

  7. #7
    Join Date
    Apr 2010
    Posts
    529
    Hey Bas,

    Looking forward to the tutorial... Sounds like metatables could be a chapter in one of Stephen Hawkings books on quantum astrophysics

  8. #8
    Join Date
    Apr 2007
    Location
    Raalte, OV, Netherlands
    Posts
    3,287
    I think astrophysics are a bit more complicated than this haha
    A metatable is just another table attached to a table containing information
    about the table it's attached to
    Bas Groothedde
    Imagine Programming :: Blog :: Familiar people here

    My AMS Plugins:

  9. #9
    Join Date
    Jun 2007
    Location
    Delphi II
    Posts
    1,534
    Quote Originally Posted by Imagine Programming View Post
    ...A metatable is just another table attached to a table containing information
    about the table it's attached to
    Oh, ok. That makes sense now. It seems like I could designate and populate my own metatables as well or is it done automatically?
    Action Plugins
    AllOn | Box | Class | Code | Cursor | DXML | Error | Frames | GlobalPaths | Group | INIPlus |KeyBind | KeyLock | MathEx | Menu | Name | Project | Resize | StatusBar
    Download

  10. #10
    Join Date
    Apr 2007
    Location
    Raalte, OV, Netherlands
    Posts
    3,287
    Quote Originally Posted by Centauri Soldier View Post
    Oh, ok. That makes sense now. It seems like I could designate and populate my own metatables as well or is it done automatically?
    setmetatable(obj,metatable) and metatable = getmetatable(obj)
    Bas Groothedde
    Imagine Programming :: Blog :: Familiar people here

    My AMS Plugins:

  11. #11
    Join Date
    Mar 2007
    Posts
    125

  12. #12
    Join Date
    Apr 2007
    Location
    Raalte, OV, Netherlands
    Posts
    3,287
    Nice link, but that's the very basic information regarding indexes in metatables. I will
    see if I can put up a tutorial based on the system Object is using, including filtering
    allowed keys and values.
    Bas Groothedde
    Imagine Programming :: Blog :: Familiar people here

    My AMS Plugins:

  13. #13
    Join Date
    May 2004
    Location
    Belgium
    Posts
    129
    Quote Originally Posted by Centauri Soldier View Post
    I found a very good reference for tables and thought I'd share it.
    http://www.gammon.com.au/forum/?id=6036

    table.foreach is a very useful function.
    small remark from the LUA 5.1 ref manual:
    Functions table.foreach and table.foreachi are deprecated.
    You can use a for loop with pairs or ipairs instead.
    www.lua.org/manual/5.1/manual.html#7

  14. #14
    Join Date
    Apr 2007
    Location
    Raalte, OV, Netherlands
    Posts
    3,287
    John,

    That's true, however those functions can be customized and you
    can make custom pairs functions. But then again, this can also be
    done in the metatables
    Bas Groothedde
    Imagine Programming :: Blog :: Familiar people here

    My AMS Plugins:

Posting Permissions

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