Jump to content

Script does nothing


EPLS

Recommended Posts

Hi, wanted to try creating a script for OSBot for the first time, came up with a cow killer. I didn't really do any studying or learning beforehand, I just kind of opened a few popular threads and used them to guide me through the creation of what I was hoping to be a simple script. Sorry if the code is a mess or if it's riddled with bad logic/bad code, I'm new to this.

import org.osbot.rs07.api.map.constants.Banks;
import org.osbot.rs07.api.model.Entity;
import org.osbot.rs07.api.model.RS2Object;
import org.osbot.rs07.api.util.LocalPathFinder;
import org.osbot.rs07.script.Script;

import org.osbot.rs07.script.ScriptManifest;

import java.awt.*;
import java.awt.geom.Area;

import javax.swing.text.Position;


@ScriptManifest(name = "Skeleton", author = "Alek", version = 1.0, info = "", logo = "") 

public class Skeleton extends Script {

    @Override

    public void onStart() {

    }
    
    private enum State {
    	GO_TO_BANK, WITHDRAW_FOOD, GO_TO_COWS, KILL_COW, WAIT
    }
    
    public static final Area LUMBRIDGE_UPPER;
    Position CowPosition = (Position) myPosition();

    private State getState() {
    	RS2Object cow = getObjects().closest("Cow");
		if (inventory.isEmpty()) //no food
			return State.GO_TO_BANK;
		if (Banks.LUMBRIDGE_UPPER.contains(myPosition()));
			return State.WITHDRAW_FOOD;
		if (inventory.isFull())
			return State.GO_TO_COWS;
		if (inventory.contains(329))
			return State.KILL_COW;
		return State.WAIT;
    }


	@Override

    public void onExit() {

        //Code here will execute after the script ends

    }
    @Override
    public int onLoop() throws InterruptedException {
    	switch (getState()) {
    	case GO_TO_BANK:
    		getWalking().webWalk(Banks.LUMBRIDGE_UPPER); //walk to lumby bank
    		break;
    	case WITHDRAW_FOOD:
    		npcs.closest("Banker").interact("Bank"); //withdraw food from lumbridge bank booth
    		break;
    	case GO_TO_COWS:
    		Position pos1 = new Position(3259, 3269, 0);
    		getWalking().walk(pos1); //walk to cows position
    		break;
    	case KILL_COW:
    		npcs.closest("Cow").interact("Attack");//get cow npc and kill, cow 2805-2809
    		break;
    	case WAIT:
    		break;
    	}
        return random(210, 305); //The amount of time in milliseconds before the loop starts over
    }

    @Override

    public void onPaint(Graphics2D g) {

        //This is where you will put your code for paint(s)

    }

}

 

Link to comment
Share on other sites

2 hours ago, sp3cpk said:

what do you mean by "script does nothing"

Be specific, what area is your character in? what exactly do you have in your inventory.

 

2 hours ago, EPLS said:

I've taken a Java course before, but it's been a while. I was hoping someone could give me a few pointers.

He is right, we will need more information about what is wrong in order to help you

Link to comment
Share on other sites

I'd recommend starting on something much simpler, and working your way up. A good example would be woodcutting; first make a script that can chop a tree with a paint as well, then add dropping to it. Then make it bank the logs instead of dropping them, and then maybe add some options in a gui - it's all stuff to learn, but the key is to get the basics down before getting too involved!

If you have any questions, don't hesitate to ask me, i'll do the best to help out as best I can. Beyond that, the best way of learning is trying so you're certainly on the right track!

Good luck!

Apa

Link to comment
Share on other sites

it wont start because of the line

Position CowPosition = (Position) myPosition();

 

its probably that the client needs to be ingame before you can get the position of the player

also 

if (Banks.LUMBRIDGE_UPPER.contains(myPosition()));

you should remove the ;

 

 

and instead of 

import java.awt.geom.Area;

import javax.swing.text.Position;

you need to 

 

import org.osbot.rs07.api.map.Area;
import org.osbot.rs07.api.map.Position;
Link to comment
Share on other sites

7 hours ago, EPLS said:

Hi, wanted to try creating a script for OSBot for the first time, came up with a cow killer. I didn't really do any studying or learning beforehand, I just kind of opened a few popular threads and used them to guide me through the creation of what I was hoping to be a simple script. Sorry if the code is a mess or if it's riddled with bad logic/bad code, I'm new to this.




