Jump to content

Filtering through NPC's help


roguehippo

Recommended Posts

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 :)

Link to comment
Share on other sites

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