Skip to content

Properly show detach script button when script is added via inspector #108122

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

Merged
merged 1 commit into from
Jul 18, 2025

Conversation

Kazuo256
Copy link
Contributor

@Kazuo256 Kazuo256 commented Jun 29, 2025

Fixes #107059.

The SceneTreeDock was not tracking script changes in selected nodes in any capacity as far as I could assess. To do
that, my solution essentially connects the "script_changed" signal from selected nodes to
SceneTreeDock::_update_script_button() whenever the selection changes. It actually queues the update to make sure it
happens only once no matter how many nodes are selected.

However, only connecting that signal would leave previously selected nodes with a signal connection that should no
longer exist. To properly disconnect previously selected nodes, we have to store the list of currently selected nodes so
we can disconnect them when the selection changes.

The PR also replaces all uses of SceneTreeDock::_update_script_button() with the queued alternative, besides other minor minor improvements requested in the review. See commit message for more details.

@Kazuo256 Kazuo256 requested a review from a team as a code owner June 29, 2025 19:19
@Kazuo256 Kazuo256 changed the title Show detach script button when added via inspector Properly show detach script button when script is added via inspector Jun 30, 2025
@Chaosus Chaosus added this to the 4.5 milestone Jun 30, 2025
@KoBeWi
Copy link
Member

KoBeWi commented Jul 3, 2025

You can't just connect script_changed to _update_script_button(), because it will update the button for every selected node. It should happen only once. You can add something like _queue_update_script_button().

@Kazuo256
Copy link
Contributor Author

Kazuo256 commented Jul 8, 2025

You can't just connect script_changed to _update_script_button(), because it will update the button for every selected node. It should happen only once. You can add something like _queue_update_script_button().

Makes sense. Do you have a suggestion for where to check for queued updates? My first guess would under the NOTIFICATION_PROCESS case in the _notification() method.

@KoBeWi
Copy link
Member

KoBeWi commented Jul 8, 2025

The usual pattern is

void queue_update() {
	if (is_update_queued) {
		return
	}
	is_update_queued = true
	call_deferred("actual_update")
}

@Kazuo256 Kazuo256 force-pushed the fix-107059 branch 2 times, most recently from 5b07c6e to a00caa9 Compare July 9, 2025 04:38
@Kazuo256
Copy link
Contributor Author

Kazuo256 commented Jul 9, 2025

I've implemented the requested changes. One thing I realized is that clicking the script button in the scene tree dock to remove the script will call _update_script_button twice (regardless of how many nodes are selected). One because of the callback I added to the script_changed signal, and another as part of the usual logic of the remove script button. Is that OK or should I also adjust the script button to queue the update?

@KoBeWi
Copy link
Member

KoBeWi commented Jul 9, 2025

should I also adjust the script button to queue the update?

Now that we have a queue method, it can be used more. So yes.

@Kazuo256
Copy link
Contributor Author

Kazuo256 commented Jul 15, 2025

Replaced all SceneTreeDock::_update_script_button() calls with the new queued version. See commit message for more details.

I chose to divide the PR's work into three commit since they were separate atomic changes that someone might want to cherry-pick/revert/etc independently, but I can squash them together if that is preferrable.

@Repiteo
Copy link
Contributor

Repiteo commented Jul 17, 2025

Could you squash the commits? See our PR workflow documentation for details: https://docs.godotengine.org/en/latest/contributing/workflow/pr_workflow.html#the-interactive-rebase

@Kazuo256
Copy link
Contributor Author

Could you squash the commits? See our PR workflow documentation for details: https://docs.godotengine.org/en/latest/contributing/workflow/pr_workflow.html#the-interactive-rebase

No problem, I'm aware of the workflow. Just to be sure, I'll reiterate my last comment, but will squash as asked:

I chose to divide the PR's work into three commit since they were separate atomic changes that someone might want to cherry-pick/revert/etc independently, but I can squash them together if that is preferable.

Fixes godotengine#107059.

The SceneTreeDock was not tracking script changes in selected nodes in any capacity as far as I could assess. To do
that, my solution essentially connects the "script_changed" signal from selected nodes to
"SceneTreeDock::_update_script_button()" whenever the selection changes. It actually queues the update to make sure it
happens only once no matter how many nodes are selected.

However, only connecting that signal would leave previously selected nodes with a signal connection that should no
longer exist. To properly disconnect previously selected nodes, we have to store the list of currently selected nodes so
we can disconnect them when the selection changes.

The commit also includes some improvements to the SceneTreeDock class:

1. Remove unnecessary initialization in SceneTreeDock

This field is already initialized in the line that declares it. As such, initializing it on the constructor as well as
is redundant.

2. Queue script button updates in scene tree dock

Since we now have the option to defer the script button update and make sure it only runs once per frame, it's always
best to use the queued version of the update from a performance perspective. I'm not entirely sure if there could be any
unexpected side effects but it is a minor self-contained UI update, so it's likely a relatively safe change.

The replacement includes the bindings since it is a requirement for the other replacements in the class to work
(UndoRedo needs their method names registered in the class DB). It should be OK to remove the old non-queued bindings
too even though they are accessible in the public API because it is a "unofficial" method starting with an underscore.
@Repiteo Repiteo merged commit b3c7beb into godotengine:master Jul 18, 2025
20 checks passed
@Repiteo
Copy link
Contributor

Repiteo commented Jul 18, 2025

Thanks!

@xuwaters
Copy link

When closing the scene tab, the pointers in LocalVector<Node *> node_previous_selection becomes dangling pointers, which causes random crash.

@Kazuo256 Kazuo256 deleted the fix-107059 branch July 20, 2025 02:06
@Kazuo256
Copy link
Contributor Author

When closing the scene tab, the pointers in LocalVector<Node *> node_previous_selection becomes dangling pointers, which causes random crash.

I'll work on a fix. Is there an issue open yet?

@xuwaters
Copy link

Thank you, opened an issue: #108783

@Kazuo256
Copy link
Contributor Author

PR to fix the crash: #108785

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

Successfully merging this pull request may close these issues.

The editor does not update the assigned script status
5 participants