Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

Sign up now!

Question How to use item in inventory on an object?

Joined
Aug 6, 2015
Messages
4
How do I use an item in an inventory on a object? Like selecting food to cook on a range.

What I've been doing is selecting object in inventory and then using Gameobject.click() to do this, however if there's someone in front of the object it doesn't work. What is the proper way to do this?

Thanks.
 
12 year old normie
Joined
Jan 8, 2015
Messages
2,769
item1.interact("Use");
item2.interact("Use -> " + item2.getName());

That's for RS3.
 
Last edited:
Joined
Aug 6, 2015
Messages
4
I'm trying to make a script for OSRS.

Had no luck with that method, GameObject.getName() doesnt exist,

I even tried to manually enter name "Range" replacing getName that also doesnt work. it just hovers over the range continuously.
 
Joined
Dec 10, 2014
Messages
3,255
I'm trying to make a script for OSRS.

Had no luck with that method, GameObject.getName() doesnt exist,

I even tried to manually enter name "Range" replacing getName that also doesnt work. it just hovers over the range continuously.
Just use .interact("Use", "Range"), the reason it's hovering is the action is "Use", not "Use -> Range"
 
Joined
Aug 6, 2015
Messages
4
Still not luck, These are the two lines that I have.

Inventory.getItems(food).first().interact("Use");
range.interact("Use", "Range");

Right now it just selects the food item and mouse goes back and forth between the food item and range.
 
Joined
Dec 10, 2014
Messages
3,255
Still not luck, These are the two lines that I have.

Inventory.getItems(food).first().interact("Use");
range.interact("Use", "Range");

Right now it just selects the food item and mouse goes back and forth between the food item and range.
Yeah that's your logic. Try this
Code:
if(Inventory.getSelectedItem() == null){
    SpriteItem item = Inventory.getItems(food).first();
    if(item != null)
        item.interact("Use");
} else {
    GameObject range = GameObjects.getLoaded("Range").nearest();
    if(range != null)
        range.interact("Use", "Range");
}

That's from the top of my head so there may be some errors.
 
Joined
Aug 6, 2015
Messages
4
Now its just selecting the inventory object and hovers the range continuously changing mouse location of the hover.

This is OSRS if that matters.

Edit:
I think this might be because "Use" isnt actually an action of range since range only has examine option. Is there a way to just right click on an object and click a menu item?
 
Last edited:
Joined
Jan 1, 2015
Messages
272
I'm trying to make a script for OSRS.

Had no luck with that method, GameObject.getName() doesnt exist,

I even tried to manually enter name "Range" replacing getName that also doesnt work. it just hovers over the range continuously.


Gamobject name;

name.getDef().getName();

idk if thats wha you want i didnt read whole thread in a rush sorry
 
Joined
Nov 26, 2014
Messages
616
Don't know if this has been resolved, but...
Code:
public static boolean useItemOnObject(SpriteItem item, GameObject object) {
        if (item == null || object == null || !item.isValid() || !object.isValid())
            return false;
        if (item.interact("Use") && locatable(object,
                "Use", item.getDefinition().getName() + " -> " + object.getDefinition().getName())) {
            return true;
        }
        return false;
    }

locatable method code:

Code:
public static <I extends LocatableEntity> boolean locatable(I target, String action, String name) {
        if (target == null || !target.isValid()) {
            return false;
        }
        if (target.getVisibility() >= 80d) {
            if (name.equals("")) {
                return target.interact(action);
            } else {
                return target.interact(action, name);
            }
        } else {
            if (target.distanceTo(Players.getLocal()) > 6d && target.getPosition().getPlane() == Players.getLocal().getPosition().getPlane()) {
                BresenhamPath.buildTo(target).step();
                return false;
            } else {
                Camera.turnTo(target);
                return false;
            }
        }
    }
 
Joined
Dec 10, 2014
Messages
3,255
Don't know if this has been resolved, but...
Code:
public static boolean useItemOnObject(SpriteItem item, GameObject object) {
        if (item == null || object == null || !item.isValid() || !object.isValid())
            return false;
        if (item.interact("Use") && locatable(object,
                "Use", item.getDefinition().getName() + " -> " + object.getDefinition().getName())) {
            return true;
        }
        return false;
    }

locatable method code:

Code:
public static <I extends LocatableEntity> boolean locatable(I target, String action, String name) {
        if (target == null || !target.isValid()) {
            return false;
        }
        if (target.getVisibility() >= 80d) {
            if (name.equals("")) {
                return target.interact(action);
            } else {
                return target.interact(action, name);
            }
        } else {
            if (target.distanceTo(Players.getLocal()) > 6d && target.getPosition().getPlane() == Players.getLocal().getPosition().getPlane()) {
                BresenhamPath.buildTo(target).step();
                return false;
            } else {
                Camera.turnTo(target);
                return false;
            }
        }
    }
Why 80d and 6d?
 
Author of MaxiBots
Joined
Dec 3, 2013
Messages
6,774
I'm trying to make a script for OSRS.

Had no luck with that method, GameObject.getName() doesnt exist,

I even tried to manually enter name "Range" replacing getName that also doesnt work. it just hovers over the range continuously.

Gamobject name;

name.getDef().getName();

idk if thats wha you want i didnt read whole thread in a rush sorry
PhaseCoder is right. getName only exists in the definition so you must first store the definition and null check it. Then you can use getName on the definition.
item1.interact("Use");
item2.interact("Use -> " + item2.getName());

That's for RS3.
And this is correct too. Except item2 should be npc or whatever. And you should make sure to use proper logic to decide when to interact with the item and when to interact with the npc rather than just calling the mdirectly one after the other, because that can be buggy.
 
Top