Jump to content

Help With Interaction of Entities


mushbam

Recommended Posts

I'be been trying to make a very basic power miner, it works alright so far the only problem being that the script clicks on multiple rocks even with one interaction code line? i think it has something to so with how the interact method works but it looks very in-human clicking on different rocks running all over the place when i just want it to click on one rock in the MINE enum.

	private State getState() {
		RS2Object tinRocks = objects.closest(7485);

		if(!myPlayer().isAnimating() && !myPlayer().isMoving())
			if (tinRocks != null)
				if (inventory.isFull()) {
					return State.DROP;
				}
				else
				{
					tinRocks.interact("Mine");
					return State.MINE;
				}
		return State.WAIT;
		
	}

	@[member=Override]
	public int onLoop() throws InterruptedException {
		switch (getState()) {
		case MINE:
			
			log("(1) Selecting Rock");
			RS2Object currentRock = objects.closest("Rocks");
			sleepB(250);
			log("(1) Finshed Selecting Rock");

			log(currentRock.getGridX() +"   " +currentRock.getGridY());
			
			log("(2) Mining..");
			currentRock.interact("Mine");
			sleepB(1500);
			log("(2) Done Mining.");

			
			sleepB(2000);
			break;
		case DROP:
			inventory.dropAll();
			break;
		case WAIT:
			break;
		}
		return random(200, 300);
	}

Can anyone help? thanks.

Link to comment
Share on other sites

Few things:

 

  1.  You are initializing an RS2Object in your get state function. Don't do that.
  2.  The reason that it is running around is because you are initializing, and assigning your currentRock object each time onLoop runs. If you want to do it like this, you should initialize it as null outside of your loop, and assign it to a rock at the beginning of your script's runtime.
  3. You aren't doing any existence checks. Since there are other rocks in the error, there won't be a null pointer error (NPE), but you should always always always perform an existence check when you are calling something that could throw an NPE.

 

EDIT: I've modified your code so that you have a better working example. Read comments + complete small exercise.

	RS2Object tinRocks = null;

	onStart() {
		[...]
		if (getObjects.closest(7485) != null) {
			tinRocks = getObjects.closest(7485);
		}
	}

	private State getState() {
		if (inventory.isFull()) {
			return State.DROP;
		} else if (!myPlayer.isAnimating() && !myPlayer.isMoving && tinRocks.exists()) {
			return State.MINE // getState should ONLY return information, so we are moving calls to functions which cause something to happen in into your loop.
		} else {
		return State.WAIT;
		}
	}

	@[member=Override]
	public int onLoop() throws InterruptedException {
		if (tinRocks == null) { // Exercise: Move this existence check into your state function.
			log("There are no tin rocks in this area!");
			stop();
		}

		switch (getState()) {
		case MINE: // If you are using getState to determine which action you should be performing, and each state handles only one simple task, then migrate all of your
			[...]  // conditional checks into getState. If we are in this state, then we know that a) You are not animating, b) You are not moving, and c) The tin rock exists.
			break;
		case DROP:
			inventory.dropAll();
			break;
		case WAIT:
			[...]
			break;
		}
		return 1000;
	}
Edited by Solzhenitsyn
  • Like 1
Link to comment
Share on other sites

 

Few things:

 

  1.  You are initializing an RS2Object in your get state function. Don't do that.
  2.  The reason that it is running around is because you are initializing, and assigning your currentRock object each time onLoop runs. If you want to do it like this, you should initialize it as null outside of your loop, and assign it to a rock at the beginning of your script's runtime.
  3. You aren't doing any existence checks. Since there are other rocks in the error, there won't be a null pointer error (NPE), but you should always always always perform an existence check when you are calling something that could throw an NPE.

 

 

thanks for your help, ill try and fix those things you've mentioned.

Link to comment
Share on other sites

				if (inventory.isFull()) {
				    return State.DROP;
				} else {
				    tinRocks.interact("Mine");
				    return State.MINE;
				}

You should not be interacting at all in your getState() method. getState() is purely for 'getting the state'. Thus, it should be:

				if (inventory.isFull()) {
				    return State.DROP;
				}
				return State.MINE; //because you already know the
 				//inventory isn't full because it didn't returnState.DROP
                                //hence you don't need the 'else'  

Also, you might want to consider checking if your player is animating. You need to either use a conditionalsleep to check when your player has stopped animating, or you need to make a new state that is validated if your player is animating. Otherwise, when it's mining, it will keep finding other rocks to mine, and will click away from your current rock.

 

Also, it's very inefficient and costly to initialize an object in your getState() method. I'd say it's not inherently necessary to do so either, so I'd recommend finding a way around it.

			currentRock.interact("Mine");
			sleepB(1500);

Rocks will not always take exactly 1.5 seconds to mine. I'd look into conditional sleeps here if I were you. Or, like I said, make a new state, and just break the 'MINE' state directly after interacting.

 

Also note that you should be checking if the rock is a null! You might get null pointer exceptions when running the script.

 

Edit: my bad looks like you have a check for if your player is animating. I'd implement a conditional sleep after interacting with the rock that checks if it's a null though

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

When I get back from work I'll try help you out regarding this, I already have a good Tin Powerminer in my Iron Mine script (levels you to 15), So I'll pull that out and make sure its commented nicely and go through the logic. Ill be back at 5pm NZST (2 and a half hours)

 

Edit: Nvm, You have it fixed. If you ever need any help on anything specific, PM me I'm always happy to help with this stuff.

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

When I get back from work I'll try help you out regarding this, I already have a good Tin Powerminer in my Iron Mine script (levels you to 15), So I'll pull that out and make sure its commented nicely and go through the logic. Ill be back at 5pm NZST (2 and a half hours)

 

Edit: Nvm, You have it fixed. If you ever need any help on anything specific, PM me I'm always happy to help with this stuff.

 

Do you have some time to give me a few logic pointers. I am starting to script up again, ive had previous experience. (First Advertising other bots isn't allowed. tab maker) but I forgot all about it. Started to direct my life towards other things so I seriously forgot almost everything and anything.

Link to comment
Share on other sites

Do you have some time to give me a few logic pointers. I am starting to script up again, ive had previous experience. (First Advertising other bots isn't allowed. tab maker) but I forgot all about it. Started to direct my life towards other things so I seriously forgot almost everything and anything.

I have to go out soon, but when I get back I will try and post some good info. A good pointer for now, is just keep making scripts, it will take a couple but you should pick it all up pretty quick if you already have some experience.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

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