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

Question How to update GUI using TreeBot

Discussion in 'Developer Support' started by CodeNinja, Dec 27, 2016.

  1. CodeNinja

    Joined:
    Dec 20, 2016
    Messages:
    37
    Likes Received:
    3
    Hello all and happy holidays.

    Running into a wall at the moment trying to get a UI functioning properly.

    I have just added a bunch of stuff to my main java class, where the bot initially is constructed from.

    There is a method named "updateInfo", which successfully updates information on the UI. The problem is, I am unable to call this method after my RootTask is created and then running.

    Here is the code to my main java class:
    Code (Text):
    1. package com.CodeNinja.bots.NDivination;
    2.  
    3. import com.CodeNinja.bots.NDivination.Branches.RootBranch;
    4. import com.CodeNinja.bots.NDivination.UI.GuiFX;
    5. import com.CodeNinja.bots.NDivination.UI.Info;
    6. import com.CodeNinja.bots.NDivination.UI.InfoUI;
    7. import com.runemate.game.api.client.embeddable.EmbeddableUI;
    8. import com.runemate.game.api.hybrid.input.Mouse;
    9. import com.runemate.game.api.hybrid.util.StopWatch;
    10. import com.runemate.game.api.hybrid.util.io.ManagedProperties;
    11. import com.runemate.game.api.script.Execution;
    12. import com.runemate.game.api.script.framework.listeners.InventoryListener;
    13. import com.runemate.game.api.script.framework.tree.TreeBot;
    14. import com.runemate.game.api.script.framework.tree.TreeTask;
    15. import javafx.application.Platform;
    16. import javafx.beans.property.ObjectProperty;
    17. import javafx.beans.property.SimpleObjectProperty;
    18. import javafx.scene.Node;
    19.  
    20. public class NDivination extends TreeBot implements InventoryListener, EmbeddableUI {
    21.  
    22.     public Info info;
    23.     public static int EnergyLevel = 0;
    24.     public static int EnergyTicks = 0;
    25.  
    26.     private ObjectProperty<Node> botInterfaceProperty;
    27.     private String currentTaskString;
    28.     private GuiFX configUI;
    29.     private InfoUI infoUI;
    30.  
    31.     public boolean guiWait = true;
    32.  
    33.     private StopWatch stopWatch = new StopWatch();
    34.  
    35.     public String GatherType, Wisp;
    36.  
    37.     public NDivination() {
    38.         guiWait = true;
    39.         setEmbeddableUI(this);
    40.     }
    41.  
    42.     @Override
    43.     public ObjectProperty<Node> botInterfaceProperty() {
    44.         if (botInterfaceProperty == null) {
    45.             botInterfaceProperty = new SimpleObjectProperty<>(configUI = new GuiFX(this));
    46.             infoUI = new InfoUI(this);
    47.         }
    48.  
    49.         return botInterfaceProperty;
    50.     }
    51.  
    52.     @Override
    53.     public void onStart(String... args){
    54.         currentTaskString = "Starting bot";
    55.         stopWatch.start();
    56.         setLoopDelay(200,400);
    57.         Mouse.setSpeedMultiplier(1);
    58.         Mouse.setForceMenuInteraction(true);
    59.         getEventDispatcher().addListener(this);
    60.     }
    61.  
    62.     @Override
    63.     public void onStop(){
    64.         System.out.println("Bot ended, ran for " + stopWatch.getRuntime());
    65.     }
    66.  
    67.     @Override
    68.     public TreeTask createRootTask() {
    69.         // While we are waiting for guiWait to be false, stay in this loop
    70.         // This will prevent the bot from starting before the GUI is complete
    71.         // Note: It will wait for 60 seconds (60,000 milliseconds)
    72.         if (!Execution.delayUntil(() -> !guiWait, 60000)) {
    73.             System.out.println("Still waiting for GUI after a minute, stopping.");
    74.             stop();
    75.         }
    76.         return new RootBranch();
    77.     }
    78.  
    79.     // When called, switch the botInterfaceProperty to reflect the InfoUI
    80.     public void setToInfoProperty(){
    81.         botInterfaceProperty.set(infoUI);
    82.     }
    83.  
    84.     // This method is used to update the GUI thread from the bot thread
    85.     public void updateInfo() {
    86.         try {
    87.             // Assign all values to a new instance of the Info class
    88.             info = new Info(stopWatch.getRuntimeAsString(), currentTaskString);
    89.  
    90.         }catch(Exception e){
    91.             e.printStackTrace();
    92.         }
    93.  
    94.         // Be sure to run infoUI.update() through runLater.
    95.         // This will run infoUI.update() on the dedicated JavaFX thread which is the only thread allowed to update anything related to JavaFX rendering
    96.         Platform.runLater(() -> infoUI.update());
    97.  
    98.         /*
    99.         *  "The way to think about it
    100.             is that the "some stuff" is a package
    101.             and you're at your house (the bot thread)
    102.             Platform.runLater is the mailman
    103.             you give the mailman your package and then go about your life however you want
    104.             i.e. keep going in the code
    105.             and then the mailman does what he needs with the package to get it delivered
    106.             and it's no longer your or your house's problem"
    107.             - The Wise. The One. The Arbiter.
    108.          */
    109.     }
    110. }
    111.  

    Usually the "updateInfo" method would run in a "onLoop()" method, however I am using TreeBot and not LoopingScript.

    How can I call this method using TreeBots "createRootTask()"

    Thanks a ton! :D
     
    #1 CodeNinja, Dec 27, 2016
    Last edited: Dec 27, 2016
  2. Qosmiof2

    Qosmiof2 I've been called a god before.

    Joined:
    Aug 5, 2014
    Messages:
    3,212
    Likes Received:
    924
    Add on start:
    Code (Text):
    1.         new LoopingThread(() -> Platform.runLater(() -> infoUi.update()), 500).start();
    2.  
     
    CodeNinja likes this.
  3. CodeNinja

    Joined:
    Dec 20, 2016
    Messages:
    37
    Likes Received:
    3
    Code (Text):
    1.  
    2. onStart()
    3. new LoopingThread(() -> Platform.runLater(() -> infoUi.update()), 500).start();
    4.  
    Not seeing the UI updating, checking for what the issue is sec.
     
  4. Qosmiof2

    Qosmiof2 I've been called a god before.

    Joined:
    Aug 5, 2014
    Messages:
    3,212
    Likes Received:
    924
    Yes. Why are you initializing loop twice?
    You can just do what i wrote before. You will not use this thread anywhere else anyway.
     
  5. CodeNinja

    Joined:
    Dec 20, 2016
    Messages:
    37
    Likes Received:
    3
    Fixed, thank you very much!

    Code (Text):
    1. onStart()
    2. new LoopingThread(() -> Platform.runLater(() -> updateInfo()), 500).start();
     
    Qosmiof2 likes this.

Share This Page

Loading...