detecting click on godot
hey! i'm short on time, so, let's get to the point...
to detect a click on godot, i had to do a few things:
consider you have a root node in a scene, and consider this scene is a simple object you will have on screen, like the miners in this game
this root node extends KinematicBody2D, in my case
inside the node root node, i have added a Sprite node named "sprite" (this name is important, keep it in mind), and a CollisionShape2D, as you can see below
having a collision-type node as a child of the root node, now you have to set a property in the root, called "Pickable", set it to "On"
since the miner is now pickable, let's code its script (more explanation right after)
func _input(event): if event is InputEventMouseButton and event.pressed: if $sprite.global_position.distance_to(get_global_mouse_position()) < $sprite.texture.get_width() / 2: open_popup() # or do whatever you want with the click else: close_popup() #the click is not on the image
the _input(event) function can be "overriden" by any node
if it's a mouse input, and the mouse button is being pressed (any button goes), then we have to check if the mouse is where we want to detect the click
in this case, i'm checking if the mouse position is inside the sprite
remember the name "sprite"? in godot, using $<name>, just like $sprite above, is the quickest way to reference a child node by name when you're in the root note script
the function get_global_mouse_position has the mouse position, and $sprite also has a position, so...
checking the distance between them solves the question...
if the distance is shorter than half the image width (the image position is its center), you can deduce the mouse is over the image
this works for square images only
i'm pretty sure there's a better way to check this, how about commenting here?
bots in mars
you just landed on mars, with the goal to replicate your ship, and a few starting resources
Status | Prototype |
Author | Thiago Hilger |
Genre | Strategy |
More posts
- silicon and iron working :)Feb 07, 2023
- restarted from scratch!Jan 25, 2023
- bots reworked! navigation reworked and failed! yayJan 10, 2023
- bots will be reworkedOct 07, 2022
- working prototype!Jul 14, 2022
- new bots are spawning again!Jun 30, 2022
- bots and modules are connectedJun 20, 2022
- reskin and refactorMay 28, 2022
- new minersMay 04, 2022
Leave a comment
Log in with itch.io to leave a comment.