1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Question Is there a better way to check if NPC is dead?

Discussion in 'Developer Support' started by 6anon6, Jun 30, 2018.

  1. 6anon6

    Joined:
    May 14, 2018
    Messages:
    9
    Likes Received:
    3
    Currently using this to delay until the npc is dead:

    Code (Text):
    1. if (nearestNpc.interact("Attack")) {
    2.     Execution.delayUntil(() -> !nearestNpc.isValid(), () -> false, 10, 60000, 70000);
    3. }
    It works fine, except it's too slow. Between the time that it dies and the time that it becomes invalid, there's a lag (about 1-3 seconds).

    I tried doing this instead:

    Code (Text):
    1. if (nearestNpc.interact("Attack")) {
    2.     Execution.delayUntil(() -> nearestNpc.getHealthGauge().getPercent() == 0, () -> false, 10, 60000, 70000);
    3. }
    and it improves things a bit, but after some time nearestNpc.getHealthGauge().getPercent() throws a null pointer exception. This happens even if I null check it!

    So I was wondering if anyone knows of the fastest way to check when an NPC is dead?
     
    Charles Isbell likes this.
  2. Negrita

    Joined:
    Jan 28, 2017
    Messages:
    491
    Likes Received:
    183
    What do you null check nearestNPC, nearestNpc.getHealthGauge() or nearestNpc.getHealthGauge().getPercent()?
     
  3. 6anon6

    Joined:
    May 14, 2018
    Messages:
    9
    Likes Received:
    3
    Initially, just nearestNpc.getHealthGauge(). But then getPercent() also threw a NullException, even when getHealthGauge() was NOT null. Weird. So I tried to null check both of them individually, which didn't work for getPercent, it still threw the error.

    Anyway, got it sorted by doing:

    Code (Text):
    1. Execution.delayUntil(() -> nearestNpc.getHealthGauge() == null, () -> false, 10, 60000, 70000);
    and then storing the position of the dead NPC, and filtering the list of NPCS to exclude it (so that the player moves on to the next NPC as soon as it's dead and doesn't keep attacking until its invalid) :

    Code (Text):
    1. LocatableEntityQueryResults<Npc> allNpcs = Npcs.newQuery().names(npcNames).off(killedNpcPosition).results().sortByDistance();
     
  4. auxi

    Joined:
    May 24, 2016
    Messages:
    1,113
    Likes Received:
    990
    If you are on OSRS, health gauge works as a good solution to determine if an NPC is dying. Getting an NPE from it is likely on your end. Something like this should work:

    Code (Text):
    1.  
    2. Npc npc = Npcs.newQuery().names("test").filter(a -> {
    3.     CombatGauge gauge = a.getHealthGauge();
    4.     return gauge == null || gauge.getPercent() > 0;
    5. }).results().nearest();
    6.  
     
    Negrita, 6anon6 and CuppaJava like this.
  5. CuppaJava

    CuppaJava cuppa.drink(java);

    Joined:
    Mar 13, 2018
    Messages:
    6,120
    Likes Received:
    1,378
    Tbh @SlashnHax should tweak his fighter bot with this. It's a great bot but often clicks the npc when it's dying and I think this delay/check would fix it.
     
    #5 CuppaJava, Jun 30, 2018
    Last edited: Jun 30, 2018
    SilenceP44, 6anon6 and Negrita like this.

Share This Page

Loading...