Jump to content

Multiple classes :doge:


Recommended Posts

Posted

Alright so, I looked at quite a few tutorials, read quite a few posts and i'm still a tad confused.

 

I have a main class and then I have other classes.

If I'm trying to use methods in my secondary class, it tends to null out, even though methods are available.

 

Halp pls

package AIO;

public class wc extends AIORuneScape{

	int abc = 5;

	public void Woodcutting() throws InterruptedException {
		sleep(500);
		if (npcs.closest("Banker") != null) {
			log("test");
		}
	}

}

This is the secondary class.

Posted

In order to access OSBot fields and methods you must have a valid Script instance reference which must also be the instance you happen to be defining as your main class. I suggest you pass it as a parameter to the constructor of each class and store it in a non-static field. Then you can instantiate the class from your main class, passing the main class as a reference for your other classes to use in order to access OSBot methods and fields.

  • Like 3
Posted (edited)

If you are getting a NPE it's either because the object returns null (non-existent or out of index range) or you aren't properly initializing the API for other classes.

 

To elaborate on the post above:

public class Secondary extends ScriptNode {

    private API api;
    
    public Second(API api) {
        this.api = api;
  }
}
Edited by Bitshift
Posted

In order to access OSBot fields and methods you must have a valid Script instance reference which must also be the instance you happen to be defining as your main class. I suggest you pass it as a parameter to the constructor of each class and store it in a non-static field. Then you can instantiate the class from your main class, passing the main class as a reference for your other classes to use in order to access OSBot methods and fields.

pF9L7A7.png

  • Like 1
Posted

One of the pinnacles of OOP is polymorphism, which by definition, will result in multiple classes.

Well, it's a good thing I have this here thread for that there point you're making.

 

And 'one of'.

Saying I know about Java doesn't necessarily imply I can list every single function and know exactly what to do in every situation without outside assistance either.

Posted

I still don't fully understand what you're trying to do.

 

Are you trying to access wc methods from AIORunescape?

 

so wc w = new wc();

wc.woodcut();

?

 

That's what I see it as rn.

trying to call the woodcutting class from main script once it's needed

then use shit like players.myplayer and so forth in the woodcutting class itself

Posted (edited)

Let me elaborate the post I made earlier since I couldnt post code from my phone.

 

Main.Java

package somePackage;

import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;


@ScriptManifest(name = "AIO Runescape", author = "JohnnyDepp", version = 6.9, info = "Unicorns", logo = "")
public class Main extends Script {

    Woodcutting wc;

    @Override
    public void onStart() {
        wc = new Woodcutting(this);
    }

    @Override
    public int onLoop() {
        wc.cutTree();
        return 100;
    }

}

Woodcutting.Java

package somePackage;

public class Woodcutting {

    Main main;

    public Woodcutting(Main mainReference) {
        this.main = mainReference;
    }

    public void cutTree() {
        main.log("Time to cut some shit");
        // insert actual code
    }

}

The above code should spam your logger with "Time to cut some shit".

 

Edited by Token
  • Like 1
Posted

Let me elaborate the post I made earlier since I couldnt post code from my phone.

 

Main.Java

package somePackage;

import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;


@ScriptManifest(name = "AIO Runescape", author = "JohnnyDepp", version = 6.9, info = "Unicorns", logo = "")
public class Main extends Script {

    Woodcutting wc;

    @Override
    public void onStart() {
        wc = new Woodcutting(this);
    }

    @Override
    public int onLoop() {
        wc.cutTree();
        return 100;
    }

}

Woodcutting.Java

package somePackage;

public class Woodcutting {

    Main main;

    public Woodcutting(Main mainReference) {
        this.main = mainReference;
    }

    public void cutTree() {
        main.log("Time to cut some shit");
        // insert actual code
    }

}

The above code should spam your logger with "Time to cut some shit".

thanks bae, this worked perfectly

Posted (edited)

Let me elaborate the post I made earlier since I couldnt post code from my phone.

 

Main.Java

package somePackage;

import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;


@ScriptManifest(name = "AIO Runescape", author = "JohnnyDepp", version = 6.9, info = "Unicorns", logo = "")
public class Main extends Script {

    Woodcutting wc;

    @Override
    public void onStart() {
        wc = new Woodcutting(this);
    }

    @Override
    public int onLoop() {
        wc.cutTree();
        return 100;
    }

}

Woodcutting.Java

package somePackage;

public class Woodcutting {

    Main main;

    public Woodcutting(Main mainReference) {
        this.main = mainReference;
    }

    public void cutTree() {
        main.log("Time to cut some shit");
        // insert actual code
    }

}

The above code should spam your logger with "Time to cut some shit".

package trumpbitch;

import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;

import trumpbitch.Woodcutting;

@ScriptManifest(name = "trumpscape", author = "trump", version = 6.9, info = "trump", logo = "trump")
public class Main extends Script {

    @Override
    public void onStart() {
        // nothing cause trump
    }

    @Override
    public int onLoop() {
        Woodcutting.cutTree(this);
        return 100;
    }

}
package trumpbitch;

import org.osbot.rs07.script.Script;

public class Woodcutting {

    public static boolean cutTree(Script script) {
        script.log("trump for pres");
        // insert just the tip
        return false;
    }

}

this way is pretty much the same thing but i find it's easier to copy paste to new scripts this way. good luck

 

Edited by Shiny
  • Like 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

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