重命名节点时调用功能?

发布于 2025-01-25 14:35:03 字数 79 浏览 3 评论 0原文

非常简单的Q,当节点重命名时,如何调用功能?

我认为可能会调用_SET()函数,但它不起作用用于重命名

pretty straight forward Q, How do I invoke a function when the node is renamed?

I thought maybe _set() function would be invoked but it doesn't work for renaming

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

她比我温柔 2025-02-01 14:35:03

从编辑器旁路设置名称 _SET()。您可以使用_SET()从代码设置name时进行拦截。


另一个选项是连接“重命名”信号(所有node s都有它)。

您可以在_Init的情况下尽早连接,如果您希望在任何代码进行任何node的任何代码之前都可以更改其名称。

但是,这仅在场景树内的节点时触发。


最后,一种检测名称 更改的方法,无论更改如何是聆听notification_path_changed Notification。

您在_notification默认情况下获得notification_path_changed ,无需启用它或类似的东西。

节点不在场景树中时,它甚至可以工作,我发现这令人惊讶。

更令人惊讶的是,它在将节点添加到场景树时不会触发,或将其删除(这些是notification_enter_treenotification_exit_tree分别) ,尽管这些动作改变了节点的路径。因此,我相信notification_path_changed是错误的称呼,应该是“ notification_name_changed”。

附录:在Godot 4上,它被更名为notification_path_renamed

Setting the name from the editor bypasses _set(). You can use _set() for intercepting when you set the name from code.


Another option is to connect the "Renamed" signal (all Nodes have it).

You can connect as early as _init if you want it to happen before whatever code is instancing the Node has a chance to change its name.

However, this one only triggers if the Node is inside the scene tree.


And, finally, a way to detect changes of name regardless of how the change happens is to listen for the NOTIFICATION_PATH_CHANGED notification.

You get NOTIFICATION_PATH_CHANGED in _notification by default, no need to enable it or anything like that.

It even works when the Node is not in the scene tree, which I find surprising.

Even more surprising, it does not trigger when adding the Node to the scene tree, or removing it (those would be NOTIFICATION_ENTER_TREE and NOTIFICATION_EXIT_TREE respectively), despite those actions changing the path the node. So I believe NOTIFICATION_PATH_CHANGED is a misnomer, it should be "NOTIFICATION_NAME_CHANGED".

Addendum: On Godot 4 it was renamed to NOTIFICATION_PATH_RENAMED.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文