Jump to content

monster detection


scriptersteve

Recommended Posts

I want to detect if a monster has spawned or not:

If the monster has spawned method is fine, if monster has not spawned goes ape-shit and crashes client:

public boolean getCallisto(){
    boolean b = false;
    NPC Callisto = getNpcs().closest(npc -> npc.getName().equals("Callisto"));
    if(Callisto.isVisible()){
        b = true;
    }

    return b;
}

 

not sure why, but assume there is a better way to detect if an NPC is present or not

Link to comment
Share on other sites

public boolean getCallisto(){
    boolean b = false;
    NPC Callisto = getNpcs().closest(npc -> npc.getName().equals("Callisto"));
    if(Callisto != null && Callisto.hasAction("Attack")) {
        b = true;
    }

    return b;
}

To illustrate what people are telling here. ALWAYS NULL CHECK.

Also, if spawning takes a while, also a good idea to check if it has the Attack option or any other stuff that's relevant.

Link to comment
Share on other sites

oh fair I had the null check in my method that called it, not this method. Guess i needed it in both. Now I look at my other scripts i do always nullcheck in the getMonster function rather than function calling it so idk why i didn't this time. Thanks for replies

11 hours ago, Alek said:

Why are you using closest? Also you are not null checking, I wouldn't trust you to write a chicken killer yet alone Callisto :( 

Edit: It was a joke, didn't mean to come off as a personal attack :feels: 

What's better than closest? I've always used that just because it can take a filter as an input?

Edited by scriptersteve
Link to comment
Share on other sites

  1. Your function is called getCallisto, but you're returning whether you see Callisto, not whether you've got him. There's plenty more information to gain from an NPC object, such as location, health, etc. But you're not returning that. Instead, you're returning just a bool.
  2. In said function, you're not checking to see whether Callisto is valid (null checks).
  3. You're using a Lambda expression to filter for an NPC by it's name only. That's putting a hat on a hat, when NPCS#getClosest will already let you find NPCs by their name.
  4. In your function, you get Callisto then you check whether Callisto is visible on your screen. I'm certain you intended to check whether Callisto is valid, not visible.
  5. Your entire function seems wholly redundant. Again, just find Callisto and store it as a local variable (outside onLoop), then check whether Callisto is valid (null check) and then do whatever it is you intend to do.

Example:

	NPC callisto;
	
	@Override
	public int onLoop() {
		
		if (callisto != null && callisto.exists()) {
		
			/* TODO: Callisto exists - what now? */
		
		} else {
		
			/* Find Callisto */
			
			callisto = npcs.closest("Callisto");
		}
		
		return 250;
	}

You can expand upon this by only searching for Callisto if your player is located near Callisto (distance check/area check/X,Y check). That way, you're not wasting time searching for Callisto while you're at the Grand Exchange, for instance.

Edited by liverare
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...