重命名节点时调用功能?
非常简单的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从编辑器旁路设置
名称
_SET()
。您可以使用_SET()
从代码设置name
时进行拦截。另一个选项是连接
“重命名”
信号(所有node
s都有它)。您可以在
_Init
的情况下尽早连接,如果您希望在任何代码进行任何node
的任何代码之前都可以更改其名称。但是,这仅在场景树内的
节点
时触发。最后,一种检测
名称
更改的方法,无论更改如何是聆听notification_path_changed
Notification。您在
_notification
默认情况下获得notification_path_changed
,无需启用它或类似的东西。当
节点
不在场景树中时,它甚至可以工作,我发现这令人惊讶。更令人惊讶的是,它在将
节点
添加到场景树时不会触发,或将其删除(这些是notification_enter_tree
和notification_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 thename
from code.Another option is to connect the
"Renamed"
signal (allNode
s have it).You can connect as early as
_init
if you want it to happen before whatever code is instancing theNode
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 theNOTIFICATION_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 beNOTIFICATION_ENTER_TREE
andNOTIFICATION_EXIT_TREE
respectively), despite those actions changing the path the node. So I believeNOTIFICATION_PATH_CHANGED
is a misnomer, it should be "NOTIFICATION_NAME_CHANGED".Addendum: On Godot 4 it was renamed to
NOTIFICATION_PATH_RENAMED
.