Booshotti 0 Share Posted July 25, 2017 Hello, I am curious what is the best way to store NPC's, Currently I am getNpcs().closest and I wanted to know if it's better to use a for loop to add NPC's to a list and then compare distance? I have noticed that my script sometimes doesn't go to the closest as at the time of the execution of the attack part of the script. Say I have an if statement that checks the closest and then within the if statement the closest has changed to another one because the NPCs are moving it will go a little crazy sometimes, I was thinking of doing getNpcs.all() and then sorting them within a List based on the distance so we can always go to the second closest NPC if the first closest has changed if that makes sense? I am at Rock Crabs so whilst attacking new idle rocks will spawn and it will choose the idle rock that is further away from the actual closest. Link to comment Share on other sites More sharing options...
TheMcPker 74 Share Posted July 25, 2017 well something you can do is for (NPC i : getNpcs().all()) { } and in that just compare distance Link to comment Share on other sites More sharing options...
Booshotti 0 Author Share Posted July 25, 2017 well something you can do is for (NPC i : getNpcs().all()) { } and in that just compare distance That was what I was thinking, Would you consider that to be more efficient than npc.closest()? Link to comment Share on other sites More sharing options...
TheMcPker 74 Share Posted July 25, 2017 That was what I was thinking, Would you consider that to be more efficient than npc.closest()? well you can check each npc this way for the things you wanna check rather then only the closest one for example if the closest ons doesn't have the attack option but the 2nd closest does then its gonna be hard to get that info using .closest Link to comment Share on other sites More sharing options...
Manly 879 Share Posted July 25, 2017 I'm pretty sure closest filters them on distance and you seem like the only one who has the problem where its not getting the closest entity. So are you sure its not other parts of your code? Link to comment Share on other sites More sharing options...
Dorkinator 64 Share Posted July 25, 2017 public T getBest(Evaluator<T> evaluator, List<T> objects){ T out = null; double best = Double.MAX_VALUE; if(objects.size() == 0){ System.out.println("No objects to evaluate"); return null; } for(T i:objects){ double tmp; if((tmp = evaluator.evaluate(i)) < best){ best = tmp; out = i; } } return out; } public abstract class Evaluator <C> { public abstract double evaluate(C i); } Link to comment Share on other sites More sharing options...
Booshotti 0 Author Share Posted July 25, 2017 I'm pretty sure closest filters them on distance and you seem like the only one who has the problem where its not getting the closest entity. So are you sure its not other parts of your code? It's really hard to explain what is going on, I will try the loop method anyway because right now I am limited on what I can check with the NPC's because it's only doing closest. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.