Jump to content

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


Apaec

Recommended Posts

6 minutes ago, Rickest said:

Does anyone reccomend Eclipse today still? I have IntelliJ on my laptop but I'm having issues following step 2. on this guide. Trying to get a vers of eclipse if this doesnt work

I'm still using eclipse, not sure about set up for IntelliJ as i've not used it, but the idea is probably the same. I'm sure you can find an online tutorial for attaching a jar dependency for intelliJ somewhere!

Link to comment
Share on other sites

3 hours ago, Botislife said:

I think i've followed everything correctly, even used the exact code. But when I go to run the script ingame it does nothing.

NVM figured it out... Got it up in running. Nearly zero coding experience so this guide is great... 

Glad you got it working! Sorry that the guide is a little disorganised at the moment, i'm working on re-writing it but I cannot do it all in one go!

Good luck with your scripting journey, let me know if/when you need any help! (:

Apa

  • Like 2
Link to comment
Share on other sites

I got this code that teleports me to GE... The teleporting works fine but when I arrive at the GE it keeps teleporting until I run out of charges. Shouldn't the "if" statement only work if my player ISN'T inside the geArea? I know I can fix this by just adding more code after my character teleports... but that makes me wonder if "if" statements are even necessary or do they even work?

I don't know much about Java but it seems like the script just ignores all of the "if" statements and executes all methods from top to bottom. 

Code:

        case TELEPORT: 
            Area geArea = new Area(3165, 3480, 3161, 3476); //area of all possible squares in GE that you can get teleported to.
            Position current2 = myPlayer().getPosition();

            log("STARTING");  
            
            if (!geArea.contains(myPlayer()));{
                getEquipment().interact(EquipmentSlot.RING, "Grand exchange");
                log("teleporting");
                new ConditionalSleep(5000) {
                    @Override
                    public boolean condition() {
                        return !myPlayer().getPosition().equals(current2);
                    }
                }.sleep();        
            }
            sleep(random(500, 600));
                        
            break;
 

Edited by theinadequacy
Link to comment
Share on other sites

1 hour ago, theinadequacy said:

I got this code that teleports me to GE... The teleporting works fine but when I arrive at the GE it keeps teleporting until I run out of charges. Shouldn't the "if" statement only work if my player ISN'T inside the geArea? I know I can fix this by just adding more code after my character teleports... but that makes me wonder if "if" statements are even necessary or do they even work?

I don't know much about Java but it seems like the script just ignores all of the "if" statements and executes all methods from top to bottom. 

Code:

        case TELEPORT: 
            Area geArea = new Area(3165, 3480, 3161, 3476); //area of all possible squares in GE that you can get teleported to.
            Position current2 = myPlayer().getPosition();

            log("STARTING");  
            
            if (!geArea.contains(myPlayer()));{
                getEquipment().interact(EquipmentSlot.RING, "Grand exchange");
                log("teleporting");
                new ConditionalSleep(5000) {
                    @Override
                    public boolean condition() {
                        return !myPlayer().getPosition().equals(current2);
                    }
                }.sleep();        
            }
            sleep(random(500, 600));
                        
            break;
 

If statements do work! lol (:

Your code looks well structured - nicely done! The reason it is failing is purely syntactical - you've got a rogue ';' on line 7.

It may be a bit confusing, but long story short if you delete it, the code should work. The if statement is interpreted as a one-line (curly bracket-less) block and the empty expression is the conditional code. The curly brackets then form their own block which is always executed. Your code would look like this if properly formatted:

        case TELEPORT: 
            Area geArea = new Area(3165, 3480, 3161, 3476); //area of all possible squares in GE that you can get teleported to.
            Position current2 = myPlayer().getPosition();

            log("STARTING");  
            
            if (!geArea.contains(myPlayer()))
              	;
		{
                getEquipment().interact(EquipmentSlot.RING, "Grand exchange");
                log("teleporting");
                new ConditionalSleep(5000) {
                    @Override
                    public boolean condition() {
                        return !myPlayer().getPosition().equals(current2);
                    }
                }.sleep();        
            }
            sleep(random(500, 600));
                        
            break;

Hopefully that makes sense. Careful where you put the ';'s!

Apa

  • Like 1
Link to comment
Share on other sites

3 hours ago, Apaec said:

 

^

 


import org.osbot.rs07.api.model.Entity;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;

import java.awt.*;

@ScriptManifest(author = "You", info = "My first script", name = "Tea thiever", version = 0, logo = "")
public class Main extends Script {

    @Override
    public void onStart() {
        log("Welcome to Simple Tea Thiever by Apaec.");
        log("If you experience any issues while running this script please report them to me on the forums.");
        log("Enjoy the script, gain some thieving levels!.");
    }

    private enum State {
        STEAL, DROP, WAIT
    };

    private State getState() {
        Entity stall = getObjects().closest("Tea stall");
        if (!inventory.isEmpty())
            return State.DROP;
        if (stall != null)
            return State.STEAL;
        return State.WAIT;
    }

    @Override
    public int onLoop() throws InterruptedException {
        switch (getState()) {
        case STEAL:
            Entity stall = getObjects().closest("Tea stall");
            if (stall != null) {
                stall.interact("Steal-from");
            }
            break;
        case DROP:
            inventory.dropAll();
            break;
        case WAIT:
            sleep(random(500, 700));
            break;
        }
        return random(200, 300);
    }

    @Override
    public void onExit() {
        log("Thanks for running my Tea Thiever!");
    }

    @Override
    public void onPaint(Graphics2D g) {

    }

}

Link to comment
Share on other sites

What was the quick fix for rs2object when the redline last we’re under it?  May just need to right click it and create a field.

Edit, did you import  -  import org.osbot.rs07.api.model.RS2Object; ?, judging by what you posted - you did not.

If not, change it back to rs2Object and right click it, select the import option.  I am also learning and apa has helped me immensely, i will try to do my best to pay it forward when i can!

Edited by sonda
Link to comment
Share on other sites

8 hours ago, b4k4l1m said:


import org.osbot.rs07.api.model.Entity;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;

import java.awt.*;

@ScriptManifest(author = "You", info = "My first script", name = "Tea thiever", version = 0, logo = "")
public class Main extends Script {

    @Override
    public void onStart() {
        log("Welcome to Simple Tea Thiever by Apaec.");
        log("If you experience any issues while running this script please report them to me on the forums.");
        log("Enjoy the script, gain some thieving levels!.");
    }

    private enum State {
        STEAL, DROP, WAIT
    };

    private State getState() {
        Entity stall = getObjects().closest("Tea stall");
        if (!inventory.isEmpty())
            return State.DROP;
        if (stall != null)
            return State.STEAL;
        return State.WAIT;
    }

    @Override
    public int onLoop() throws InterruptedException {
        switch (getState()) {
        case STEAL:
            Entity stall = getObjects().closest("Tea stall");
            if (stall != null) {
                stall.interact("Steal-from");
            }
            break;
        case DROP:
            inventory.dropAll();
            break;
        case WAIT:
            sleep(random(500, 700));
            break;
        }
        return random(200, 300);
    }

    @Override
    public void onExit() {
        log("Thanks for running my Tea Thiever!");
    }

    @Override
    public void onPaint(Graphics2D g) {

    }

}

As Sonda said, You need the RS2Object import!

Link to comment
Share on other sites

13 hours ago, sonda said:

What was the quick fix for rs2object when the redline last we’re under it?  May just need to right click it and create a field.

Edit, did you import  -  import org.osbot.rs07.api.model.RS2Object; ?, judging by what you posted - you did not.

If not, change it back to rs2Object and right click it, select the import option.  I am also learning and apa has helped me immensely, i will try to do my best to pay it forward when i can!

Thanks, this worked for me. Now i have to figure out how to let the bot fill the inv and then drop all items, now its dropping immediately when 1 tea is in inv.

Link to comment
Share on other sites

25 minutes ago, b4k4l1m said:

Thanks, this worked for me. Now i have to figure out how to let the bot fill the inv and then drop all items, now its dropping immediately when 1 tea is in inv.

Great! It's awesome that you're trying to expand the code. I will give you a hint. At the moment, we are moving to the drop state when '!inventory.isEmpty()'.

You can do a similar check for whether the inventory is full, as follows:

getInventory().isFull(); // Returns whether the inventory is currently full

Good luck!

Apa

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