Jump to content

A Beginners Guide to Writing OSBot Scripts (where to get started!) by Apaec


Recommended Posts

Posted

public int onLoop() throws InterruptedException {
        RS2Object stall = getObject().closest("Tea Stall");
                if (getInventory().contains("Cup of Tea")) {
                    getInventory().drop("Cup of Tea");
                    sleep(700);
                }
                else if (stall != null && !myPlayer().isAnimating) {
                        stall.interact("Steal-From");
                        log ("Stall Here!");
                        sleep(700);
                }
        return random(200, 300);
    }

 

So my inventory can be full and NOT have tea (just full with other items), i was thinking about doing something like

if ( getInventory().isFull() AND getInventory().contains("anything other than tea") ) {

getInventory().drop("a random item in my inventory")

 

I've been looking on the API but im not sure how to check for "anything other than tea" and how to drop a random item in my inventory.

 

Posted
1 hour ago, badjie said:

public int onLoop() throws InterruptedException {
        RS2Object stall = getObject().closest("Tea Stall");
                if (getInventory().contains("Cup of Tea")) {
                    getInventory().drop("Cup of Tea");
                    sleep(700);
                }
                else if (stall != null && !myPlayer().isAnimating) {
                        stall.interact("Steal-From");
                        log ("Stall Here!");
                        sleep(700);
                }
        return random(200, 300);
    }

 

So my inventory can be full and NOT have tea (just full with other items), i was thinking about doing something like

if ( getInventory().isFull() AND getInventory().contains("anything other than tea") ) {

getInventory().drop("a random item in my inventory")

 

I've been looking on the API but im not sure how to check for "anything other than tea" and how to drop a random item in my inventory.

 

Sounds like the relevant part of the API to you would be the inventory API: https://osbot.org/api/org/osbot/rs07/api/Inventory.html

Something like inventory#dropAllExcept sounds about right. For example:

if (getInventory().isFull()) {
	getInventory().dropAllExcept("Cup of Tea");
}

 

 

Posted
1 hour ago, Apaec said:

Sounds like the relevant part of the API to you would be the inventory API: https://osbot.org/api/org/osbot/rs07/api/Inventory.html

Something like inventory#dropAllExcept sounds about right. For example:


if (getInventory().isFull()) {
	getInventory().dropAllExcept("Cup of Tea");
}

 

 

what if i dont want to drop everything, and instead just 1 thing at a time? that's my real problem cuz i can't find anything for that

 

Posted
7 hours ago, badjie said:

what if i dont want to drop everything, and instead just 1 thing at a time? that's my real problem cuz i can't find anything for that

 

Hmm, this is certainly a niche situation, but can definitely be done. Might be a little trickier though. Typically, when the players inventory is full of junk so a script cannot run, the script should either terminate and let the player know this, or do something loss-less, such as going to a bank and depositing the junk. Arbitrarily dropping items isn't a great idea in general: what is the inventory was full of godswords?

Anyway, to achieve what you're looking for, the default API entries won't seem to provide this functionality. We'll have to create our own filter:

(Note that I haven't tested this code and wrote it here in the reply box so there could be errors, let me know if it doesn't work!)

if (getInventory().isFull()) {
    getInventory().dropForFilter(new Filter<Item>(){
    	@Override
        public boolean match(Item x) {
          return !x.getName().equals("Cup of Tea");
        }
    });
}

 

Posted
8 hours ago, Apaec said:

Hmm, this is certainly a niche situation, but can definitely be done. Might be a little trickier though. Typically, when the players inventory is full of junk so a script cannot run, the script should either terminate and let the player know this, or do something loss-less, such as going to a bank and depositing the junk. Arbitrarily dropping items isn't a great idea in general: what is the inventory was full of godswords?

Anyway, to achieve what you're looking for, the default API entries won't seem to provide this functionality. We'll have to create our own filter:

(Note that I haven't tested this code and wrote it here in the reply box so there could be errors, let me know if it doesn't work!)


if (getInventory().isFull()) {
    getInventory().dropForFilter(new Filter<Item>(){
    	@Override
        public boolean match(Item x) {
          return !x.getName().equals("Cup of Tea");
        }
    });
}

 

Oh i see what you did there! just to make sure you are basically making a filter for an item, then you create a method with the argument x, and make it return an item thats NOT x and then you pass that to the filter? i currently have no way to test it since i have a main rs account and i dont want to test it on a bot and get my ip flagged. I still need to take care of that later(prob getting a proxy). Anyways, the important thing is that i think i understood how it works. Thanks once again!

  • Like 1
Posted
2 hours ago, badjie said:

Oh i see what you did there! just to make sure you are basically making a filter for an item, then you create a method with the argument x, and make it return an item thats NOT x and then you pass that to the filter? i currently have no way to test it since i have a main rs account and i dont want to test it on a bot and get my ip flagged. I still need to take care of that later(prob getting a proxy). Anyways, the important thing is that i think i understood how it works. Thanks once again!

Yep, that's more or less what is going on. It looks a bit complicated and is certainly advanced syntax (it's an anonymous instantiation) so don't worry too much if you're not comfortable with it. 

Posted
23 minutes ago, Apaec said:

Yep, that's more or less what is going on. It looks a bit complicated and is certainly advanced syntax (it's an anonymous instantiation) so don't worry too much if you're not comfortable with it. 

