Jump to content

dynamic task selection


Recommended Posts

Posted

Hey guys!

 

Newbie here. I'm writing my first chopper script using a standard node/task based framework, and I want to introduce a way to dynamically select a task (position, target, endconditions) when my script starts.

 

More specifically, I'm looking for some code magic that will run once on start, hold a few pieces of data, and be referable from within any of my node classes. Unfortunately I'm new to programming, so I'm clueless as to where I would even start searching for such a thing.

 

I attempted coding a task object which held static variables and was instantiated in my main script class, but I ran into goofy runtime errors that have me thinking there's a better way to accomplish my goal.

 

 

 

Posted

 

I attempted coding a task object which held static variables and was instantiated in my main script class, but I ran into goofy runtime errors that have me thinking there's a better way to accomplish my goal.

 

Not sure if I fully understand you or not, but I am assuming you are trying to make an AIO chopper, where the user can select different locations etc.

 

This is a very simple example, which you can build upon.

 

You start by having an enum for each Tree, which contains an Area in which the trees can be found:

public enum Tree {

    NORMAL (new Area(0, 0, 0, 0)),
    OAK (new Area(0, 0, 0, 0));

    Area area;

    Tree(final Area area){
        this.area = area;
    }

    @Override
    public String toString(){
        char[] name = name().toLowerCase().toCharArray();
        name[0] = Character.toUpperCase(name[0]);
        return new String(name);
    }
}

Then when the script starts, have the user select what tree they want to chop:

import org.osbot.rs07.script.Script;

import javax.swing.*;

public class Woodchopper extends Script {

    @Override
    public void onStart() {

        Tree selectedTree = (Tree) JOptionPane.showInputDialog(null,
                "Select a Tree",
                "Woodchopper",
                JOptionPane.INFORMATION_MESSAGE,
                null,
                Tree.values(),
                Tree.values()[0]
        );
    }

    @Override
    public int onLoop() throws InterruptedException {
        return 0;
    }
}

Your Chop Node can then take the selected tree as a constructor parameter:

import org.osbot.rs07.script.Script;

public class ChopNode extends Node {

    private final Tree tree;

    public ChopNode(final Script S, final Tree tree) {
        super(S);
        this.tree = tree;
    }

    @Override
    public boolean validate() {
        return !S.getInventory().isFull();
    }

    @Override
    public void execute() {
        if(!tree.area.contains(S.myPosition())){
            S.getWalking().webWalk(tree.area);
        } else{
            chop();
        }
    }

    private void chop(){
        // chop the trees
    }
}

So now in your onStart, when you create the nodes, you can pass the selected Tree:

import org.osbot.rs07.script.Script;

import javax.swing.*;

public class Woodchopper extends Script {

    @Override
    public void onStart() {

        Tree selectedTree = (Tree) JOptionPane.showInputDialog(null,
                "Select a Tree",
                "Woodchopper",
                JOptionPane.INFORMATION_MESSAGE,
                null,
                Tree.values(),
                Tree.values()[0]
        );

        Node chopNode = new ChopNode(this, selectedTree);
    }

    @Override
    public int onLoop() throws InterruptedException {
        return 0;
    }
}

Hope that helps

  • Like 1
Posted (edited)
Hope that helps

 

Perfect! Your example was well structured and easy to understand, and is equivalent to what I'm attempting to accomplish. Thanks homie! 

 

Sorry for my garbled example tongue.png it seems you understood. You guessed correctly about the AIO; this is a nicely generalizable construction, so I'm thinking things will work out nicely smile.png

 

 

Edited by Jachos

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