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
  • Using variables from parent java file


    Et43

    Recommended Posts

    Been struggling with this for a while now I've tried quite a lot of things and at this point idk if its even possible. I've tried using my prior python knowledge but nothing helps.

    I have a variable in a my main class and I want to access and use it in my paint class, but it does not seem to work.

    I've tried:

    • main core;
    • main core = new main();
    • main.yes;

    File structure:

    image.png.474ec6b63e61dc4651b530c82ed1cb07.png

     

    main

    import API.paint;
    import org.dreambot.api.script.Category;
    import org.dreambot.api.script.ScriptManifest;
    import org.dreambot.api.script.impl.TaskScript;
    
    import javax.swing.*;
    import java.awt.*;
    
    @ScriptManifest(name="TESTSSS", author="Et", version=1.1, category = Category.AGILITY)
    public class main extends TaskScript {
      // thing i want to use in my child java files vvvvvvvv
        public boolean yes;
    
        @Override
        public void onStart(){
            addNodes(new paint());
            SwingUtilities.invokeLater(() -> createGUI());
            log("Staet");
    
        }
        private void createGUI(){
            JFrame frame = new JFrame();
            frame.setTitle("test");
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.setLocationRelativeTo(getClient().getInstance().getCanvas());
            frame.setPreferredSize((new Dimension(250, 120)));
            frame.getContentPane().setLayout(new BorderLayout());
    
    
            JPanel buttonPanel = new JPanel();
            buttonPanel.setLayout((new FlowLayout()));
    
            JButton button = new JButton();
            button.setText("Start Script");
            button.addActionListener(l -> {
                yes = true;
    
                frame.dispose();
            });
            buttonPanel.add(button);
    
            frame.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
            frame.pack();
            frame.setVisible(true);
    
        }
    
    
    
    
    }

     

    paint

    package API;
    
    import org.dreambot.api.script.TaskNode;
    
    public class paint extends TaskNode {
    
       
    
        @Override
        public int priority() {
            return 1;
        }
    
        @Override
        public boolean accept() {
            if(){ // if the yes variable in main is true continue but i cant use the variable from main 
                return true;
            }else{
                return false;
            }
           
        }
    
        @Override
        public int execute() {
    
            return 300;
        }
    
    
    }

     

    Link to comment
    Share on other sites

    1 hour ago, Et43 said:

    Been struggling with this for a while now I've tried quite a lot of things and at this point idk if its even possible. I've tried using my prior python knowledge but nothing helps.

    I have a variable in a my main class and I want to access and use it in my paint class, but it does not seem to work.

    I've tried:

    • main core;
    • main core = new main();
    • main.yes;

    File structure:

    image.png.474ec6b63e61dc4651b530c82ed1cb07.png

     

    main

    
    import API.paint;
    import org.dreambot.api.script.Category;
    import org.dreambot.api.script.ScriptManifest;
    import org.dreambot.api.script.impl.TaskScript;
    
    import javax.swing.*;
    import java.awt.*;
    
    @ScriptManifest(name="TESTSSS", author="Et", version=1.1, category = Category.AGILITY)
    public class main extends TaskScript {
      // thing i want to use in my child java files vvvvvvvv
        public boolean yes;
    
        @Override
        public void onStart(){
            addNodes(new paint());
            SwingUtilities.invokeLater(() -> createGUI());
            log("Staet");
    
        }
        private void createGUI(){
            JFrame frame = new JFrame();
            frame.setTitle("test");
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.setLocationRelativeTo(getClient().getInstance().getCanvas());
            frame.setPreferredSize((new Dimension(250, 120)));
            frame.getContentPane().setLayout(new BorderLayout());
    
    
            JPanel buttonPanel = new JPanel();
            buttonPanel.setLayout((new FlowLayout()));
    
            JButton button = new JButton();
            button.setText("Start Script");
            button.addActionListener(l -> {
                yes = true;
    
                frame.dispose();
            });
            buttonPanel.add(button);
    
            frame.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
            frame.pack();
            frame.setVisible(true);
    
        }
    
    
    
    
    }

     

    paint

    
    package API;
    
    import org.dreambot.api.script.TaskNode;
    
    public class paint extends TaskNode {
    
       
    
        @Override
        public int priority() {
            return 1;
        }
    
        @Override
        public boolean accept() {
            if(){ // if the yes variable in main is true continue but i cant use the variable from main 
                return true;
            }else{
                return false;
            }
           
        }
    
        @Override
        public int execute() {
    
            return 300;
        }
    
    
    }

     

    At the very least it should be public static boolean yes;

    Link to comment
    Share on other sites

    You can do what Aeglen suggested, add the static keyword for the yes variable. Then you could access it this way: main.yes. I also suggest you to rename your main class as Main instead.

    Alternatively, pass the reference to your main class in the paint's constructor, store the reference in a field in the paint class and access it through that:

    class Main extends TaskScript {
    
    	@Override
    	public void onStart() {
    		addTask(new Paint(this));
    	}
    	...
    }
    
    class Paint extends TaskNode {
    
    	private final Main main;
    
    	public Paint(Main main) {
    		this.main = main;
    	}
    	
    	@Override
    	public boolean accept() {
    		return main.yes;
    	}
    	...
    }

     

    Link to comment
    Share on other sites

    34 minutes ago, Hashtag said:

    You can do what Aeglen suggested, add the static keyword for the yes variable. Then you could access it this way: main.yes. I also suggest you to rename your main class as Main instead.

    Alternatively, pass the reference to your main class in the paint's constructor, store the reference in a field in the paint class and access it through that:

    
    class Main extends TaskScript {
    
    	@Override
    	public void onStart() {
    		addTask(new Paint(this));
    	}
    	...
    }
    
    class Paint extends TaskNode {
    
    	private final Main main;
    
    	public Paint(Main main) {
    		this.main = main;
    	}
    	
    	@Override
    	public boolean accept() {
    		return main.yes;
    	}
    	...
    }

     

    Am i doing something wrong?

    Paint class

    https://imgur.com/20zeAnP

    Main class

    https://imgur.com/MGVlkRD

    Link to comment
    Share on other sites

    Archived

    This topic is now archived and is closed to further replies.

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