Et43 2 Posted May 23, 2021 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: 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; } }
Aeglen 423 Posted May 23, 2021 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: 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;
Hashtag 9072 Posted May 23, 2021 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; } ... }
Et43 2 Author Posted May 23, 2021 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
Recommended Posts
Archived
This topic is now archived and is closed to further replies.