Question How to use item in inventory on an object?

Discussion in 'Developer Support' started by stanm3, Aug 8, 2015.

  1. 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 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?

  2. item1.interact("Use");
    item2.interact("Use -> " + item2.getName());

    That's for RS3.
    #2 Derk, Aug 8, 2015
    Last edited: Aug 8, 2015
  3. I'm trying to make a script bot 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.
  4. Just use .interact("Use", "Range"), the reason it's hovering is the action is "Use", not "Use -> Range"
  5. Still not luck, These are the two lines that I have.

    range.interact("Use", "Range");

    Right now it just selects the food item and mouse goes back and forth between the food item and range.
  6. Yeah that's your logic. Try this
    Code (Text):
    1. if(Inventory.getSelectedItem() == null){
    2.     SpriteItem item = Inventory.getItems(food).first();
    3.     if(item != null)
    4.         item.interact("Use");
    5. } else {
    6.     GameObject range = GameObjects.getLoaded("Range").nearest();
    7.     if(range != null)
    8.         range.interact("Use", "Range");
    9. }
    That's from the top of my head so there may be some errors.
  7. Now its just selecting the inventory object and hovers the range continuously changing mouse location of the hover.

    This is OSRS if that matters.

    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?
    #7 stanm3, Aug 9, 2015
    Last edited: Aug 9, 2015

  8. Gamobject name;


    idk if thats wha you want i didnt read whole thread in a rush sorry
  9. Don't know if this has been resolved, but...
    Code (Text):
    1. public static boolean useItemOnObject(SpriteItem item, GameObject object) {
    2.         if (item == null || object == null || !item.isValid() || !object.isValid())
    3.             return false;
    4.         if (item.interact("Use") && locatable(object,
    5.                 "Use", item.getDefinition().getName() + " -> " + object.getDefinition().getName())) {
    6.             return true;
    7.         }
    8.         return false;
    9.     }
    locatable method code:

    Code (Text):
    1. public static <I extends LocatableEntity> boolean locatable(I target, String action, String name) {
    2.         if (target == null || !target.isValid()) {
    3.             return false;
    4.         }
    5.         if (target.getVisibility() >= 80d) {
    6.             if (name.equals("")) {
    7.                 return target.interact(action);
    8.             } else {
    9.                 return target.interact(action, name);
    10.             }
    11.         } else {
    12.             if (target.distanceTo(Players.getLocal()) > 6d && target.getPosition().getPlane() == Players.getLocal().getPosition().getPlane()) {
    13.                 BresenhamPath.buildTo(target).step();
    14.                 return false;
    15.             } else {
    16.                 Camera.turnTo(target);
    17.                 return false;
    18.             }
    19.         }
    20.     }
  10. Why 80d and 6d?
  11. Dunno, habits from university. Prof took marks away if I didn't explicitly make it a double.
  12. That's strange, you'd think they'd focus more on stuff like method names xD
  13. 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.
    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.

Share This Page