Jump to content

Better Than a Script Instance


Joseph

Recommended Posts

Typically in most tutorial or snippet with in OSB. You have many of them telling you to create a script instance with in a new class. So that your allowed to use methods within the Script instance. Now i found a better and easier way of doing this. 

 

So here an example class

public class WorldHopper {
	
	private Script script;
	
	public final static int[] P2P_WORLDS = {301, 302, 303, 304, 305, 306, 309, 310, 311, 312,
		313, 314, 317, 318, 319, 320, 321, 322, 326, 327, 328, 329, 330, 333, 334, 335,
		336, 338, 341, 342, 343, 344, 345, 346, 349, 350, 351, 352, 353, 354, 357, 358,
		359, 360, 361, 362, 366, 367, 368, 369, 370, 373, 374, 375, 376, 377, 378};	
	
	public final static int[] F2P_WORLDS = {308, 316};
	
	public final static int[] PVP_WORLDS = {325, 337};

	public final static int[] TRIAL_WORLDS = {381, 382, 383, 384, 393, 394};
	
	public WorldHopper(Script script)	{
		this.script = script;
	}
	
	public void hopWorld(int...worlds)	{
		List<Integer> worldList = new ArrayList<Integer>(); 
		
		for (int world: worlds){
			worldList.add(world);
		}
		
		if (script.interfaces.closeOpenInterface())	{
			int index = MethodProvider.random(worldList.size());
			script.worldHopper.hop(worldList.get(index));			
		}else{
			script.log("can't close open interface");
		}
	}	

}

 

See how within your class constructor you have Script argument that will initialize your private field Script. So whenever you want to use the bank. You'll have to use your script instance then call the bank method. Finally call the methods you want to use from the bank class.

 

What you should do stead would be to extend Method Provider. Then within the constructor you will use the method within the Method Provider class to help you initialize all the classes it has.

 

Example of the code above redone.

public class WorldHopper extends MethodProvider{
		
	public final static int[] P2P_WORLDS = {301, 302, 303, 304, 305, 306, 309, 310, 311, 312,
		313, 314, 317, 318, 319, 320, 321, 322, 326, 327, 328, 329, 330, 333, 334, 335,
		336, 338, 341, 342, 343, 344, 345, 346, 349, 350, 351, 352, 353, 354, 357, 358,
		359, 360, 361, 362, 366, 367, 368, 369, 370, 373, 374, 375, 376, 377, 378};	
	
	public final static int[] F2P_WORLDS = {308, 316};
	
	public final static int[] PVP_WORLDS = {325, 337};

	public final static int[] TRIAL_WORLDS = {381, 382, 383, 384, 393, 394};
	
	public WorldHopper(Script script)	{
        exchangeContext(script.bot);

	}
	
	public void hopWorld(int...worlds)	{
		List<Integer> worldList = new ArrayList<Integer>(); 
		
		for (int world: worlds){
			worldList.add(world);
		}
		
		if (interfaces.closeOpenInterface())	{
			int index = MethodProvider.random(worldList.size());
			worldHopper.hop(worldList.get(index));			
		}else{
			log("can't close open interface");
		}
	}	
}

Link to comment
Share on other sites

dude i was just lazy i ddint really want to create a new instance and have to go about using that. Rather then having it already initialize.

 

Well that's why a lot of people including myself when I wrote scripts use the Node frame, since your able to store your method provider instance inside of the NodeWrapper, allowing you to use it throughout all your nodes, which I'm sure you already know.

 

Though as @Swizzbeat said above, this creates a unnecessary amount of method provider instances; when in reality, you only need one, and have it passed around like a dirty whore.

 

The less redundant instantiations, the better. :)

  • Like 3
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

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