Jump to content
Frequently Asked Questions
  • Are you not able to open the client? Try following our getting started guide
  • Still not working? Try downloading and running JarFix
  • Help! My bot doesn't do anything! Enable fresh start in client settings and restart the client
  • How to purchase with PayPal/OSRS/Crypto gold? You can purchase vouchers from other users
  • WidgetChild seems to break script?


    SummonerLit

    Recommended Posts

    I'm still pretty new when it comes to writing scripts (learned this past week thanks to some of the tutorials here). Ive been trying my hand at making my own scripts without the help of others so I can learn more. I'm currently writing a script that just tans hides in Al Kharid, I got everything to work up to the point where the script needs to find the shop widget and tan the hides. I cant seem to find any tutorials that include the use of widgets so this has been quite hard. Below is what I currently have written. After adding in the WidgetChild line every time I go into the client to test the script, I press start on it and nothing happens. Nothing comes up in the debug console, theres no mouse movement, not even the script selection interface goes away. previously the bot would run up to the point of opening the trade option with the tanner and then stop as it had no further instruction on what to do. If I could get any help it would be much appreciated. If you need more info to help let me know, or if theres a currently existing tutorial on making a hide tanner or anything else that interacts with widgets that you could link, let me know. 

    package BasicHideTanner;
    
    import javafx.scene.Parent;
    import org.dreambot.api.methods.Calculations;
    import org.dreambot.api.methods.container.impl.bank.BankLocation;
    import org.dreambot.api.methods.filter.Filter;
    import org.dreambot.api.methods.map.Area;
    import org.dreambot.api.script.AbstractScript;
    import org.dreambot.api.script.Category;
    import org.dreambot.api.script.ScriptManifest;
    import org.dreambot.api.wrappers.interactive.NPC;
    import org.dreambot.api.wrappers.widgets.WidgetChild;
    
    
    @ScriptManifest(category = Category.MONEYMAKING, author = "Jacob", name = "BasicHideTanner", description = "Tans Hides in Al Kharid", version = 1.0)
    public class MainClass extends AbstractScript {
    
        Area tanningArea = new Area(3277,3189,3270,3193,0);
    
        Area bankArea = new Area(3269,3170,3272,3164,0);
    
        public static final int GOLD = 995;
        public static final int HIDES = 1739;
        public static final int LEATHER = 1741;
        public static final String TANNER = "Ellis";
        public static final String BANKER = "Banker";
        public static final Filter<NPC> BANK_FILTER = npc -> npc != null && npc.getName().equals(BANKER) && !npc.isHealthBarVisible();
        public static final Filter<NPC> TANNER_FILTER = npc -> npc !=null && npc.getName().equals(TANNER);
        public WidgetChild TanInterface = getWidgets().getWidgetChild(324);
        @Override
        public int onLoop() {
            if (getBank().isOpen());
                if (getInventory().isFull() && !getInventory().contains(HIDES,GOLD)){
                    getBank().depositAllExcept(GOLD,HIDES);
                }else if (getInventory().contains(GOLD)){
                    getBank().withdrawAll(HIDES);
                }else {
                    getBank().withdrawAll(GOLD);
                }
                if (!getBank().isOpen() && !getInventory().contains(HIDES,GOLD)) {
                    getBank().open(BankLocation.AL_KHARID);
    
                }
            if (getInventory().isFull() && getInventory().contains(HIDES,GOLD)){
                    getWalking().walk(tanningArea.getRandomTile());
            if (tanningArea.contains(getLocalPlayer())){
                NPC TANNER = getNpcs().closest(npc -> npc != null && npc.hasAction("Trade"));
                if (TANNER.interact("Trade")){
                  if  (sleepUntil(() -> getShop().isOpen(), 9000));
                      if  (getWidgets().getWidget(324) != null){
                          if (TanInterface != null && TanInterface.isVisible()){
                              TanInterface.interact("Tan All");
                          }
    
                      }
                }
                }
            }
    
            return (Calculations.random(500,2000));}
            }
    
    Link to comment
    Share on other sites

    public WidgetChild TanInterface = getWidgets().getWidgetChild(324);

    Initialize the "TanInterface" after your script is loaded, as in putting "TanInterface = getWidgets().getWidgetChild(324)" in either the constructor of the script or before you start looping the script. You might also want to do it only when the player is logged in, or else you are fetching a null interface that is not showing.

     

    Also as a side tip you might want to add some sleeps after each interaction rather than relying directly on what you return for the onLoop() method.

     

    Also:

    if (TanInterface != null && TanInterface.isVisible()){

    Should be:

    if(TanInterface != null) {
    	if(TanInterface.isVisbile()) {
    		// do things
    	}else{
    		// open it or boolean for "isTanInterfaceOpened" set to true.
    	}
    }

    Because even if TanInterface is null in your above code (not mine), it is still going to test "TanInterface.isVisible()" but since it is null, it will throw a NullPointerException.

    Link to comment
    Share on other sites

    36 minutes ago, ArmyofDragons said:
    
    public WidgetChild TanInterface = getWidgets().getWidgetChild(324);

    Initialize the "TanInterface" after your script is loaded, as in putting "TanInterface = getWidgets().getWidgetChild(324)" in either the constructor of the script or before you start looping the script. You might also want to do it only when the player is logged in, or else you are fetching a null interface that is not showing.

     

    Also as a side tip you might want to add some sleeps after each interaction rather than relying directly on what you return for the onLoop() method.

     

    Also:

    
    if (TanInterface != null && TanInterface.isVisible()){

    Should be:

    
    if(TanInterface != null) {
    	if(TanInterface.isVisbile()) {
    		// do things
    	}else{
    		// open it or boolean for "isTanInterfaceOpened" set to true.
    	}
    }

    Because even if TanInterface is null in your above code (not mine), it is still going to test "TanInterface.isVisible()" but since it is null, it will throw a NullPointerException.

    "public WidgetChild TanInterface = getWidgets().getWidgetChild(324)" exists before the onLoop, so im not sure where you mean to put it? Would this be better suited to go above Scriptmanifest or above the extension of AbstractScript?

    I figured I would add in the sleeps after the full loop functions, and then insert between actions.

    Will change the null and visibility section to what you have, just seemed better to compress to one line of &&, but I think I understand why that isnt good.

    Link to comment
    Share on other sites

    1 minute ago, SummonerLit said:

    "public WidgetChild TanInterface = getWidgets().getWidgetChild(324)" exists before the onLoop, so im not sure where you mean to put it? Would this be better suited to go above Scriptmanifest or above the extension of AbstractScript?

    I figured I would add in the sleeps after the full loop functions, and then insert between actions.

    Will change the null and visibility section to what you have, just seemed better to compress to one line of &&, but I think I understand why that isnt good.

    Um, no. That's not what I meant. You are initializing a Widget before the constructor is called.

     

    From StackerOverflow:

    The correct order of initialisation is:

    1. Static variable initialisers and static initialisation blocks, in textual order, if the class hasn't been previously initialised.
    2. The super() call in the constructor, whether explicit or implicit.
    3. Instance variable initialisers and instance initialisation blocks, in textual order.
    4. Remaining body of constructor after super().

     

    You are initializing a field as something that may not exist yet, because field initialization is called before anything else in the runtime process for Java. A good example of this is when someone starts the script and the widget is null or they are not even logged in, so the widget will always be null. So at the beginning it is always "null" and you have no point in initializing it at that point in time (unless Dreambot has it differently in how everything is loaded into memory).

    Link to comment
    Share on other sites

    9 minutes ago, ArmyofDragons said:

    Um, no. That's not what I meant. You are initializing a Widget before the constructor is called.

     

    From StackerOverflow:

    The correct order of initialisation is:

    1. Static variable initialisers and static initialisation blocks, in textual order, if the class hasn't been previously initialised.
    2. The super() call in the constructor, whether explicit or implicit.
    3. Instance variable initialisers and instance initialisation blocks, in textual order.
    4. Remaining body of constructor after super().

     

    You are initializing a field as something that may not exist yet, because field initialization is called before anything else in the runtime process for Java. A good example of this is when someone starts the script and the widget is null or they are not even logged in, so the widget will always be null. So at the beginning it is always "null" and you have no point in initializing it at that point in time (unless Dreambot has it differently in how everything is loaded into memory).

    Sorry if I'm misinterpreting what you're saying (much of this is still very new to me), although I think I understand the basics of what you mean. I'am initializing the widget during a time when the widget is inherently going to be null. I've moved down the line of WidgetChild TanInterface = getWidgets.... as seen below. This seems to have solved my problem of the script not doing anything. Now I just need to properly interact with the widget. Thanks for your help. 

    if (tanningArea.contains(getLocalPlayer())){
        NPC TANNER = getNpcs().closest(npc -> npc != null && npc.hasAction("Trade"));
        if (TANNER.interact("Trade")){
          if  (sleepUntil(() -> getShop().isOpen(), 9000));
          WidgetChild TanInterface = getWidgets().getWidgetChild(324);
          if (getShop().isOpen()){
              if (TanInterface != null){
                  if (TanInterface.isVisible()){
                      TanInterface.interact("Tan All");
          }else {
                      getShop().open();
                  }
    Link to comment
    Share on other sites

    6 minutes ago, SummonerLit said:

    Sorry if I'm misinterpreting what you're saying (much of this is still very new to me), although I think I understand the basics of what you mean. I'am initializing the widget during a time when the widget is inherently going to be null. I've moved down the line of WidgetChild TanInterface = getWidgets.... as seen below. This seems to have solved my problem of the script not doing anything. Now I just need to properly interact with the widget. Thanks for your help. 

    
    if (tanningArea.contains(getLocalPlayer())){
        NPC TANNER = getNpcs().closest(npc -> npc != null && npc.hasAction("Trade"));
        if (TANNER.interact("Trade")){
          if  (sleepUntil(() -> getShop().isOpen(), 9000));
          WidgetChild TanInterface = getWidgets().getWidgetChild(324);
          if (getShop().isOpen()){
              if (TanInterface != null){
                  if (TanInterface.isVisible()){
                      TanInterface.interact("Tan All");
          }else {
                      getShop().open();
                  }

    So it's solved? It looks like my code helped you fix your issue. As for the interaction part just find the WidgetChild (with the widget debugger) of the button (move your mouse on and off the button and see what changes to pinpoint it) then just called interact on it.

    Link to comment
    Share on other sites

    Archived

    This topic is now archived and is closed to further replies.

    ×
    ×
    • Create New...

    Important Information

    We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.