Jump to content

Finding the object that you clicked


Recommended Posts

Posted

Can you explain why you need this?

 

I'm making a mining script and I'm storing the current rock that is mined, so that I could later check if it still contains ore (to stop animating and go to next rock). The problem is when I enable input and click on a random rock myself the script stops working after the rock has been mined. I want to determine on what rock (or on what location) I've clicked and do something accordingly.

Posted

I'm making a mining script and I'm storing the current rock that is mined, so that I could later check if it still contains ore (to stop animating and go to next rock). The problem is when I enable input and click on a random rock myself the script stops working after the rock has been mined. I want to determine on what rock (or on what location) I've clicked and do something accordingly.

 

When you interact with the rock you have an instance of the object.

You can store the position and see if the rock is still a valid mineable rock?

Posted

When you interact with the rock you have an instance of the object.

You can store the position and see if the rock is still a valid mineable rock?

 

Nah, that doesn't work or I don't know how to check that. The object that I've interacted with somehow is still visible and existing. The new no-ore rock is a new object with a different ID. So what I do is I store the objects' that I've interacted with position and then scan for rocks that contain no ore. If the rocks' with no ore position matches the the rocks' position that I'm interacting then I now that the rock was mined.

Posted

Nah, that doesn't work or I don't know how to check that. The object that I've interacted with somehow is still visible and existing. The new no-ore rock is a new object with a different ID. So what I do is I store the objects' that I've interacted with position and then scan for rocks that contain no ore. If the rocks' with no ore position matches the the rocks' position that I'm interacting then I now that the rock was mined.

 

You need to regrab the object on the position you store. You do not store the RS2Object.

 

you can get an updated version of the current object on a tile using Objects.get(x,y).get(0);

  • Like 2
Posted

You need to regrab the object on the position you store. You do not store the RS2Object.

 

you can get an updated version of the current object on a tile using Objects.get(x,y).get(0);

Thanks. Will make the code much cleaner. But what does the get(0) do? 

 

And back to the original question. The mouse class has a method getPosition(), but I'm pretty sure it returns the coordinates of your window. Any way to get a runescape map position from those mouse coordinates?

Posted (edited)

Thanks. Will make the code much cleaner. But what does the get(0) do? 

 

Objects.get() returns an array of objects at the position, .get(0) returns the first one and usually there is only one objects per tile.

 

 

And back to the original question. The mouse class has a method getPosition(), but I'm pretty sure it returns the coordinates of your window. Any way to get a runescape map position from those mouse coordinates?

 

You could take an area around the player in a eg 10x10 grid and check if the mouse click or press event's .getPoint() is within the bounds of a tile.

(Do not check every tile loaded, 10x10 is already a significant load)

 

Idk if this is the best way to approach it but i know it works

 

Useful snippets:

ArrayList<Position> nearby_positions = new ArrayList<Position>();

private void updateNearbyPositions(int scan_radius) {
		Position my_position = myPosition();
		int start_x = my_position.getX() - scan_radius;
		int start_y = my_position.getY() - scan_radius;
		int end_x = my_position.getX() + scan_radius;
		int end_y = my_position.getY() + scan_radius;
		for (int xx = start_x; xx <= end_x; xx++) {
			for (int yy = start_y; yy <= end_y; yy++) {
				Position pp = new Position(xx, yy, my_position.getZ());
				nearby_positions.add(pp);
			}
		}
	}
@[member=Override]
	public void mouseClicked(MouseEvent e) {

		for (Position p : nearby_positions) {
			if (p.isVisible(getBot()) && p.getPolygon(getBot()).contains(e.getPoint())) {
				if (mining_rock_positions.containsKey(p)) {
					mining_rock_positions.remove(p);
					e.consume();
				} else if (!mining_rock_positions.containsKey(p)) {
					RS2Object rock = getObjects().get(p.getX(), p.getY()).get(0);
					if (rock != null) {
						mining_rock_positions.put(p, rock.getId());
						e.consume();
					}
				}
			}
		}

	}

Apologies for the structure, its a pretty old code of mine feels.png

Edited by House
Posted

Objects.get() returns an array of objects at the position, .get(0) returns the first one and usually there is only one objects per tile.

 

 

You could take an area around the player in a eg 10x10 grid and check if the mouse click or press event's .getPoint() is within the bounds of a tile.

(Do not check every tile loaded, 10x10 is already a significant load)

 

Idk if this is the best way to approach it but i know it works

 

Useful snippets:

ArrayList<Position> nearby_positions = new ArrayList<Position>();

private void updateNearbyPositions(int scan_radius) {
		Position my_position = myPosition();
		int start_x = my_position.getX() - scan_radius;
		int start_y = my_position.getY() - scan_radius;
		int end_x = my_position.getX() + scan_radius;
		int end_y = my_position.getY() + scan_radius;
		for (int xx = start_x; xx <= end_x; xx++) {
			for (int yy = start_y; yy <= end_y; yy++) {
				Position pp = new Position(xx, yy, my_position.getZ());
				nearby_positions.add(pp);
			}
		}
	}
@[member='Override']
	public void mouseClicked(MouseEvent e) {

		for (Position p : nearby_positions) {
			if (p.isVisible(getBot()) && p.getPolygon(getBot()).contains(e.getPoint())) {
				if (mining_rock_positions.containsKey(p)) {
					mining_rock_positions.remove(p);
					e.consume();
				} else if (!mining_rock_positions.containsKey(p)) {
					RS2Object rock = getObjects().get(p.getX(), p.getY()).get(0);
					if (rock != null) {
						mining_rock_positions.put(p, rock.getId());
						e.consume();
					}
				}
			}
		}

	}

Apologies for the structure, its a pretty old code of mine feels.png

 

 

Thanks! Extremely useful. Just to clarify on this bit: 

if (mining_rock_positions.containsKey(p)) {
					mining_rock_positions.remove(p);
					e.consume();
				} else if (!mining_rock_positions.containsKey(p)) {
					RS2Object rock = getObjects().get(p.getX(), p.getY()).get(0);
					if (rock != null) {
						mining_rock_positions.put(p, rock.getId());
						e.consume();
					}
				}

You populate a HashMap of rock positions in the area where you are and then add/remove the rocks with ores in the list? Though this snippet is only for updating the rock map that the user manually clicked on, cause I believe mouseListener does not catch client "mouse clicks".  Am I right?

Posted

You populate a HashMap of rock positions in the area where you are and then add/remove the rocks with ores in the list? Though this snippet is only for updating the rock map that the user manually clicked on, cause I believe mouseListener does not catch client "mouse clicks".  Am I right?

 

Im not exactly sure what you are asking sorry lol.

It stores the rocks that are clicked in a hashmap with the key being the rocks position and the value being the rocks id at the selection time (the rock is selected when it has ore in it)

 

And if i understand what you are saying then why are you trying to listen to mouse clicks by the client mouse method.

All you need is the rs2object it is interacting with (clicking) to know the position the script "clicked on".

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