import java.awt.*;
import java.awt.geom.Area;

 

should really be 

import org.osbot.rs07.api.map.Area;

 

like the poster above me said.

 

also, if statements should be scoped,  and its a common beginner mistake to add a semicolon to your if statement but you should avoid doing that.

 

and 1 more thing

if (inventory.isFull())
			return State.GO_TO_COWS;

is the same as saying

if inventory is full, go to cows.

it should be

!inventory.isFull()

which translates to not inventory is full

 

package core;

import org.osbot.rs07.api.map.Area;
import org.osbot.rs07.api.map.Position;
import org.osbot.rs07.api.map.constants.Banks;
import org.osbot.rs07.api.model.RS2Object;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;

import java.awt.*;

import static org.osbot.rs07.api.map.constants.Banks.LUMBRIDGE_UPPER;

@ScriptManifest(name = "Skeleton", author = "Alek", version = 1.0, info = "", logo = "") 

public class Skeleton extends Script {

    @Override

    public void onStart() {

    }

    private enum State {
        GO_TO_BANK, WITHDRAW_FOOD, GO_TO_COWS, KILL_COW, WAIT
    }

    public static final Area BANK_AREA = Banks.LUMBRIDGE_UPPER;
    Position CowPosition = (Position) myPosition();

    private State getState() {
        RS2Object cow = getObjects().closest("Cow");
        if (inventory.isEmpty()) { //no food
            return State.GO_TO_BANK;
        }
        if (LUMBRIDGE_UPPER.contains(myPosition())) {
            return State.WITHDRAW_FOOD;
        }
        if (!inventory.isFull()) {
            return State.GO_TO_COWS;
        }
        if (inventory.contains(329)) {
            return State.KILL_COW;
        }
        return State.WAIT;
    }


    @Override

    public void onExit() {

        //Code here will execute after the script ends

    }
    @Override
    public int onLoop() throws InterruptedException {
        switch (getState()) {
            case GO_TO_BANK:
                getWalking().webWalk(BANK_AREA); //walk to lumby bank
                break;
            case WITHDRAW_FOOD:
                npcs.closest("Banker").interact("Bank"); //withdraw food from lumbridge bank booth
                break;
            case GO_TO_COWS:
                Position pos1 = new Position(3259, 3269, 0);
                getWalking().walk(pos1); //walk to cows position
                break;
            case KILL_COW:
                npcs.closest("Cow").interact("Attack");//get cow npc and kill, cow 2805-2809
                break;
            case WAIT:
                break;
        }
        return random(210, 305); //The amount of time in milliseconds before the loop starts over
    }

    @Override

    public void onPaint(Graphics2D g) {

        //This is where you will put your code for paint(s)

    }

}

This is what it probably was intended to be by you?

Edited by luminlumin
Link to comment
Share on other sites

5 hours ago, sp3cpk said:

what do you mean by "script does nothing"

Be specific, what area is your character in? what exactly do you have in your inventory.

 

3 hours ago, HunterRS said:

 

He is right, we will need more information about what is wrong in order to help you

It just does nothing. I play it from the OSBot menu and it starts then stops. Script does nothing. I'm fairly certain I started the script with an empty inventory, so it should have started the banking sequence and went on. It's possible I had something in my inventory, and looking at the code now it appears it will only move with a full or empty inventory.

 

 

3 hours ago, Apaec said:

I'd recommend starting on something much simpler, and working your way up. A good example would be woodcutting; first make a script that can chop a tree with a paint as well, then add dropping to it. Then make it bank the logs instead of dropping them, and then maybe add some options in a gui - it's all stuff to learn, but the key is to get the basics down before getting too involved!

If you have any questions, don't hesitate to ask me, i'll do the best to help out as best I can. Beyond that, the best way of learning is trying so you're certainly on the right track!

Good luck!

Apa

 

Thanks Apaec, it's good getting some feedback from you considering your sandcrab script is one of the most used in my library. I'm going to put this project on pause for the night and do what you suggest. I'll get a good grip of the fundamentals and continue to work my way up.

 

3 hours ago, ZX700 said:

it wont start because of the line


Position CowPosition = (Position) myPosition();

 

its probably that the client needs to be ingame before you can get the position of the player

also 


