Jump to content

Filtering through NPC's help


Recommended Posts

Posted

Hello, so im trying to make a filter that goes through all the npcs and find the one that is within 1 square of me and has a certain name. i think i got most of it right except when i go to use the list that the filter gives me, it says that it is full of "Objects" and not "NPC"s . even though i made the for npc objects. 

here is the filter object that i made:

myFilter = new Filter<NPC>() {
            public boolean match(NPC obj) {
                
                return (myArea.contains(obj)  && obj.getName() == "Npc name");
            }
        };

here is where i implement it: 

List npcs = getNpcs().filter(myFilter);
        for(NPC n : npcs)
        {
          code....  
        }

it says that "Type mismatch: cannot convert from element type Object to NPC" . sorry i dont understand filters too much i thought <NPC> would work, if anyone could help it would be appreciated :)

Posted (edited)
11 minutes ago, roguehippo said:

Hello, so im trying to make a filter that goes through all the npcs and find the one that is within 1 square of me and has a certain name. i think i got most of it right except when i go to use the list that the filter gives me, it says that it is full of "Objects" and not "NPC"s . even though i made the for npc objects. 

here is the filter object that i made:

myFilter = new Filter<NPC>() {
            public boolean match(NPC obj) {
                
                return (myArea.contains(obj)  && obj.getName() == "Npc name");
            }
        };

here is where i implement it: 

List npcs = getNpcs().filter(myFilter);
        for(NPC n : npcs)
        {
          code....  
        }

it says that "Type mismatch: cannot convert from element type Object to NPC" . sorry i dont understand filters too much i thought <NPC> would work, if anyone could help it would be appreciated :)

 

It returns a List<NPC> and you're storing it just as a List.

You need to specify the type:

List<NPC> npcs = getNpcs().filter(myFilter);


Also no need to define your own Filter for this, OSBot already has predefined ones:

getNpcs().closest(new NameFilter<>("NPC Name"), new AreaFilter<>(myArea));

Or if you don't care about whether it is the closest one:

getNpcs().singleFilter(getNpcs().getAll(), new NameFilter<>("NPC Name"), new AreaFilter<>(myArea));

 

Edited by Explv

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