Jump to content
View in the app

A better way to browse. Learn more.

OSBot :: 2007 OSRS Botting

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Basic Scripting, Classes, and Good Code

Featured Replies

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:)

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.

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 by Gary_Oak

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 by Flamo

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 sad.png

Edited by Imateamcape

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 by lg_juggles

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. emote32342.png

 

You mean create an instance of the GUI onStart()?

This boy just wrote half the definitions in the Java complete reference 

Edited by Qubit

  • Author

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

 

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 by Explv

  • Author

 

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 :))

How many times can I use the word redundant in one post?

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 by Qubit

How many times can I use the word redundant in one post?

 

Depends on if your post is redundant.

Create an account or sign in to comment

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.