Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nodes deleted in a scene unexpectedly remain when referencing them with a tool script. #22105

Open
ghost opened this issue Sep 15, 2018 · 5 comments

Comments

@ghost
Copy link

ghost commented Sep 15, 2018

EDIT / UPDATE

As mentioned below, this is most certainly for undo purposes, and is_inside_treeO is at least a method of detecting this, if used carefully. There are situations where you might pluck stuff out of the tree temporarily for your own reasons, so it might be lead to some collisions.

Maybe there is some way to make it more intuitive by default, better error messages, or a very specific editor script method.
...

Original

Alpha v3.1 4547e22

Example Project: Tool Deleted Nodes Leak.zip

When deleting a node in the editor, tool scripts that have a reference to a node in the scene tree retain that object. Not only that, it is completely accessible.

This is very tricky to work around when making tools that need to know when objects are deleted.

The $ and get_node will not find them.

This is what happens when it is deleted. Left is the variable and the right is the get_node() result.
console

@vnen
Copy link
Member

vnen commented Sep 16, 2018

Wild guess: nodes are kept in memory for the undo operation. Since the variable contains a direct pointer, it can still be accessed.

@ghost
Copy link
Author

ghost commented Sep 16, 2018

@vnen That makes sense, but I must say it does make things quite difficulty in tool code for detecting when nodes have been deleted out of the scene tree.

Would you have any suggestions for code like this? What can I use in place of is_instance_valid() to know when a node has dropped out of the scene, because all the properties seem to be intact, including it's parent. (As you say, probably for undo purposes.)

tool
extends Node2D

onready var node = $my_node

func _physics_process(delta):

	if(is_instance_valid(node)):
		node.do_stuff()

	else:
		do_other_stuff()

Presently, there is a lot of odds side stuff I have to do to be able to try and catch this, and sometimes there are really odd timing issues when processing loops are needed. Right now for me it is leading towards dozens of lines of copy and paste code per tool script, that is potentially buggy and unreliable when mixed with signals/yields/defers/processing.

@vnen
Copy link
Member

vnen commented Sep 16, 2018

What can I use in place of is_instance_valid() to know when a node has dropped out of the scene

There is Node.is_inside_tree(), which should be false at this point.

@ghost
Copy link
Author

ghost commented Sep 16, 2018

Bailing me out yet again tonight. XD

Whew, glad there was something.

I gave up checking properties and methods too soon on it, it is at least out of the tree. A small thing, but will make life easier going forward.

Going to try to update the title here of this thread to be more precise.

@ghost ghost changed the title Possible Node Leak in Tool Script Nodes deleted in a scene unexpectedly remain when referencing them with a tool script. Sep 16, 2018
@akien-mga
Copy link
Member

The issue is still reproducible in 3.3, the error message changed a bit:

[Sprite:18258]  [Sprite:18258]
ERROR: get_node: (Node not found: "DELETE IN EDITOR" (relative to "/root/EditorNode/@@581/@@582/@@590/@@592/@@596/@@600/@@601/@@602/@@618/@@619/@@628/@@629/@@6071/@@5910/@@5911/@@5912/@@5913/@@5914/issue").)
   At: scene/main/node.cpp:1322.
[Sprite:18258]  [Object:null]

I'm not sure if anything can be done about it though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants