Check if the mouse is over gui?

Godot Version

4.5.1 stable

Question

How would I make this code not fire if the mouse is clicking over a gui element currently it will just try place it anyway

func _process(delta):
	if Input.is_action_just_pressed("place_tower"):
		if ResourceManager.Packets >= 50:
			ResourceManager.remove_packets(50)
			var mouse_pos = get_global_mouse_position()
			place_tower(mouse_pos, Globals.current_selection)
			add_buffer(mouse_pos)
		else:
			print("Not enough money")

also the place_tower action is just left click

preferably without accessing every gui node or connecting signals like that

Please help!!!

Don’t check for events in _process() but instead overwrite the _input() method for the object that should watch out for the mouse clicks.

This isnt the most effective to do this by any means, but in the root node, I created a variable that when GUI is clicked, it is set to true (interact=true). All other nodes that rely on clicking are disabled until the interaction is complete. Relatively new to actual coding software.

The simplest solution is to handle mouse clicks in _unhandled_input().
When you click on a control node with mouse_filter set to stop or pass, the input is consumed before it can reach unhandled_input.

3 Likes

thanks! I stuck with a bypass solution as I didn’t want to use this at first as I couldn’t get it to work in the past but after I started having trouble with the bypass I tried it again and it works beautifully!