• Godot Help
  • "size_changed" signal will not connect for the life of me

I have a grid that runs a function to scale/position any new elements offset to the center of the viewport/window. If I change the window size while the program is running, such as maximizing, suddenly any new elements are offset offscreen or just in a place they are not supposed to be. I'm attempting to check when the window is resized and then loop through the grid's array and update all of their positions based on the new viewport/window size but I cannot get the signals to emit correctly.
I've tried these three options (separately):
get_window().size_changed.connect(Callable(self, "window_size_changed"))
get_tree().get_root().size_changed.connect(Callable(self, "window_size_changed"))
get_viewport().connect("size_changed", window_size_changed)

and none of them throw errors but none of them emit a signal when the size changes. I've tried force emitting the signal (using emit_signal) and the function works properly and prints my debug message.

This might not even be the right way to fix the positioning issue I'm having, but I am for sure misunderstanding something here

    two_teaspoons If I change the window size while the program is running, such as maximizing, suddenly any new elements are offset offscreen or just in a place they are not supposed to be.

    One of the options is to check the window size in each frame and calculate the position of elements based on it. If there are a lot of elements, this is not the best solution, but if there are not a lot of calculations, it is quite acceptable.

    two_teaspoons size_changed is not a signal of the Window class. It belongs to Window's parent class Viewport and it will happen only when viewport's pixel size changes. If you have the stretch mode in your project settings set to viewport, resizing the window will actually never change viewport's pixels size. It'll merely stretches the pixels. Hence, the signal won't fire. Try changing the stretch mode to canvas item and see if the signal gets triggered.

      xyz This makes sense. I actually started a separate project to figure out some things and I realized my issue: My camera wasn't centered on my viewport. I didn't have something to parent it to so I just sorta forgot to make sure it was positioned properly, and then I changed recentering functions to accommodate for that not realizing the camera was what was offset. I'm assuming the centering function is working fine, it's just that maximizing the window messes with some sort of coords that look weird because of how offset my camera was. I'm not entirely sure how that makes sense if the viewport should stay the same size in-engine (you are correct that I'm on viewport stretch mode), but I got it working in a different project using the camera correctly so I'm happy and free lol.

      two_teaspoons Try connecting to resized signal of Viewport node: get_viewport().connect("resized", self, "_on_Viewport_resized").