Vogelbekdier 1 Share Posted July 12, 2023 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 More sharing options...
camelCase 231 Share Posted July 12, 2023 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 More sharing options...
Vogelbekdier 1 Author Share Posted July 12, 2023 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 More sharing options...
camelCase 231 Share Posted July 12, 2023 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 More sharing options...
Vogelbekdier 1 Author Share Posted July 12, 2023 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 More sharing options...
Tweeboy 14 Share Posted July 12, 2023 (edited) 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 July 13, 2023 by roflme Link to comment Share on other sites More sharing options...
Diggington 18 Share Posted July 12, 2023 Are you initializing your wheat variable at a global level? It would help if you could paste your whole code Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now