0 votes

Situation:
I have a few Texturerects, each parented to their own Node2d node.
By using;

move_child(node, get_child_count()-1) 

in the root node, I can change the order in witch they are drawn.
But if I try to select them with the mouse, strange things happen: sometimes a texture
that is drawn in front, behaves as if it is further back.
Now, if I parent them to a Control node in stead of a Node2D, the behavior is,
as I would expect.
Question:
Why?

in Engine by (50 points)

1 Answer

0 votes

The reason for this behavior (for a Control node) is that those nodes are related to the user interface. TextureRect is meant to take user interface input (e.g. mouse clicks) while Node2D is a general node meant for anything related to the CanvasItem node. Since Control nodes include responding to the mouse, it may be preferable to use those nodes.

by (2,064 points)

Thanks for the quick response.
I get what you're saying, but still:
In both cases the TextureRects are drawn in the right order (as in the order of the parents in the tree).
So why is the input-order wrong? (what matters the parent?)

Maybe it would help if I saw your node tree, or the code you were using.

Setup:
one owner:node2D
with several children:Node2D
each with a TextureRect added.
the script for the TextureRect is shared between all texturerects.

The owner:Node2D

extends Node2D

func moveit(node):
    move_child(node, get_child_count()-1)

The Texturerect:

extends TextureRect


signal move

func _ready():
    connect("move", get_owner(), "moveit")
var drag_pos:Vector2
var dragging

func _on_Menu_gui_input(event) -> void:
    var mouse_pos:Vector2
    mouse_pos= get_global_mouse_position()
    if event is InputEventMouseButton:
    if  event.pressed and event.button_index == BUTTON_LEFT:
                   emit_signal("move",get_parent())
            drag_pos= mouse_pos - get_parent().get_position()
            dragging=1
        else:
            dragging = 0
    elif event is InputEventMouseMotion:
        if dragging and Input.is_mouse_button_pressed(BUTTON_LEFT)  :
            get_parent().set_position(get_global_mouse_position()-drag_pos)

Some of the windows you can click 'through', as if they are in the back.
If you change the owner:node in a Control:node it works as expected.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.