Jump to content

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


Apaec

Recommended Posts

1 hour ago, badjie said:

what is the "." just before "closest" doing? i also didnt find anything on the api so im assuming its a language thing

Its a java thing.  I personaly suck at explaining things. So here some link. I would recommend watching a crash coruse on java over the next few days.

  1. https://www.freecodecamp.org/news/java-getters-and-setters/#:~:text=Getters and setters are used,as accessors and mutators%2C respectively.
  2. https://www.tutorialspoint.com/java/java_inheritance.htm
  3. https://www.tutorialspoint.com/java/java_object_classes.htm 

 

 

Edited by Nbacon
  • Like 1
Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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");
}

 

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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");
        }
    });
}

 

Link to comment
Share on other sites

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

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. 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

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!

Link to comment
Share on other sites

  • 2 weeks later...

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

 

Link to comment
Share on other sites

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