How to get the actual/current size of an on-screen Control Node?

Godot Version

4.1.1

Question

Essentially, I have a Marker2D which is a child of a custom Control node (to be clear, none of the additional code modifies the positioning or sizing behaviour), which is itself a child of a Container. I want to be able to update the position of the Marker2D based on the size of it’s parent Control, which will be variable as the Container may contain more than one of these custom Control nodes, and it shares a screen with other variably sized Controls. However, it seems that the Size property of the parent Control doesn’t get updated during runtime, even though the onscreen elements show it has been resized. How do I access the current size of a dynamically resized Control node during Runtime?

I found this similar post:

The title seems to be asking the same question I am, but the solution provided doesn’t translate to my situation.

I have tested this, but can’t replicate the problem.

With this script it works for me and in every frame the correct size of the parent control node is printed:

extends Marker2D

func _process(delta):
	var parent: Control = get_parent()
	position = parent.size
	print (position)

Since there are so many possibilities, where this can go wrong, it would help, if you could share the code, you are using.

1 Like

While writing my reply to this, I realized the problem only occurred when I was calling for it in _Ready(). I thought I checked this with breakpoints in VSCode but turns out I had been looking at the wrong node’s size the whole time. Thanks for your help.

Yes, during _ready, the size of Control nodes is not yet accessible.

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