Empires Posted July 24, 2016 Share Posted July 24, 2016 Hi guys, I'm new to scripting, and since this is my first time programming without guidance I had a quick question to try and get me off to a good start. Lets say I had a basic chicken killer script that kills chickens in either Lumbridge or Falador depending on what the user selects in the GUI. How would I structure this program into classes to minimise the redundant code regarding the methods between the two locations would be slightly different. Would I be able to build a class hierarchy based around the two locations and then inherit from the class that matches the users needs based on the location selected? Basically I'm just wanting to know what functionality should I be grouping together in my classes to allow minimum processing time and redundant code, and if I'm able to inherit from specific classes based on the GUI input (unless this isnt necessary/possible or theres a better way). Sorry for the noob question, I just want to be off to a good start, tyty:) Quote Link to comment Share on other sites More sharing options...
Final Posted July 24, 2016 Share Posted July 24, 2016 You've pretty much thrown a ton of buzz words/jargon into a question... You can do it however you like, it doesn't really matter within a script, as long as you aren't trying to call GC. 5 Quote Link to comment Share on other sites More sharing options...
Gary_Oak Posted July 24, 2016 Share Posted July 24, 2016 (edited) public class MyScript { Area area; public Area setArea(Area area) { this.area = area; } } public class Gui { MyScript script = new MyScript(); Area area1 = new Area(0,0,1000,1000); Area area2 = new Area(1001,1001,2000,2000); public void someButtonAction(){ Area area; if(user selects area1){ area = area1; }else{ area=area2 } script.setArea(area); } } tl;dr: Make an instance of your script in the GUI. use a setter Edited July 24, 2016 by Gary_Oak 3 Quote Link to comment Share on other sites More sharing options...
Easy Posted July 24, 2016 Share Posted July 24, 2016 (edited) With the goal of reusing as much code as possible without writing duplicate code, try to come up with the answer on your own Edited July 24, 2016 by Flamo 2 Quote Link to comment Share on other sites More sharing options...
Team Cape Posted July 24, 2016 Share Posted July 24, 2016 (edited) Hi guys, I'm new to scripting, and since this is my first time programming without guidance I had a quick question to try and get me off to a good start. Lets say I had a basic chicken killer script that kills chickens in either Lumbridge or Falador depending on what the user selects in the GUI. How would I structure this program into classes to minimise the redundant code regarding the methods between the two locations would be slightly different. Would I be able to build a class hierarchy based around the two locations and then inherit from the class that matches the users needs based on the location selected? Basically I'm just wanting to know what functionality should I be grouping together in my classes to allow minimum processing time and redundant code, and if I'm able to inherit from specific classes based on the GUI input (unless this isnt necessary/possible or theres a better way). Sorry for the noob question, I just want to be off to a good start, tyty:) dont think you'd need any of that... think you'd just need something like note: wrote this in like 2min dont take it word for word private enum State { KILLING, WAITING }; private State getState() { if(combat.isFighting()) { return State.KILLING; } return State.WAITING; } private int onLoop() { switch(getState()) { case WAITING: NPC n = npcs.closest("Chicken"); if(n != null) { n.interact("Attack"); } break; case ATTACKING: break; } return 100; } Spoonfeeding's no fun though Edited July 24, 2016 by Imateamcape 2 Quote Link to comment Share on other sites More sharing options...
Juggles Posted July 24, 2016 Share Posted July 24, 2016 (edited) Your question is confusing but if I am understanding it correctly, you don't need to put your whole attack code twice.Do something like if (GUI.AttackAreaSelectedByPlayer.contains(myPlayer) { //Attack code} else { //WalkToArea }this will separate your two areas and you will only need the code once. Edited July 24, 2016 by lg_juggles 1 Quote Link to comment Share on other sites More sharing options...
House Posted July 24, 2016 Share Posted July 24, 2016 public class MyScript { Area area; public Area setArea(Area area) { this.area = area; } } public class Gui { MyScript script = new MyScript(); Area area1 = new Area(0,0,1000,1000); Area area2 = new Area(1001,1001,2000,2000); public void someButtonAction(){ Area area; if(user selects area1){ area = area1; }else{ area=area2 } script.setArea(area); } } tl;dr: Make an instance of your script in the GUI. use a setter Why are you creating a new instance of your script when that is done by the script manager of the client. You mean create an instance of the GUI onStart()? 2 Quote Link to comment Share on other sites More sharing options...
Gary_Oak Posted July 24, 2016 Share Posted July 24, 2016 Yeah your correct Quote Link to comment Share on other sites More sharing options...
Qubit Posted July 24, 2016 Share Posted July 24, 2016 (edited) This boy just wrote half the definitions in the Java complete reference Edited July 25, 2016 by Qubit 2 Quote Link to comment Share on other sites More sharing options...
Empires Posted July 25, 2016 Author Share Posted July 25, 2016 This boy just wrote half the definitions in the Java complete reference Lol its a bit of a habit its how they have us explain everything in my CS course, ill tone it down next time :L Quote Link to comment Share on other sites More sharing options...
Explv Posted July 25, 2016 Share Posted July 25, 2016 (edited) Lets say I had a basic chicken killer script that kills chickens in either Lumbridge or Falador depending on what the user selects in the GUI. One simple method of doing it, would be to create an enum that stores the various Locations. For example: public enum Location { FALADOR(new Area(0, 0, 0, 0)), LUMBRIDGE(new Area(0, 0, 0, 0)); private final Area area; Location(final Area area) { this.area = area; } public final Area getArea() { return area; } @Override public final String toString() { return StringUtils.capitalize(name().toLowerCase()); } } Each value in the enum has an associated Area. This area can be used to walk to the chickens. Note, I also have overridden toString() here to return the name of the value in the enum in regular case, for example: VARROCK -> Varrock For the user to select a Location from the GUI, you can just use a JComboBox: JComboBox<Location> locationSelector = new JComboBox<>(Location.values()); Location selectedLocation = (Location) locationSelector.getSelectedItem(); You don't need to hardcode any paths because we now have web walking, so to walk to the chickens you can just do: getWalking().webWalk(chickenArea); Edited July 25, 2016 by Explv 1 Quote Link to comment Share on other sites More sharing options...
Empires Posted July 25, 2016 Author Share Posted July 25, 2016 One simple method of doing it, would be to create an enum that stores the various Locations. For example: public enum Location { FALADOR(new Area(0, 0, 0, 0)), LUMBRIDGE(new Area(0, 0, 0, 0)); private final Area area; Location(final Area area) { this.area = area; } public final Area getArea() { return area; } @Override public final String toString() { return StringUtils.capitalize(name().toLowerCase()); } } Each value in the enum has an associated Area. This area can be used to walk to the chickens. Note, I also have overridden toString() here to return the name of the value in the enum in regular case, for example: VARROCK -> Varrock For the user to select a Location from the GUI, you can just use a JComboBox: JComboBox<Location> locationSelector = new JComboBox<>(Location.values()); Location selectedLocation = (Location) locationSelector.getSelectedItem(); You don't need to hardcode any paths because we now have web walking, so to walk to the chickens you can just do: getWalking().webWalk(chickenArea); Thanks a lot for this, really easy to understand and helped me out ) Quote Link to comment Share on other sites More sharing options...
Ducky Posted July 25, 2016 Share Posted July 25, 2016 How many times can I use the word redundant in one post? Quote Link to comment Share on other sites More sharing options...
Qubit Posted July 25, 2016 Share Posted July 25, 2016 (edited) Lol its a bit of a habit its how they have us explain everything in my CS course, ill tone it down next time :L There is no way in a intro course they reinforce this terminology like that. XD Edited July 25, 2016 by Qubit Quote Link to comment Share on other sites More sharing options...
Prolax Posted July 29, 2016 Share Posted July 29, 2016 How many times can I use the word redundant in one post? Depends on if your post is redundant. Quote Link to comment Share on other sites More sharing options...