0 votes

Background: I have a node that extends Node2D that's meant to have custom position snapping in the 2D scene editor. I use an editor plugin to control how the node is positioned after it's dragged with the mouse or nudged with the arrow keys. I'm now trying to figure out how to control the node's position during editing when one is added to the scene by dragging it from the FileSystem window to the 2D scene editor.

I'm trying to get a node's position when it's initialized in the editor by drag-and-drop. I've tried accessing it in the node's _ready and in _enter_tree functions (with the node's script set to tool mode) but these seem to be called before you've finally dropped the node into the scene, so those functions always report (0, 0). I've also tried using an editor plugin that handles the node's class, but none of its functions seem to be called when the node is first initialized.

How else may I access the node's final position on drag-and-drop initialization, if possible?

in Engine by (116 points)

1 Answer

0 votes
Best answer

I would have thought _ready() would have worked in your case, which implies that the editor sets the position of the node AFTER adding it to the tree, which is not ideal and basically prevents what you are trying to do.

You could open an issue on Github to discuss about ways to do this properly, but in the meantime perhaps you could try this:

func _ready():
    if Engine.editor_hint:
        call_deferred("snap")

func snap():
    # Do your positionning here
by (27,593 points)
selected by

If this doesn't work, you might be able to yield to a short timer you create in _ready before snapping, but that seems like a very very very un-ideal solution.

Using call_deferred in my node's _ready function solved my issue. Thank you.

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.