Jump to content
Frequently Asked Questions
  • Are you not able to open the client? Try following our getting started guide
  • Still not working? Try downloading and running JarFix
  • Help! My bot doesn't do anything! Enable fresh start in client settings and restart the client
  • How to purchase with PayPal/OSRS/Crypto gold? You can purchase vouchers from other users
  • Try asking for help in the chatbox
  • How to select a random node


    choucter
     Share

    Recommended Posts

    Hi, I’m having difficulty figuring out how to do something and curious if it’s even possible. I’m coding a skills leveler in TaskScript that will randomly choose between various nodes from the Main class. Each node’s execute()  is coded to raise that skill level by 3, and then return to Main to select another random skill.

    My solution to achieve this was a Do…While loop in Main. I generate a random number from 1 to 3, where 1 sets a true condition in the CombateNode accept() using the ‘fight’ variable, and similarly 2 for fishing and 3 for mining, as shown in the code below.

    itemMissing = true;
    do
    {
       
    int rndNumber = Calculations.random(1, 4); // assigns a random # from 1 to 3
       
    if (rndNumber == 1) {         // Run the Combat node
           
    fight = true;
           
    fish = false;
           
    mine = false;
       
    } else if (rndNumber == 2) {  // Run the Fishing node
           
    fight = false;
           
    fish = true;
           
    mine = false;
       
    } else if (rndNumber == 3) {  // Run the Mining node
           
    fight = false;
           
    fish = false;
            
    mine = true;
       
    }
    }

    while (itemMissing);

    Each node’s accept method also has a condition that checks for the appropriate equipment in the inventory. For example, if the random number is 2 but I don’t have a fishing net, then all 3 nodes will return false and the script will go back to the Main loop, generate a new random number and check that node's accept() for true. If the node returns true, it also sets itemMissing to false and exits the loop.

    My previous scripting has been in AbstractScript and my plan was to place the code inside onLoop. I now realize that TaskScript has its own internal onLoop and gives an error when I add onLoop into my script. I can't use onStart for this; I was desperate and tried it but the script loops inside the Do...While forever and doesn't reach node selection since onStart didn't complete.

    I can’t think of another way to achieve my plan. Anyone have any suggestions or is this simply not possible with TaskScript?

    Link to comment
    Share on other sites

    To overcome this obstacle you can try a different approach!
    For example, try setting a variable "initialized" to false! After that you can put that in the while section of this code.  After setting which "Task" you would like to select, at the bottom of each of your if statements you can set initialized to true.  This way this will only execute one time, and your do while loop will exit and it won't be stuck forever.   After this it should reach the node section.  This is definitely possible with TaskScript, however I recommend following a java tutorial to help you overcome this obstacle.  Here are some that I have found useful.
    https://www.w3schools.com/java/
    https://docs.oracle.com/javase/tutorial/getStarted/index.html

    Link to comment
    Share on other sites

    Thanks for the response. If I understand correctly, you're suggesting I leave my code in the onStart section?

    I feel your solution will execute a single node but then cannot get back into the loop to change the random number because onStart has now ended. Since the random number won't be changed, the script will keep selecting the same node (and therefore the same skill) over and over. I am trying to figure out how to select random skills.

    Am I missing something in your reply?

    Link to comment
    Share on other sites

    • 1 month later...

    Nice to see you managed it! Lacking a suitable answer here, I went back to AbstractScript and manage the "not real nodes" selection from onLoop. 

    I would be interested to know where you placed your timing/random code. Since there's no onLoop in TaskScript, I don't see where you can put it in the main class. The alternative seems to be that you have to put it inside each and every node? I did toy with the idea of adding a "control node" that runs on every odd-numbered loop and sets the random number. Then the random number is used on the even-numbered loops to execute a random node.

    That seems cumbersome though. I'm new to TaskScript but there must be a better way.

    Link to comment
    Share on other sites

    • 2 weeks later...

    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
     Share

    ×
    ×
    • Create New...

    Important Information

    We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.