Jump to content

Basic Scripting, Classes, and Good Code


Empires

Recommended Posts

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

Link to comment
Share on other sites

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
  • Like 3
Link to comment
Share on other sites

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
  • Like 2
Link to comment
Share on other sites

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
  • Like 1
Link to comment
Share on other sites

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()?

  • Like 2
Link to comment
Share on other sites

 

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
  • Like 1
Link to comment
Share on other sites

 

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

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...