Jump to content

Finding the object that you clicked


Butters
 Share

Recommended Posts

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

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?

Link to comment
Share on other sites

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

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?

Link to comment
Share on other sites

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

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.

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.

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...