Jump to content


Photo

Index in the treasure pool question


23 replies to this topic

#21 sdahlka

sdahlka

    Advanced Member

  • Members
  • PipPipPip
  • 324 posts

    Posted 17 February 2015 - 09:11 PM

    While everything sdahlka said was correct, a few additions:

     

    1. " don't work in the above code because the command handler uses those to group tokens together. It will work when you use it in an addon file, but if you enter it via the chat box or Windower console, you need to resort to using ' or escaping the " with a backslash: //eval print(\"word\")

     

    2. The outer if in the example code is not required.

     

    3. You don't need to check if the type of v is a table, only if v exists. If no item is in a certain treasure pool slot, the value will be nil.

     

    4. Re-using windower.ffxi.get_items() is a bad idea, as it's a very expensive function. In the above code snippet you call it up to 11 times every time the code runs, instead of just one, which is all you need.

     

    5. pairs, unlike ipairs, also doesn't guarantee the order. ipairs always enumerates elements from 1 to n, where n is the size of the table as defined by the # operator. pairs enumerates the elements in an arbitrary order (although it will often, but not always, coincide with ipairs on numerically indexed tables).

     

    All said, the code can be prettified a bit more if you're using common libraries. If you want, for example, a set of IDs, you can do this:

     

    S(windower.ffxi.get_items().treasure):map(table.get-{'item_id'})

    i have only seen codeing like this once and did not know how it worked

     

    so these are the same thing only one is less resource intensive

     

     

    require('luau')
    print(res.items[S(windower.ffxi.get_items().treasure):map(table.get-{'item_id'})].name)
    
    res = require('resources')
    if windower.ffxi.get_items().treasure[0] then
        for i,v in pairs(windower.ffxi.get_items().treasure) do
            if type(windower.ffxi.get_items().treasure[i]) == "table" then
                print(res.items[windower.ffxi.get_items().treasure[i].item_id].name)
            end
        end
    end
     


    #22 Arcon

    Arcon

      Advanced Member

    • Windower Staff
    • 1189 posts
    • LocationMunich, Germany

    Posted 17 February 2015 - 09:28 PM

    What I meant above is that windower.ffxi.get_items is a very expensive function and you should call it as little as possible. Your code is equivalent to this:

     

    res = require('resources')
    for _, v in pairs(windower.ffxi.get_items().treasure) do
        if v then
            print(res.items[v.item_id].name)
        end
    end

     

    That way windower.ffxi.get_items is only called once per run instead of up to 22 times a run.



    #23 sdahlka

    sdahlka

      Advanced Member

    • Members
    • PipPipPip
    • 324 posts

      Posted 17 February 2015 - 09:34 PM

      What I meant above is that windower.ffxi.get_items is a very expensive function and you should call it as little as possible. Your code is equivalent to this:

       

      res = require('resources')
      for _, v in pairs(windower.ffxi.get_items().treasure) do
          if v then
              print(res.items[v.item_id].name)
          end
      end

       

      That way windower.ffxi.get_items is only called once per run instead of up to 22 times a run.

      i bow to your wisdom



      #24 Arcon

      Arcon

        Advanced Member

      • Windower Staff
      • 1189 posts
      • LocationMunich, Germany

      Posted 17 February 2015 - 09:35 PM

      About this code:

      S(windower.ffxi.get_items().treasure):map(table.get-{'item_id'})

       

      Here is a breakdown of what's happening. I posted on Gist because that site is better for formatting code.






      1 user(s) are reading this topic

      0 members, 1 guests, 0 anonymous users