Jump to content

Noob question - Why Isn't this Task looping?


Elixar
 Share

Recommended Posts

  • I'm learning the different types of script making.
  • With this task based script I'm wondering why the <SlapCrabs> task only fights 1 npc then does nothing? ( testing on Highwayman, but the script will eventually be for crabs )

I know that you can use NPC filters and Lambda's ect. But for now I'm just trying to get this task to loop, It's supposed to fight highwaymen, But it only attacks 1 and then doesn't attack anymore after that?

here is the

  1. SlapCrabs ( Not looping )
  2. Task
  3. Main 

Why does it only kill 1 npc then stop? The canProcess() is true so it should attack as long as it's true?

SlapCrabs

package core;
import org.osbot.rs07.api.map.Area;
import org.osbot.rs07.api.model.NPC;
import org.osbot.rs07.script.MethodProvider;


public class SlapCrabs extends Task {
	public SlapCrabs(MethodProvider api) {
		super(api);
	}
	public static Area crabZone = new Area(3013, 3288, 2999, 3270);
	NPC npc = api.getNpcs().closest("Highwayman");
	

	@Override
	public boolean canProcess() {
		return 
		crabZone.contains(api.myPlayer()) && npc.isAttackable();
	}

	@Override
	public void process() {
		npc.interact("Attack");
		api.log("Slappin Crabz'");
	}
}


Task

package core;

import org.osbot.rs07.script.MethodProvider;

public abstract class Task {
	protected MethodProvider api;

	public Task(MethodProvider api) {
		this.api = api;
	}

	public abstract boolean canProcess();

	public abstract void process();
	
	public void run() {
		if (canProcess())
			process();
	}
	
}

 

Edited by Elixar
Link to comment
Share on other sites

1 hour ago, Elixar said:
  • I'm learning the different types of script making.
  • With this task based script I'm wondering why the <SlapCrabs> task only fights 1 npc then does nothing? ( testing on Highwayman, but the script will eventually be for crabs )

I know that you can use NPC filters and Lambda's ect. But for now I'm just trying to get this task to loop, It's supposed to fight highwaymen, But it only attacks 1 and then doesn't attack anymore after that?

here is the

  1. SlapCrabs ( Not looping )
  2. Task
  3. Main 

Why does it only kill 1 npc then stop? The canProcess() is true so it should attack as long as it's true?

SlapCrabs


package core;
import org.osbot.rs07.api.map.Area;
import org.osbot.rs07.api.model.NPC;
import org.osbot.rs07.script.MethodProvider;


public class SlapCrabs extends Task {
	public SlapCrabs(MethodProvider api) {
		super(api);
	}
	public static Area crabZone = new Area(3013, 3288, 2999, 3270);
	NPC npc = api.getNpcs().closest("Highwayman");
	

	@Override
	public boolean canProcess() {
		return 
		crabZone.contains(api.myPlayer()) && npc.isAttackable();
	}

	@Override
	public void process() {
		npc.interact("Attack");
		api.log("Slappin Crabz'");
	}
}


Task


package core;

import org.osbot.rs07.script.MethodProvider;

public abstract class Task {
	protected MethodProvider api;

	public Task(MethodProvider api) {
		this.api = api;
	}

	public abstract boolean canProcess();

	public abstract void process();
	
	public void run() {
		if (canProcess())
			process();
	}
	
}


Main
 


package core;

import org.osbot.rs07.api.ui.Skill;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;
import java.awt.*;
import java.util.ArrayList;

@ScriptManifest(author = "Elixar", info = "Fossil Island Crabs", logo = "https://i.imgur.com/tcvs0Wx.png", name = "Fossil Island Crabs", version = 0)

public class Main extends Script {
	ArrayList<Task> tasks = new ArrayList<Task>();
	private int slapped = 42;
	public String state = "Status: Idle";
	
	
	@Override
	public void onStart() {
		tasks.add(new WalkToCrabs(this));
		tasks.add(new SlapCrabs(this));
		log("Task System:Loading Tasks...");
		log("Task System:( ACTIVE )");
	}
	
	@Override
	public void onExit() {
		log("ree");
	}

	@Override
	public int onLoop() throws InterruptedException {
		tasks.forEach(tasks -> tasks.run());
		return 700;
	}

	@Override
	public void onPaint(Graphics2D g) {
		super.onPaint(g);
		g.setColor(Color.CYAN);
		g.setFont(new Font("Trebuchet MS", Font.BOLD, 14));
		g.drawString("Island Crabs 0.4", 8, 300);//X: 450 (left and right), Y: 345 (up and down)
		g.drawString("by Elixar", 8, 320);
		g.drawString("o", (int)getMouse().getPosition().getX() - 4, (int)getMouse().getPosition().getY() + 5);
		g.drawString(state, 8, 50);
		g.drawString("XP Gained: " + getExperienceTracker().getGainedXP(Skill.ATTACK) + " [lvl " + getExperienceTracker().getGainedLevels(Skill.ATTACK) + " ]", 8, 80);
		g.drawString("Slapped Crabs: " + slapped, 400, 320);
		
		
	}

}





:feels:        ?        :feels:          ?     :feels:

It's because you're declaring the Highwayman outside of the run loop, so it essentially only searches once for an available highwayman, move the 
 

NPC npc = api.getNpcs().closest("Highwayman");

into the process method. 

edit: to summarize, the task actually is actually being checked,, but it's only searching for a highwayman when the class is initially loaded, so Npc#isAttackable returns false

Edited by Slut
  • Heart 1
Link to comment
Share on other sites

32 minutes ago, Elixar said:

Oh my god you're 100% right.

You literally just took so much stress off my mind

Thank You!❤️

In the future, try placing some simple logs in your verify function (canProcess).

For example, in this case simply adding:

If (crabZone.contains(api.myPlayer())

   log("contains")

If (npc.isAttackable())

    log("attackable")

 

Would have greatly limit your bug searching to a specific area of the code and helped you figure out everything faster.

Just some advice :)

 

  • Heart 1
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.

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.

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...