If you are making a GUI with Control Nodes, then you don't find the center. You're not allowed to manually position objects. However, you can use Controls like a VBox or HBox or CenterBox and use the layout controls to center objects (or move them to the corners, etc.) The options are on the top above the viewport, it looks like a square.
How to get the center of the screen?
- Edited
And then he solves it in 5 seconds after that and facepalms been there.
Terrestroid_Developer-Official I have done every tutorial that exists out there
wwaaaooooo !! Amazing ! I guess you're crazy as hell to inflict so much pain to yourself
It would be great to have a definitive guide to UI and containers in Godot 4 because documentation is far too light on this and devs made so much changes to this part. In fact, it's quite a complete mess and anchors behaviour with content of a container works very weirdly, for instance a NinePatchRect does not auto adjust its size despite its content (another container) is bigger, whereas it looks so easy when everything is set-up correctly like in this GDQuest vdeo.
- Edited
In a 2D project in Godot 3.5.2, I use this to get the center as a Vector2:
get_viewport_rect().size / 2.0
It's only used for positioning my player, who remains at the center.
I forgot to mention that I am using Godot 3, so I have now updated my post to include that.
- Edited
DaveTheCoder How are you able to use "get_viewport_rect()", it always returns "isn't declared in current class"?
What is your exact Godot version? There are many releases that start with "3".
DaveTheCoder Godot_v3.5.1
- Edited
Terrestroid_Developer-Official How are you able to use "get_viewport_rect()", it always returns "isn't declared in current class"?
It's a method of CanvasItem ("Base class of anything 2D"), so it should work in any script that's attached to a node that inherits that class, which includes anything that's displayable as 2D, which a GUI would normally be.
https://docs.godotengine.org/en/3.5/classes/class_canvasitem.html?highlight=get_viewport_rect#class-canvasitem-method-get-viewport-rect
- Edited
JusTiCe8 This is why I am only gonna switch to Godot 4 for a new game project after I finish my current game, and by the time that happens, Godot 4 will hopefully be as fleshed out as Godot 3.
DaveTheCoder Ah, now it works, but it produces really weird results which, for your information, appear the same across both windows and mobile devices (I tested this).
Anchors and stuff didn't do anything to fix this. Here are screenshots of what I am talking about (the center node is supposed to be that big placeholder astronaut character. The darkened astronaut is the player and that other darkened astronaut is just a player duplicate that I was just fooling around with):
This was the code I used for it (I took the "get_viewport_center" function from someone else's answer since it seemed to work better than just the plain "get_viewport_rect().size / 2" which returned results that were out of the screen):
func _process(_delta):
rect_position = _get_viewport_center()
func _get_viewport_center() -> Vector2:
var transform : Transform2D = get_viewport_transform()
var scale : Vector2 = transform.get_scale()
return -transform.origin / scale + get_viewport_rect().size / scale / 2
And now to wait for answers.
cybereality I am now starting to wonder if there is anything in project settings that could be the possible source of this whole mess...
You might want to read this:
https://docs.godotengine.org/en/stable/tutorials/rendering/multiple_resolutions.html