Results 1 to 11 of 11

Thread: How to find out what is in a loot bag

  1. #1

    How to find out what is in a loot bag

    Hello!

    Doing some scripting and looking for a function to find out the names (or number of) items in a loot bag. Tried getTable() but that doesn't seem to give me information about objects that aren't spawned. Any leads?

    Thanks!

  2. #2
    getObjects() on the bag will return name, GUID and index in the bag

    http://berserk-games.com/knowledgebase/object/ for details

  3. #3
    MrStump! Read your guides, very helpful leg up to get started. Thanks for the answer!

  4. #4
    Whoops! Too much of a novice to make it work the way I want. Don't leave yet, halp!

    contents = getObjectFromGUID('690698').getObjects()
    print(contents.name)
    print(contents.guid)
    print(contents.index)

    all come up nil

    The following does work, so I must still misunderstand and not know how to check the output of getObjects():

    takeParams={}
    takeParams.position = getObjectFromGUID('18236c').getPosition()
    takeParams.position['x'] = takeParams.position['x'] + 2
    takeParams.index = 0
    takeParams.top = true
    getObjectFromGUID('690698').takeObject(takeParams)

  5. #5
    probably because the bag contains more than one object.. the contents is a table of items. Easiest way to see the data (should work) is contents[1].name etc.. but in reality you're going to need to loop through the table:


    Code:
    for i, obj in ipairs(contents) do
        print(obj.name)
    end

  6. #6
    Quote Originally Posted by azreal42 View Post
    Whoops! Too much of a novice to make it work the way I want. Don't leave yet, halp!

    contents = getObjectFromGUID('690698').getObjects()
    print(contents.name)
    print(contents.guid)
    print(contents.index)

    all come up nil

    The following does work, so I must still misunderstand and not know how to check the output of getObjects():

    takeParams={}
    takeParams.position = getObjectFromGUID('18236c').getPosition()
    takeParams.position['x'] = takeParams.position['x'] + 2
    takeParams.index = 0
    takeParams.top = true
    getObjectFromGUID('690698').takeObject(takeParams)
    So when you use the .getObjects() function, you get a table of tables. It sort of looks like this:

    Code:
    {{item1},{item2},{item3} ... {item42}}
    Now, unfortunately for us, what information you can access regarding these items changes depending on whether it's a loot bag, deck or zone you've called .getObjects() on. You can check which keys go with which on the API.

    In order for you to print the name, guid and index like you've tried to do, you first need to access one element in the .getObjects() table. You'd use something like this for the first item:

    Code:
    print(contents[1].name)
    print(contents[1].guid)
    or something like this for the 31st:

    Code:
    print(contents[31].name)
    print(contents[31].guid)
    Like somebody above me said, you could also iterate through the table, and print every name of every object in the bag using ipairs(contents).

    Now, the great thing about it is that the table is keyed with indices and this allows us to just use the # symbol to get the number of items in the table. You have to be careful with it, because it won't count any table entries that have anything other than an index as a key.

    Examples:
    Code:
    contents = {[1]=item1, [2]=item2, [3]=item3}
    print(#contents) --> 3
    
    favouriteThings = {fruit = 'banana', song = 'American Pie', colour = 'Blue'}
    print(#favouriteThings) --> 0
    You can use #contents on a table that comes from getObjects() because it returns a table like the first one. A side point is also that the first table you can use ipairs(contents) or pairs(contents) to iterate over and you need pairs(favouriteThings) for the second... however I might be getting offtopic. Alternatively, you can also use lootbag.getQuantity() to return the number of items in a bag/deck.

    The reason why your takeObject code works is because most of the parameters are optional, and you don't need to provide it with much for it to just take the top object out of the deck/bag. You can get rid of takeParams.index and takeParams.top; you'll be just fine as it should default to the top of the Deck.

    Hopefully that's a little extra clarity
    Last edited by frisbee09; 02-16-2017 at 01:02 PM.

  7. #7
    Thanks! ipairs works. You've led me to the lua documentation which I'm finding extremely useful

  8. #8
    frisbee09 -

    What an incredible response. You're really helping me reckon with the problem. The ipairs iteration works for now, but I'll have to go back and optimize later based on your input! I'm coming from a matlab background so the whole thing seems kind of cloak and dagger not being able to examine workspace elements more freely but I'm learning!

  9. #9
    Oh well.

  10. #10
    Quote Originally Posted by azreal42 View Post
    frisbee09 -

    What an incredible response. You're really helping me reckon with the problem. The ipairs iteration works for now, but I'll have to go back and optimize later based on your input! I'm coming from a matlab background so the whole thing seems kind of cloak and dagger not being able to examine workspace elements more freely but I'm learning!
    No problem, I'm glad you found it useful.

    I came from a MATLAB background myself and I agree, not having a command line/interpreter readily available is kind of jarring at first but once you get used to debugging without it you'll find it's a fantastic transferable skill for many other languages. Happy coding!

  11. #11
    Quote Originally Posted by frisbee09 View Post
    I came from a MATLAB background myself and I agree, not having a command line/interpreter readily available is kind of jarring at first
    There is a Lua interpreter you can use for testing as long as you're not using any special TTS functions. It's great to use for testing when just starting out.

Similar Threads

  1. [SOLVED] Can't find servers
    By paglioz in forum Technical Support
    Replies: 0
    Last Post: 04-02-2016, 08:52 AM
  2. [ADDED] [v6.6] the ability to look for objects in the loot stashes
    By blackblizzard in forum Suggestions
    Replies: 8
    Last Post: 02-15-2016, 05:20 PM
  3. [ADDED] [v5.8] Able to create Custom Models that act like Loot Bags
    By SwiftIllusion in forum Suggestions
    Replies: 5
    Last Post: 10-09-2015, 11:12 PM
  4. Jointed objects in the Loot bag break apart.
    By Lucky seven in forum Bug Reports
    Replies: 0
    Last Post: 09-25-2015, 08:59 PM
  5. How did you find out about us?
    By Knil in forum General Discussion
    Replies: 48
    Last Post: 04-29-2014, 07:24 PM

Tags for this Thread

Posting Permissions

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