Jump to content

listing item causing script to break


scriptersteve

Recommended Posts

Item strPot = getInventory().getItem("Strength potion(4)","Strength potion(3)","Strength potion(2)","Strength potion(1)");

The following line of code, causes my script to not even start even when i am not implementing it anywhere in the script just defining it at the beginning.

TRying to use it in the following scenario:-

 

 

if((getSkills().getDynamic(Skill.STRENGTH) - getSkills().getStatic(Skill.STRENGTH)) < pottingValue) {
    if(inventory.contains("Strength potion(4)","Strength potion(3)","Strength potion(2)","Strength potion(1)")){
        strPot.interact("Drink");
    }
    pottingValue = random(2,6);
}
Link to comment
Share on other sites

45 minutes ago, scriptersteve said:

Item strPot = getInventory().getItem("Strength potion(4)","Strength potion(3)","Strength potion(2)","Strength potion(1)");

The following line of code, causes my script to not even start even when i am not implementing it anywhere in the script just defining it at the beginning.

TRying to use it in the following scenario:-

 

 


if((getSkills().getDynamic(Skill.STRENGTH) - getSkills().getStatic(Skill.STRENGTH)) < pottingValue) {
    if(inventory.contains("Strength potion(4)","Strength potion(3)","Strength potion(2)","Strength potion(1)")){
        strPot.interact("Drink");
    }
    pottingValue = random(2,6);
}

 

I'm assuming that strPot is defined as a member variable.

You can't do that, the API will not be setup yet.

The order is as follows:

  1. Static variables are initialised
  2. Member variables are initialised
  3. Constructor is called
  4. API context is exchanged
  5. onStart() is called
  6. onLoop() is repeatedly called until script exit

You are defining strPot as a member variable, if you look at the above list, you can see that it will be initialised before the API context is exchanged. This means that getInventory().getItem() will throw an NPE somewhere and your script will crash.

It doesn't make sense to do it there anyway, as it will only get a strength potion from your inventory once. If you then consumed that strength potion, that Item would  no longer exist and your script would fail. You need to call getInventory().getItem() every time you want to drink the potion. The code should be put inside of your if (inventory contains strength potion) check.

Side note:

You can make use of a Filter<Item> to reduce some code, for example:

private final Filter<Item> strPotionFilter = item -> item.getName().startsWith("Strength potion(");
    
public void someMethod() {
    Item strPotion = getInventory().getItem(strPotionFilter);
        
    if (strPotion != null) {
        strPotion.interact("Drink");
    }
}

 

Edited by Explv
  • Like 1
Link to comment
Share on other sites

  • Alek locked this topic
Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

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