+ Reply to Thread
Results 1 to 10 of 10

  Click here to go to the first Rift Team post in this thread.   Thread: Slash command table structure

  1. #1
    RIFT Community Ambassador the_real_seebs's Avatar
    Join Date
    Jan 2011
    Posts
    16,859

    Default Slash command table structure

    Okay, silly question.

    I register "foo". I then insert into its table { function, addonname, "/foo" }.

    Why is the name "foo" here twice? Can I insert additional slash commands into the same table obtained from command.slash.register? Do those two names have to be the same?
    You can play WoW in any MMO. You don't have to play WoW in RIFT. Oh, and no, RIFT is not a WoW clone. Not having fun any more? Learn to play, noob! I don't speak for Riftui, but I moderate stuff there. Just came back? Welcome back! Here's what's changed. (Updated for 2.5!)

  2.   Click here to go to the next Rift Team post in this thread.   #2
    Rift Team
    Join Date
    Oct 2010
    Posts
    927

    Default

    Quote Originally Posted by the_real_seebs View Post
    Okay, silly question.

    I register "foo". I then insert into its table { function, addonname, "/foo" }.

    Why is the name "foo" here twice? Can I insert additional slash commands into the same table obtained from command.slash.register? Do those two names have to be the same?
    The second "foo" is simply a name for an event. You can name it whatever you like. It's intended for error messages and performance diagnostics, and can also be used to track down specific event handlers in order to reorganize/remove/wrap them.

    Personally I'd probably use "slash command" for that.

  3. #3
    RIFT Community Ambassador the_real_seebs's Avatar
    Join Date
    Jan 2011
    Posts
    16,859

    Default

    ohhhh. I see. I didn't understand the contents of that table. I think what happened was I started with the thought that the item was { function, addon, slashcommand }, and then found out that you register a slashcommand and insert the item in that table.

    Hmm. table.insert... is there defined behavior if I insert more than one such table in the thing returned by command.slash.register?
    You can play WoW in any MMO. You don't have to play WoW in RIFT. Oh, and no, RIFT is not a WoW clone. Not having fun any more? Learn to play, noob! I don't speak for Riftui, but I moderate stuff there. Just came back? Welcome back! Here's what's changed. (Updated for 2.5!)

  4. #4
    Rift Chaser
    Join Date
    Oct 2011
    Posts
    398

    Default

    Quote Originally Posted by the_real_seebs View Post
    ohhhh. I see. I didn't understand the contents of that table. I think what happened was I started with the thought that the item was { function, addon, slashcommand }, and then found out that you register a slashcommand and insert the item in that table.

    Hmm. table.insert... is there defined behavior if I insert more than one such table in the thing returned by command.slash.register?
    I'd like to respond to this as I just finished figuring out exactly how the table worked... but I'm not sure I completely understand you.

    Let me try to break down how all the slash registration works. Do correct me if I am wrong - I am still learning LUA. If none of this answers your question, I'd appreciate it if you could elaborate.
    table = LUA table. LUA's version of arrays.
    insert(key, value) = method of table.
    Command.Slash.Register(command) = Returns event table with the following info:
    key1 = The function callback. (Correct me if I am wrong.) Must have only one parameter, which will be a string of all the args the user sends with the command.
    key2 = The addon name.
    key3 = As ZorbaT said, just a name for the event. I use "Slash Command".

    You can set/change the value of Command.Slash.Register(command) by using the table.insert method. If anything is already in there, it will be overridden.
    If nothing is in there, and thus it returns nil, then that command has not been set yet.
    Last edited by TimeBomb; 03-02-2012 at 03:34 AM.

  5. #5
    RIFT Community Ambassador the_real_seebs's Avatar
    Join Date
    Jan 2011
    Posts
    16,859

    Default

    No, table.insert(foo, bar) adds bar to foo, leaving existing entries alone.

    So x = Command.Slash.Register("foo"), if it succeeds, yields
    x = {}

    Then you can to table.insert(x, { a, b, c }) and you have
    x = {
    [1] = { a, b, c }
    }

    and if you do table.insert(x, { d, e, f }), you get
    x = {
    [1] = { a, b, c }
    [2] = { d, e, f }
    }

    but I don't know what happens at that point. For event handlers, all of the handlers get called, presumably in order.
    You can play WoW in any MMO. You don't have to play WoW in RIFT. Oh, and no, RIFT is not a WoW clone. Not having fun any more? Learn to play, noob! I don't speak for Riftui, but I moderate stuff there. Just came back? Welcome back! Here's what's changed. (Updated for 2.5!)

  6.   Click here to go to the next Rift Team post in this thread.   #6
    Rift Team
    Join Date
    Oct 2010
    Posts
    927

    Default

    Quote Originally Posted by the_real_seebs View Post
    For event handlers, all of the handlers get called, presumably in order.
    That's what happens Command.Slash.Register() creates a standard event table - it goes through the exact same code path.

  7. #7
    Rift Chaser
    Join Date
    Oct 2011
    Posts
    398

    Default

    Quote Originally Posted by the_real_seebs View Post
    No, table.insert(foo, bar) adds bar to foo, leaving existing entries alone.

    So x = Command.Slash.Register("foo"), if it succeeds, yields
    x = {}

    Then you can to table.insert(x, { a, b, c }) and you have
    x = {
    [1] = { a, b, c }
    }

    and if you do table.insert(x, { d, e, f }), you get
    x = {
    [1] = { a, b, c }
    [2] = { d, e, f }
    }

    but I don't know what happens at that point. For event handlers, all of the handlers get called, presumably in order.
    Oh. Just did a simple test.
    Registered the same command 3x to 3 different functions.
    Typed the command ingame.
    All 3 functions were called simultaneously... in order from the first registered to the last registered.

    Hope this helps.

    EDIT: Argh, ZorbaTHut beat me to the punch.

    EDIT2: Just thinking here... couldn't an addon developer be.. 'evil' and just do (Untested code, written at 4AM)
    Code:
    local myCommand = 'myaddon'
    local theTable = Command.Slash.Register(myCommand)
    if theTable ~= nil then -- Table is already set to a command - let's clear everything currently in the table so we can hog it
      local count = table.getn(theTable)
      for i = 1, count do
        table.remove(theTable, i)
      end
    end
    table.insert(theTable, {MyAddon.MyFunction, 'MyAddon', 'The ONLY Slash Command'})
    Thus they kill off all other addons attempting to use this command, and hog it for their own without warning anything/anyone. If they wanted to make this even more evil, they could have it 'check' to make sure it is the only slash command registered in the table every so often.

    I don't know if the above is possible. It's untested. But, if the following isn't already true, then I recommend something be done to make it so.
    The Command.Slash.Register should be somewhat protected to prevent tampering/etc, as you see above.
    I propose making it so that the table entry can't be removed from the table if the addon attempting to remove it is not the same as the entry's addon.
    Last edited by TimeBomb; 03-02-2012 at 04:31 AM.

  8. #8
    Plane Touched Verea's Avatar
    Join Date
    Feb 2011
    Location
    Netherlands
    Posts
    200

    Default

    Quote Originally Posted by TimeBomb View Post
    table = LUA table. LUA's version of arrays.
    I'd like to drop in something here, there is actually a different between Lua Arrays and Lua Tables. While the difference is rather small, it does count.

    Semantically, the difference between a table and a array in Lua is that an array only has numeric indices and no string indices. This generally has the result that arrays are a lot faster in Lua.

    Allocating an array:
    Code:
    local array = { 1, 2, 3, 4, 5, 6, 19, 59 }
    Code:
    local array = { "Apple", "Pear", "Orange" }
    Allocating a table:
    Code:
    local table = { Fruits = { "Apple", "Pear", "Orange" }, Numbers = { 1, 2, 3, 4, 5, 6, 19, 59 }}
    Maybe this clears some things up. In the end, an array is just a table with numeric indices exclusively, but some Lua programmers insist there is a difference. The access speed is a lot faster nevertheless.
    Verae, level 60 Cleric @ Blightweald, Guild Master of Tea Club
    I want to fly like an eagle, to the sea.
    I want to fly like an eagle, let my spirit carry me.

  9.   This is the last Rift Team post in this thread.   #9
    Rift Team
    Join Date
    Oct 2010
    Posts
    927

    Default

    Quote Originally Posted by TimeBomb View Post
    EDIT2: Just thinking here... couldn't an addon developer be.. 'evil'
    There's like a bajillion ways to be evil Yes, an addon could do that. They could also hook the system functions and cause the system functions to misbehave. They could also find important frames and hide them or break the SetPoint() linkages. The only way to prevent addons from being evil is to prevent them from communicating in any way, and some of the best addons are those that build on other addons to work.

    There's just no technical solution for that, unfortunately.

  10. #10
    Plane Walker Imhothar's Avatar
    Join Date
    Feb 2012
    Posts
    439

    Default

    Quote Originally Posted by Verea View Post
    I'd like to drop in something here, there is actually a different between Lua Arrays and Lua Tables. While the difference is rather small, it does count.

    Semantically, the difference between a table and a array in Lua is that an array only has numeric indices and no string indices. This generally has the result that arrays are a lot faster in Lua.

    Allocating an array:
    Code:
    local array = { 1, 2, 3, 4, 5, 6, 19, 59 }
    Code:
    local array = { "Apple", "Pear", "Orange" }
    Allocating a table:
    Code:
    local table = { Fruits = { "Apple", "Pear", "Orange" }, Numbers = { 1, 2, 3, 4, 5, 6, 19, 59 }}
    Maybe this clears some things up. In the end, an array is just a table with numeric indices exclusively, but some Lua programmers insist there is a difference. The access speed is a lot faster nevertheless.
    Additional offtopic remark:

    In fact, there is nothing like a "pure" array and a "pure" table in Lua. Every table in Lua contains both, an "array" and a "hash table". There is only one C data structure for tables. Whether your value is put in the array or the hash table compartment depends on some conditions.
    If the range 1..N has at least N/2 values and all keys are numeric then the values are placed in the array part, otherwise they go to the hash table. That means the table
    Code:
    Frutis = {
        "Apple",
        "Pear",
        "Orange",
        Exotic = "Mango",
        Red = "Strawberry"
    }
    has an "array" part with the indices 1, 2, 3 and values "Apple", "Pear", "Orange" and additionally a hash table with the values ("Exotic", "Mango") and ("Red", "Strawberry"). Access to Fruits[1] is stil fastest despite there being non-numerical indices in the table.
    If one were to add Fruits[100] = "Banana", then the array would be dumped and the values moved over to the hash table, because the range 1..100 only contains 4 < 100/2 values. Then Fruits[1] would no longer be fastest but as slow as Fruits["Exotic"]. Some implementations might keep the small array and only put the new value into the hash table. But for the default implementation the resulting increase of memory consumption is not acceptable.

    A different table created like
    Code:
    foo = { 1 = "1", 3 = "3", 4 = "4", 5 = "5" }
    Would also store all values in the array part, but index 2 with value nil. Creating the latter table would be slower than if one omitted the indices and put nil as second value, though.

    The thing is: it's not black and white whether you deal with an array or hash table and it is not necessary to strictly separate numerical and other indices into separate tables. Lua adapts to how you use the table. But fact is: as long as you only add continuous numerical indices to the table (that is exactly what table.insert does) you have an array and fastest lookups for these numeric indices.

+ Reply to Thread

Bookmarks

Posting Permissions

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