Just wondering do most paid scripts use advanced java knowledge (like anonymous instantiation) or can most of them be acomplished through simple use of the api capabilities?

Posted
2 hours ago, badjie said:

Just wondering do most paid scripts use advanced java knowledge (like anonymous instantiation) or can most of them be acomplished through simple use of the api capabilities?

Well, 'advanced java knowledge' isn't exactly some unattainable feat, though it does take some practice and reading some tutorials. I'd say that while most scripters with premium scripts on the market have a solid if not strong understanding of Java, and some (esp. scripter IIIs) have wider software dev/computer science knowledge too, you can certainly get a long way with just the basics. It's amazing how much you can pick up by just trying API things out, failing, and asking questions - once you've beaten the initial learning curve (which is quite steep, but don't let that put you off!), improving is much quicker and easier. Indeed, writing scripts for OSBot doesn't have to just be a fun hobby---is a great introduction to the world of programming and software development. Well worth learning!

I'm always here to answer questions, programming related or otherwise. Reply here or send me a PM any time you need anything :)

-Apa

 

  • Like 1
Posted
24 minutes ago, Apaec said:

Well, 'advanced java knowledge' isn't exactly some unattainable feat, though it does take some practice and reading some tutorials. I'd say that while most scripters with premium scripts on the market have a solid if not strong understanding of Java, and some (esp. scripter IIIs) have wider software dev/computer science knowledge too, you can certainly get a long way with just the basics. It's amazing how much you can pick up by just trying API things out, failing, and asking questions - once you've beaten the initial learning curve (which is quite steep, but don't let that put you off!), improving is much quicker and easier. Indeed, writing scripts for OSBot doesn't have to just be a fun hobby---is a great introduction to the world of programming and software development. Well worth learning!

I'm always here to answer questions, programming related or otherwise. Reply here or send me a PM any time you need anything :)

-Apa

 

thank you so much man!

  • 2 weeks later...
Posted

I've recently started looking into learning to script and was wondering where do you suggest I go from here to learn more about scripting. I have no prior knowledge and would like to see someone make simple scripts like in a video or something like free lessons. Thanks for any advice and I really appreciate the guide :)

 

Posted
1 hour ago, mike3zx said:

I've recently started looking into learning to script and was wondering where do you suggest I go from here to learn more about scripting. I have no prior knowledge and would like to see someone make simple scripts like in a video or something like free lessons. Thanks for any advice and I really appreciate the guide :)

 

You can get a really long way by trying (and failing!) to write scripts of your own. This kind of stuff is much better learned by doing, rather than through lessons, guides or videos. Think of a cool script that you want to write, make sure it is simple, and give it a shot! Good examples of simple scripts are those which have a very basic task which can be gradually expanded. For example, a woodcutting script can start by just clicking on trees. Then, you can add appropriate delays and pauses, dropping, walking, banking, upgrading axes, etc etc.

As always, if you have any questions, post em here and i'll do my best to answer!

-Apa

  • Like 1
Posted

Thanks :) will definitely try to start something simple. Now with a woodcutting script or mining could I use a similar layout as this script but change some wording around to fit woodcutting. Inside of steal from Tea Stall it'd go to Chop Tree or Mine rock, can't remember what it's actually called in game off the top but my head. 

  • 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...