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
  • Object null check not updating


    Vogelbekdier

    Recommended Posts

    I'm trying to null check a GameObject and interact if it's != null or else walk over.

    Initial null check comes back false and I walk over to the area, but even when I'm standing in a field of wheat it keeps still returning wheat == null. Only when I restart the script it will turn to true.

    Help please.

    Quote

    GameObject wheat = GameObjects.closest("Wheat");

    if (wheat == null) {

    walkToWheat();

    } else if (wheat != null) {

    pickWheat();\

    }

     

    Link to comment
    Share on other sites

    what happens inside those methods? and where is this code block positioned? sounds like wheat is never gets assigned again after its initialized but i cant tell not enough code

    
    // i think using guard clauses help your code look nicer and be clearer :3 like this
    if (wheat == null) {
    	walkToWheat();
      	return 2400; // or whatever
    } 
    pickWheat();
    return 2400; // or whatever

     

     

    Link to comment
    Share on other sites

    Quote
    12 minutes ago, camalCase said:

    what happens inside those methods? and where is this code block positioned? sounds like wheat is never gets assigned again after its initialized but i cant tell not enough code

    
    // i think using guard clauses help your code look nicer and be clearer :3 like this
    if (wheat == null) {
    	walkToWheat();
      	return 2400; // or whatever
    } 
    pickWheat();
    return 2400; // or whatever

     

    Quote
    private void pickWheat() {
        GameObject wheat = GameObjects.closest((gameObject) -> {
            return gameObject != null && gameObject.getName().equals("Wheat") && gameObject.canReach() && gameObject.hasAction(new String[]{"Pick"});
        });
    
        if (!Inventory.contains("Grain")) {
            wheat.interactForceLeft("Pick");
            Sleep.sleepUntil(() -> Inventory.contains("Grain"), longSleep);
        } else if (Inventory.contains("Grain")) {
            gotGrain = true;
        }
    }

    This doesn't work because when I start the script when Grain == null it always stays null. So it just walks over to the area and gives error "Grain == null" and freezes only when I restart the script grain becomes != null and it will pick one wheat. If I drop it manually again it will just stand there trying to pick another one but never actually does.

     

     

    Link to comment
    Share on other sites

    4 minutes ago, Vogelbekdier said:

    This doesn't work because when I start the script when Grain == null it always stays null. So it just walks over to the area and gives error "Grain == null" and freezes only when I restart the script grain becomes != null and it will pick one wheat. If I drop it manually again it will just stand there trying to pick another one but never actually does.

    what happens in walkToWheat

    Link to comment
    Share on other sites

    1 minute ago, camalCase said:

    what happens in walkToWheat

    Quote
    Area wheatArea = new Area(3162, 3294, 3159, 3296);
    walkToArea(wheatArea);
    
    private void walkToArea(Area area) {
        if (!area.contains(Players.getLocal())) {
            Walking.walk(area.getRandomTile());
            Sleep.sleepUntil(() -> Walking.getDestinationDistance() < 5, longSleep);
        }
    }
    Link to comment
    Share on other sites

    The snippets you've shared are a bit scattered, would need you to share a larger snippet to confirm issue

     

    You could condense that down to something like:

     

            if (!Inventory.contains("Grain")){
                if (!wheatArea.contains(Players.getLocal())){
                    if (Walking.shouldWalk(5)){
                        Walking.walk(wheatArea.getRandomTile());
                    }
                } else {
                    GameObject wheat = GameObjects.closest("Wheat");
                    if (wheat != null && wheat.interact()){
                        Sleep.sleepUntil(() -> Inventory.contains("Grain"), 2000);
                    }
                }
            }

     

    Edited by roflme
    Link to comment
    Share on other sites

    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
    ×
    ×
    • 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.