if (Banks.LUMBRIDGE_UPPER.contains(myPosition()));

you should remove the ;

 

 

and instead of 


import java.awt.geom.Area;

import javax.swing.text.Position;

you need to 

 


import org.osbot.rs07.api.map.Area;
import org.osbot.rs07.api.map.Position;

Thank you, I will make the changes tonight. I noticed the first line of code you're referring to earlier, I must have left that there on accident when I was trying to figure out the proper usage of positions.

  • Like 1
Link to comment
Share on other sites

15 hours ago, luminlumin said:

should really be 


import org.osbot.rs07.api.map.Area;

 

like the poster above me said.

 

also, if statements should be scoped,  and its a common beginner mistake to add a semicolon to your if statement but you should avoid doing that.

 

and 1 more thing


if (inventory.isFull())
			return State.GO_TO_COWS;

is the same as saying

if inventory is full, go to cows.

it should be


!inventory.isFull()

which translates to not inventory is full

 


package core;

import org.osbot.rs07.api.map.Area;
import org.osbot.rs07.api.map.Position;
import org.osbot.rs07.api.map.constants.Banks;
import org.osbot.rs07.api.model.RS2Object;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;

import java.awt.*;

import static org.osbot.rs07.api.map.constants.Banks.LUMBRIDGE_UPPER;

@ScriptManifest(name = "Skeleton", author = "Alek", version = 1.0, info = "", logo = "") 

public class Skeleton extends Script {

    @Override

    public void onStart() {

    }

    private enum State {
        GO_TO_BANK, WITHDRAW_FOOD, GO_TO_COWS, KILL_COW, WAIT
    }

    public static final Area BANK_AREA = Banks.LUMBRIDGE_UPPER;
    Position CowPosition = (Position) myPosition();

    private State getState() {
        RS2Object cow = getObjects().closest("Cow");
        if (inventory.isEmpty()) { //no food
            return State.GO_TO_BANK;
        }
        if (LUMBRIDGE_UPPER.contains(myPosition())) {
            return State.WITHDRAW_FOOD;
        }
        if (!inventory.isFull()) {
            return State.GO_TO_COWS;
        }
        if (inventory.contains(329)) {
            return State.KILL_COW;
        }
        return State.WAIT;
    }


    @Override

    public void onExit() {

        //Code here will execute after the script ends

    }
    @Override
    public int onLoop() throws InterruptedException {
        switch (getState()) {
            case GO_TO_BANK:
                getWalking().webWalk(BANK_AREA); //walk to lumby bank
                break;
            case WITHDRAW_FOOD:
                npcs.closest("Banker").interact("Bank"); //withdraw food from lumbridge bank booth
                break;
            case GO_TO_COWS:
                Position pos1 = new Position(3259, 3269, 0);
                getWalking().walk(pos1); //walk to cows position
                break;
            case KILL_COW:
                npcs.closest("Cow").interact("Attack");//get cow npc and kill, cow 2805-2809
                break;
            case WAIT:
                break;
        }
        return random(210, 305); //The amount of time in milliseconds before the loop starts over
    }

    @Override

    public void onPaint(Graphics2D g) {

        //This is where you will put your code for paint(s)

    }

}

This is what it probably was intended to be by you?

Almost missed your post! I'll be looking at the script tomorrow evening. Hopefully with the support from you all we can get a basic script up and running.

Regardless, I'm going to brush up on Java with some YouTube, Pluralsight, and eBooks. I'll be doing some scripting on the side. I hope to slowly show improvement and one day release a premium or VIP level script.

Link to comment
Share on other sites

34 minutes ago, EPLS said:

Almost missed your post! I'll be looking at the script tomorrow evening. Hopefully with the support from you all we can get a basic script up and running.

Regardless, I'm going to brush up on Java with some YouTube, Pluralsight, and eBooks. I'll be doing some scripting on the side. I hope to slowly show improvement and one day release a premium or VIP level script.

No problem, and another thing, after looking at your code, in your withdrawing from bank case, there is no code to actually withdraw anything, it's only going to open the bank window, and your kill cow will keep spam clicking because there is no checks for different scenario.

 

Best of luck on your journey to learn Java, dude!

Link to comment
Share on other sites

Whoever keeps telling new scripters to use states - stop it.

You've made this new guy's script so fucking unreadable and overly complicated that I would be aghast if he can even get it to work.

