1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Question Change String in main task from leaftask

Discussion in 'Developer Support' started by Cru5ty, Mar 23, 2019.

  1. Cru5ty

    Joined:
    Mar 12, 2019
    Messages:
    59
    Likes Received:
    20
    Ok so im stuck, cant for the life of me get this to work. I am trying to change a string in my main class(treebot) from a LeafTask. Its doing my head in.

    I tried to change it directly by implimenting a comman in the Leaftask-

    new Cru5tyBot().status = ("Doing stuff...");

    Didnt work!

    In my main class I created a method to change it internaly -

    public class Cru5tyBot extends TreeBot implements EmbeddableUI {
    public String status = "Starting...";

    public void updateStatus(String newStatus) {
    this.status = newStatus;
    }
    }

    and calling that method in the Leasf Task -

    new Cru5tyBot().updateStatus("Doing stuff....");

    Still didnt work

    Should be simple but its driving me nuts, cant work out what im doing wrong, someone please help...
     
  2. Fabreze

    Fabreze #1 Fabric Cleaner

    Joined:
    Mar 18, 2017
    Messages:
    388
    Likes Received:
    106
    If this string is in your main class you can access it by using the Environment.getbot() method as follows: ((MainClassName)Environment.getbot()).stringname= “new string”.

    Also, make sure the string is public in your mainclass!

    I believe the reason your current implementation isnt’t working is because you created a new instance of your mainclass, so changing variables in that new instance wont affect anything in the first instance.
     
    #2 Fabreze, Mar 23, 2019
    Last edited: Mar 23, 2019
  3. Cru5ty

    Joined:
    Mar 12, 2019
    Messages:
    59
    Likes Received:
    20
    Thanks Fabreze, I knew it was creating a new instance and was 99% sure that was the issue but that was the only command I could get it to complie with. Its always easy once you see the answer, thank you for your help...
    --- Double Post Merged, Mar 25, 2019, Original Post Date: Mar 24, 2019 ---
    Ok new problem.

    I am trying to do the same (change a Boolean value in my main class) only this time from my controller class. Whenever I try to use the Enviroment.getbot() it throws an exception.

    Any ideas how to change main class values from the controller?
     
    Fabreze likes this.
  4. Fabreze

    Fabreze #1 Fabric Cleaner

    Joined:
    Mar 18, 2017
    Messages:
    388
    Likes Received:
    106
    The controller isn’t on the bot thread so thats why it’s throwing the error. To get around this you can pass your main class imas an argument into your controller usong a constructor. In your controller add a variable which stores your mainclass, and a constructor:

    Class mainclass;

    public controller(Class mainclass){

    This.mainclass= mainclass;

    }

    And then when load up your controller in your mainclass, pass “this” as its argument, loader.load(controller(this)) or howevr you load your controller in your main class.

    This way, you can access any public variables in your main class through

    mainclass.variable=whatever

    in your controller.
     
  5. Cru5ty

    Joined:
    Mar 12, 2019
    Messages:
    59
    Likes Received:
    20
    Thanks Mate you've been a massive help.

    Im sorry but I havnt scripted in a few years. Started with SCAR back in the day and end up on Powerbot and RSbot. Quit when whats his face went to work for Jagex 5 or so years ago. So Im very very rusty.

    I pretty much rewrote my whole bot to get it working. Now functioning pretty well. I am getting an exception thrown every time I start my bot,

    javafx.fxml.LoadException:
    unknown path
    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2425)
    at com.Cru5tyBots.bots.Cru5tyBot.UI.Cru5tyBotUI.<init>(Cru5tyBotInfoUI.java:103)
    at com.Cru5tyBots.bots.Cru5tyBot.Cru5tyBotbotInterfaceProperty(Cru5tyBot.java:88)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
    Caused by: java.lang.NullPointerException

    Which has something to do with
    loader.load(stream.get());

    Can you explain exactly waht stream.get() is doing any any idea why it may be throwing an exception? It doesnt sem to make any differance to my bot if I delete it all togehter but every exmple script bot I have disected seems to call it so im wondering what it does.
     
  6. Fabreze

    Fabreze #1 Fabric Cleaner

    Joined:
    Mar 18, 2017
    Messages:
    388
    Likes Received:
    106
    Join the Runemate slack and ask this in #development, I struggle with stacktraces and bugfixing in javafx myself and am not really sure what stream.get() does as Ive never called that before, but someone on there will be able to help!
     
  7. Cru5ty

    Joined:
    Mar 12, 2019
    Messages:
    59
    Likes Received:
    20
    Thanks mate, will do.

    It fixed itself somehow, I was editing my controller and I dont know what I done but its no longer throwing the exception. Would still like to know what it does...
     
    Fabreze likes this.

Share This Page

Loading...