Jump to content

Why does my script attempt to attack a new npc


Beenrange

Recommended Posts

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.

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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