Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mouse position does not work in @tool scripts #60390

Closed
KoBeWi opened this issue Apr 20, 2022 · 4 comments · Fixed by #71768
Closed

Mouse position does not work in @tool scripts #60390

KoBeWi opened this issue Apr 20, 2022 · 4 comments · Fixed by #71768

Comments

@KoBeWi
Copy link
Member

KoBeWi commented Apr 20, 2022

Godot version

f4b0c7a

System information

Windows 10 x64

Issue description

Attach this script to any Node2D on a scene:

@tool
extends Node2D

func _physics_process(delta: float) -> void:
	print(get_local_mouse_position())

Reload scene. It will always print (0, 0).

EDIT:
This is related to the fact that the nodes on scene are inside a SubViewport. I was able to get mouse position from the main viewport and use transform magic to get what I wanted.

Steps to reproduce

See above.

Minimal reproduction project

No response

@akien-mga
Copy link
Member

Is this still reproducible in the latest 4.0 betas?

@KoBeWi
Copy link
Member Author

KoBeWi commented Jan 12, 2023

Yes.

@FeralBytes
Copy link
Contributor

@KoBeWi How did you do this: "This is related to the fact that the nodes on scene are inside a SubViewport. I was able to get mouse position from the main viewport and use transform magic to get what I wanted."
Also this also applies if the tool tries for get_global_mouse_position().
Edit I figured it out, I needed to do this:

get_tree().get_root().get_mouse_position()

@KoBeWi
Copy link
Member Author

KoBeWi commented Jan 17, 2023

How did you do this

root is the scene root node.

func get_local_mouse_workaround(root: Node2D) -> Vector2:
	var container: SubViewportContainer = root.get_parent().get_parent()
	var viewport := root.get_viewport()
	var screen_mouse := container.get_local_mouse_position() / viewport.global_canvas_transform.get_scale()
	var translation := viewport.global_canvas_transform.get_origin() / viewport.global_canvas_transform.get_scale()
	return screen_mouse - translation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
3 participants