Jump to content

How do slots work?


Recommended Posts

Posted (edited)

getSlot

public int getSlot(Filter<Item>... filter)

Gets the slot for the first item matched after filtering the container.

Parameters: filter - The filter(s). Returns: The first slot matched.

http://osbot.org/api/org/osbot/rs07/api/util/ItemContainer.html#getItem-org.osbot.rs07.api.filter.Filter...-

 

1) Will the method return 0 if no item is found? 

2) What orientation? Is the upper left slot 1, upper right 4, lower left 24, lower right 28? 

Edited by FuckingAshole
Posted (edited)

This is something you could have easily figured out yourself by doing a small amount of testing.

 

The definition of getSlot() looks like:

public int getSlot(final Item item) {
    if(item == null) {
        return -1;
    } else {
        Item[] items = getInventory().getItems(); // returns an array of size 28
        for(int i = 0; i < items.length; i++) {
            if(items[i] != null && items[i].equals(item)) {
                return i;
            }
        }
        return -1;
    }
}

As you can see, if the item is not found it returns -1. Otherwise it returns a value from 0 (inclusive) to 28 (exclusive). 0 is the top left slot, 27 is the bottom right slot. The index increments from left to right:

0 1 2 3
4 5 6 7
8 9 10 11
etc.
Edited by Explv
  • Like 1
Posted (edited)

 

This is something you could have easily figured out yourself by doing a small amount of testing.

 

The definition of getSlot() looks like:

public int getSlot(final Item item) {
    if(item == null) {
        return -1;
    } else {
        Item[] items = getInventory().getItems(); // returns an array of size 28
        for(int i = 0; i < items.length; i++) {
            if(items[i] != null && items[i].equals(item)) {
                return i;
            }
        }
        return -1;
    }
}

As you can see, if the item is not found it returns -1. Otherwise it returns a value from 0 (inclusive) to 28 (exclusive). 0 is the top left slot, 27 is the bottom right slot. The index increments from left to right:

0 1 2 3
4 5 6 7
8 9 10 11
etc.

 

 

How did you find the source for the function? I have a hard time understanding API, but now that I know that getItems returns an array of  the contents of each slot (I was under the impression it returned an array of only the items contained) I understand perfectly. Thanks for your post.

Edited by FuckingAshole
Posted (edited)

How did you find the source for the function? I have a hard time understanding API

 

The source code is heavily obfuscated, so I wouldn't bother trying to read it.

 

In terms of the getSlot() method, as the API does not specify anything else, it is safe to assume that it would return an index from 0 to the inventory size. Most methods that return some form of index would start at 0, and you know that the inventory has 28 slots. As a valid index would always be positive, -1 is typically used to indicate that a valid index could not be found, you will find this everywhere in programming. Regarding the ordering, you can't know for sure as it's not in the API, but most people would assume left to right in rows. 

 

If you don't know exactly how something works, just write a little test and you'll find out.

Edited by Explv
Posted (edited)

 It simply returns the first index of a slot which is found to match your filtering criteria.

 

getSlot is a pretty misleading method name tbh, when it's purpose is almost exactly the same as indexOf. A more meaningful name would be getFirstSlotIndexOf, since there's a filter for a parameter and only one value gets returned. #indexOf default return value is -1, because 0 is a valid array index. With the value being -1, the programmer can use logic to determine if a slot had actually been returned, before attempting to access said slot one.

 

The orientation of slots is, I'm guessing, however Jagex dictate it to be, since all the widgets are designed by them. Khaleesi mentioned the orientation of the inventory slots.

Edited by liverare

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...