1

I have a program which uses a TextureRect for a mouse cursor, and a home-rolled focus system. It works fine, but in _ready I set the global_position of the mouse to the focused control, and a problem manifests.

When it's called in _ready, it jumps to (0, -610); but if I await a timer set to 0.025 seconds, and then assign it, it (correctly) jumps to (0, 560). Checking the position of the control I'm jumping the cursor over shows that these are the global_position values of the control at the respective times.

I would think I could just call_deferred on the function that moves the mouse, and it would resolve it; but I still get the incorrect (0, -610) coordinate even then.

What is giving me this strange behavior? My current solution feels very much like a hack and I would like to do something else, or await a signal, which would do it more cleanly. It's a bit of an eyesore, and I do not know what it is that I am waiting on. At what point is it safe to set the global_position of one control to another?

I am running in Full Screen Exclusive Mode (but wish to support windowed as well), and my control is in a CanvasLayer. The root control is maximized vertically and horizontally, and is a base control under the CanvasLayer.

2
  • 1
    in which ready function do you try to set the global_position? How does the tree look like?
    – Bugfish
    Commented Jun 17 at 6:58
  • The Mouse texture itself, which is a first-generation child of the root Control node and at the very end of the list—ah, I think you just solved it for me. I'll add a response below to clarify for everyone else. Commented Jun 17 at 10:30

1 Answer 1

1

OK, here was my issue.

On entering the scene tree, two functions are called; first, _enter_tree, and then later, _ready. They are also associated with signals. _ready does not imply that my data and metadata (like my focus list) has been adequately updated or is correct, and some things like positions may not be correct; but by setting certain things during the _enter_tree signal, I can ensure that.

So, I tied the enter-tree signal of my Mouse texture to a function which updates my focus list and then moves the mouse, like so:

func _on_mouse_tree_entered() -> void:
    $Mouse.focus_list = focus_list
    $Mouse.move_mouse_to_focus()

Where all move_mouse_to_focus does is update the displayed position of the mouse to be over that specific focus coordinate. As long as it's done with an event callback, instead of in _ready, everything works fine in all window modes.

1
  • Note that for containers made visible for the first time, all you need to do is call your texture-moving function from the sort_children callback. At that point, the child nodes should be in their final position. No need to await an arbitrary amount of time. Commented Jun 17 at 11:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.