InputEventMouseMotion + button overlaying (picture included)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By quizzcode

Hello there,
I have a button that activate the build UI of my tower defense game.
I also use InputEventMouseMotion to see where i will be placing the tower and if you can place the tower. (checking the tile_id).

If I place the tower on a tile_id that ids’t buildable : I start the function : _off_build()
Which closes the Build UI and reset it.

Clicking on my button activate UI, but clicking on it again, turn it off too.

Ok so now you know what my button and ui does. Here is the issue.
The InputEventMouseMotion react to the clic even tho the button is on top of it.
(see attached picture) !

enter image description here

Here is my code, any idea ? thank you very much in advance.

func _input(event):
if build_mode == true:
	if event is InputEventMouseButton:
		var pos = event.position
		if event.pressed:
			#release clic 
			var id_tile = map.get_cellv(current_tile)
			if can_build == true:
				if id_tile == 5: #we can build
					#get info from the tower and the cost for it.
					if tower_class.tower_cost <= player_gold: #player still has enough money to pay for it.
				#Not on a buildable tile.
				#reset UI building and buttons
:bust_in_silhouette: Reply From: Andrea
func _unhandled_input(event):

should make the trick.
proper event handling is not as straightforward as it seems, i suggest to read the doc about it

Hey Andrea, thank you for your input.
So I read the doc and watch a few tutorials from GDquest & co.

This would word to disactivate the input from on or an other.
But in this case i need to be able to place elements on the map as well as clicking back on the buttons that is on top of the map.

So. isn’t _unhandled_input taking in consideration only one at a time ? Making it not suitable ? Or did I misunderstand it ?

thank you !

quizzcode | 2021-08-29 15:30

I don’t like it and feels a bit hacky.
But since my camera and interface never move. And the map under the button is never used in the game…
I simply added a “if” position of the mouse is within the map space.

if mouse_position.y < 680 and mouse_position.y > 104 and mouse_position.x < 467 and mouse_position.x > 15:

bit ugly tbh. But does the job !

quizzcode | 2021-08-29 15:41

I’m not sure what you mean with disactivate…
You have to imagine every object that can interact with events (suck as mouse click, key stroke, etc) like something that use the event and then “consume” it.
Once consumed, the event generated is destroyed.
So basically: one event, one use.

Since there are many things that can consume an event (e.g buttons, or other pieces of code), Godot gives you an order on which objects are checked for consumption (see the list in the documentation i shared).

_input(event) is the higher level, which means that if the function inside intercept the mouse click, it will not be used by anything else, not even buttons.

Unhanlded input is the lower, it takes events that have not being used by anything else (including buttons), and check if they can still be of use.

Hope it is clearer now

Andrea | 2021-08-29 16:04

God. you make so much more sense then these tutorials.
Thank you very much - gonna give it a shot !

I will handle my map clic with unhandled, making normal clic single used on what ever is on top of it. Love it

quizzcode | 2021-08-29 16:07