Make Window Node Undraggable

Godot Version

4.2.2

Question

So I want to make the Window node undraggable.

I first tried to look if inside the inspector there is a flag for that (like for making it unresizable) but I did not find anything.

So then I tried doing something like this:

func _input(event): if event is InputEventMouseButton: return (pseudo-code)

hoping that it would “consume” the event and it wouldn’t be processed from the GUI, but no luck.

Then I tried doing func _process: if start_pos != pos: pos = start_pos (pseudo-code)

but once I wanted to have window’s position not absolute (to have it in center of the screen in fullscreen and windowed mode) it snapped it back to the windowed start_pos (even when starting with fullscreen mode from settings) so it wasn’t viable

Searching the subreddit and forum I did not find anything too, except one that said to use Control node instead

So does anybody knows a way to do this?

Thank you everybody in advance (sorry for my english)

You could make it not resizable and have a position variable. Using the second piece of code, you can keep the window’s position at the position variable’s location. Then you can use this code to set the variable to the center of the screen:

@onready var window = $Window #The location of the window node in the scene tree

var position = Vector2(0, 0) #Set to a default value

func _process(_delta:float) -> void:
    position = get_window().size / 2.0 #get the window that the node running this code is in and find the center of it

    #You can replace the window variable with a reference to the window node, or just change the variable
    if not position == window.position:
        #You don't even need the if, although it *might* make it a little faster.
        #It could also make it a little slower though
        window.position = position

NOTE: This code is untested. Use at your own risk.

1 Like

Testing code, thank you for taking the time to reply :smile:

1 Like

I changed your solution slightly and it works now :smile: thank you so much!

Changes for anybody with the same problem in the future:

var win_position: Vector2i = get_tree().get_root().size / 2.0
win_position.x -= size.x / 2
win_position.y -= size.y / 2

Thank you so much for your time and effort!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.