[Citizens API] Making NPC respond faster

Discussion in 'Spigot Plugin Development' started by XxFroggyKnightxX, Jul 1, 2018.

  1. Hello everyone! Today I've been working with the Citizen's API a lot. And frankly the NPC respond to the targeting very slowly. Is there anyways to make this faster? This is a short film of what I'm talking about. https://gyazo.com/315755b48957002ed5f12c6fbb6d219e

    My current code:

    Code (Text):


    npcInstance.getNavigator().setTarget(target, true); // target object is a player.

     
    If anyone has any idea, please do respond, thanks!
     
  2. FrostedSnowman

    Resource Staff

    A shame the api isn't the most documented it could be. However, try using Citizens' AStar pathfinding for this:

    Code (Java):
    getNavigator().getDefaultParameters()
            .useNewPathfinder(true);
    note: it has to calculate the positions, so any massive range that it has to pathfind to, will most likely time out your server, so don't go nuts on the range. maybe calculate the range by finding the distance from the NPC to its target location
     
  3. Gotcha, thanks I will try this out.
     
  4. FrostedSnowman

    Resource Staff

    can i see your full code
     
  5. Sure thing:

    Code (Text):

    public static void setGuardTarget(Player target, LivingEntity npc){
        NPC npcInstance = Main.registry.getNPC(npc);
        npcInstance.getNavigator().setTarget(target, true);
        npcInstance.getNavigator().getDefaultParameters().useNewPathfinder(true);
    ..........
    }
     
     
  6. FrostedSnowman

    Resource Staff

    It hits the player automatically, because the destination to the player is literally right there.

    I would recommend creating a Behavior or Goal for the npc to begin following the player, but only begin attacking the player after x ticks.

    Like I said, there's barely documentation for this, but if you implemented Behavior into a class, you should see what I mean.
     
  7. Alright thanks.