Jump to content

Why does my script attempt to attack a new npc


Recommended Posts

Posted
gargoyle = getNpcs().closest(n -> n.getName().equals("Gargoyle") && !n.isUnderAttack());
        if ((!player.isMoving()) && (!player.isUnderAttack()) && (!player.isAnimating()) && (gargoyle != null) && (gargoyle.getHealthPercent() > 0) && (!gargoyle.isUnderAttack() && (!gargoyle.isHitBarVisible())))
        {
            if(!gargoyle.isOnScreen())
            {
                getCamera().toEntity(gargoyle);
            }
            gargoyle.interact("Attack");
        }

Sometimes, the script will find a target successfully and click it, but then after one attack (and the npc not attacking me back) it will switch to a new gargoyle.

Does anyone know why/have any suggestions on how to fix this? I think its a result of player.isUnderAttack - obviously im not under attack(yet), but surely the player.isAnimating() call would fix it?

Cheers.

Posted (edited)
1 hour ago, Chuckle said:

add a sleep timer after the original attack so it has time to get in combat in game 

Thats sorted it, cheers. My sleep wasn't long enough.  Still happens every now and again though, no idea why. 5 second sleep should have sorted it tbf

Edited by Beenrange
Posted
5 minutes ago, Beenrange said:

Thats sorted it, cheers. My sleep wasn't long enough.  Still happens every now and again though, no idea why. 5 second sleep should have sorted it tbf

Use a conditional sleep to check if you're under attack or interacting with your npc. You can also combine filters, rather than adding a load of boolean checks like you've done above. ? 

  • Like 1
Posted (edited)
6 minutes ago, HeyImJamie said:

Use a conditional sleep to check if you're under attack or interacting with your npc. You can also combine filters, rather than adding a load of boolean checks like you've done above. ? 

Yeah, its my first time using filters so I tested removing some to see if that was the cause, I'll re-add them as clearly not. Will look at the conditional sleep - yet to use one but can see that SDN scripts require it so must be important. Do you have any examples of where it is used by any chance?

Edited by Beenrange
Posted
12 minutes ago, Beenrange said:

Yeah, its my first time using filters so I tested removing some to see if that was the cause, I'll re-add them as clearly not. Will look at the conditional sleep - yet to use one but can see that SDN scripts require it so must be important. Do you have any examples of where it is used by any chance?

Item food = getInventory().getItem("Example");

			if (food != null) {

				if(food.interact("Eat")){

				new ConditionalSleep(random(2500, 5000)) {
					@Override
					public boolean condition() {
						return !myPlayer().isAnimating();
					}

				 }.sleep();
               }
			}

It'll sleep until the return condition is met or until its slept for 2500-5000 ms

  • Like 1
Posted
22 minutes ago, Castro_ said:

Item food = getInventory().getItem("Example");

			if (food != null) {

				if(food.interact("Eat")){

				new ConditionalSleep(random(2500, 5000)) {
					@Override
					public boolean condition() {
						return !myPlayer().isAnimating();
					}

				 }.sleep();
               }
			}

It'll sleep until the return condition is met or until its slept for 2500-5000 ms

hmm.. I don't think the return condition is right, should be without the '!'. If you click eat, there's probably atleast 300 ms of no action which means your player isn't animating and it will immediately continue execution. If it loops fast enough in time (nothing else blocking execution) which it probably will, then it will double click or triple click until you do animate. 

Posted
46 minutes ago, Castro_ said:

Item food = getInventory().getItem("Example");

			if (food != null) {

				if(food.interact("Eat")){

				new ConditionalSleep(random(2500, 5000)) {
					@Override
					public boolean condition() {
						return !myPlayer().isAnimating();
					}

				 }.sleep();
               }
			}

It'll sleep until the return condition is met or until its slept for 2500-5000 ms

Thank you, the logic is clear now :) will be replacing many of my sleeps.

Posted
On 6/30/2018 at 10:27 AM, Beenrange said:

Thank you, the logic is clear now :) will be replacing many of my sleeps.

The random is completely useless. You're going to be returning the same sleep every single time (~500ms) because it's conditional with a 50ms recheck. Just save yourself the trouble and only add a timeout of 3000ms.

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
  • Recently Browsing   0 members

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