States are horrible! Stop using them!

Here's what I can do without states; compare for yourself:

String status = "Ready"

Position cowTile = new Position(3259, 3269, 0);
NPC banker;
NPC cow;

@Override
public void onLoop() throws InterruptedException {
	if (shouldGoToBank()) {
		goToBank();
	} else if (shouldWithdrawFood()) {
		withdrawFood();
	} else if (shouldGoToCows()) {
		goToCows();
	} else if (shouldKillCows()) {
		killCows();
	}
}

private boolean shouldGoBank() {
	return inventory.isEmpty();
}

private void goToBank() {
	status = "Going to the bank";
	getWalking().webWalk(Banks.LUMBRIDGE_UPPER);
}

private boolean shouldWithdrawFood() {
	return Banks.LUMBRIDGE_UPPER.contains(myPosition());
}

private void withdrawFood() {
	status = "Withdrawing food";
	banker = npcs.closest("Banker");
	if (banker != null && banker.interact("Bank")) {
		// now sleep until the bank interface opens
		// hint: https://osbot.org/api/org/osbot/rs07/utility/ConditionalSleep.html
	}
}

private boolean shouldGoToCows() {
	return inventory.isFull();
}

private void goToCows() {
	status = "Going to the cows";
	getWalking().walk(cowTile);
}

private boolean shouldKillCows() {
	return inventory.contains("Salmon");
}

private void killCows() {
	status = "Killing cows";
	cow = npcs.closest("Cow"); // better filtering needed - you might attack a cow someone else is already attacking, or you might be fighting a cow and attempt to attack another cow in non-multi zone
	if (cow != null && cow.interact("Attack")) {
		// now sleep until you've killed the cow or are running low on health
		// hint: https://osbot.org/api/org/osbot/rs07/utility/ConditionalSleep.html
	}
}

 

  • Like 1
Link to comment
Share on other sites

38 minutes ago, liverare said:

Whoever keeps telling new scripters to use states - stop it.

You've made this new guy's script so fucking unreadable and overly complicated that I would be aghast if he can even get it to work.

States are horrible! Stop using them!

Here's what I can do without states; compare for yourself:


String status = "Ready"

Position cowTile = new Position(3259, 3269, 0);
NPC banker;
NPC cow;

@Override
public void onLoop() throws InterruptedException {
	if (shouldGoToBank()) {
		goToBank();
	} else if (shouldWithdrawFood()) {
		withdrawFood();
	} else if (shouldGoToCows()) {
		goToCows();
	} else if (shouldKillCows()) {
		killCows();
	}
}

private boolean shouldGoBank() {
	return inventory.isEmpty();
}

private void goToBank() {
	status = "Going to the bank";
	getWalking().webWalk(Banks.LUMBRIDGE_UPPER);
}

private boolean shouldWithdrawFood() {
	return Banks.LUMBRIDGE_UPPER.contains(myPosition());
}

private void withdrawFood() {
	status = "Withdrawing food";
	banker = npcs.closest("Banker");
	if (banker != null && banker.interact("Bank")) {
		// now sleep until the bank interface opens
		// hint: https://osbot.org/api/org/osbot/rs07/utility/ConditionalSleep.html
	}
}

private boolean shouldGoToCows() {
	return inventory.isFull();
}

private void goToCows() {
	status = "Going to the cows";
	getWalking().walk(cowTile);
}

private boolean shouldKillCows() {
	return inventory.contains("Salmon");
}

private void killCows() {
	status = "Killing cows";
	cow = npcs.closest("Cow"); // better filtering needed - you might attack a cow someone else is already attacking, or you might be fighting a cow and attempt to attack another cow in non-multi zone
	if (cow != null && cow.interact("Attack")) {
		// now sleep until you've killed the cow or are running low on health
		// hint: https://osbot.org/api/org/osbot/rs07/utility/ConditionalSleep.html
	}
}

 

What style do you recommend for bigger scripts? Task based?

Edited by Jammer
Link to comment
Share on other sites

1 hour ago, Jammer said:

What style do you recommend for bigger scripts? Task based?

Bigger scripts have bigger moving parts, which can be broken down into APIs and additional classes.

The loop function should only contain a series of checks and calls, like I've demonstrated, so anyone can easily analyse what the script does and where problems are likely occurring.

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