Search code examples
user-interfaceonclick2dgame-developmentgodot

How to get UIs elments' position (or space) in Godot 4.0


I am making a game with Godot 4.0.stable where you can move characters around a 2D Top Down planet by clicking on the position you wish the character to move to, just like in Rimworld.

I would like to implement UI elements such as a button to switch scene, a fast foward, a pause etc...

However my issue is that when I click on UI elements it moves the player to the coordinate under the UI element.

For example here : https://prnt.sc/0VtHloCDBNiO The red character is chosen, when I click on the top left button the function associate to the button is used but the player will still moves under the button. I would like to know if there is a way to do somehting like :

If Input.is_action_just_pressed("left_click") and click_position != UI_elements_space:
    #move the character
else:
    #dont move the character

I tried to get every UI elements position and multiply by them by their size so I can add it so the condition. But I have to implement every new element of the UI this condition, which is pretty tedious and not clean.

I know there is a better and much easier way to do it. I just don't know how...

Any help is appreciated!


Solution

  • Refactor your project so you:

    And then the Controls will consume the input before it gets to the game world.

    In fact, the issue most people have with pointing input is the opposite as yours: that the Control is preventing their game world from getting it. Which you can handle with mouse_filter.

    See also: Using InputEvent.