连接没有节点和通过 child 节点的信号 - Godot

Connecting signals without node and through node of a child - Godot

到目前为止我一直在尝试做的事情 -

我有一个名为 recipes 的 class 单例脚本。我在里面有一个信号launched。我有objects的recipeclass。 objects 之一是 noodles。我想将信号 launched 连接到 noodles 中一个名为 after_launch 的函数。我不确定将信号连接到哪个节点。我正在使用 class,因为我有多个 recipes,每个都有自己的 launched 信号。

我还想知道如何在单例脚本中将信号连接到函数 - 完全没有节点。

我一直在使用的代码 -

connect("launched", self, "launched_noodle")

我明白使用 "connect" 而不使用它前面的节点会出错。 错误 -

error(3,1) Unexpected token Identifier:connect

我该如何 -

1) 将其连接到写入代码的节点 - w.r.t recipesnoodles。您通常将信号连接到哪些节点?

2) 当我只想将它连接到 Singleton 脚本时连接。

我试图 Google 解决这个问题并遇到 this question on Godot Forum 类似的问题。我不明白完整的问题或答案。或许对你有帮助可以参考。

附加信息 -

我的代码class(单例)recipe

extends Node2D
class_name recipe
signal launched

noodles 和其他 children 的代码 -

extends recipe
connect("launched", self, "launched_noodle")

代码的流程-

用户触发 cooking station 并且 cooking station 节点在屏幕上打开。

之后,检查用户的级别和其他参数,并在屏幕上显示适当的recipe。这个 recipe 是一个已定义的 class - 一个 Godot 单例 - 并且在 cooking station

下链接了许多物理节点

一旦适当的节点被定位并显示在屏幕上,我想启动的 signal 应该由 cooking station 发出。 signal - 根据发出它的节点,在屏幕上进行某些更改。那是通过一个名为 launched_recipename

的函数

出现错误Unexpected token Identifier:connect是因为connect是一个函数,所以需要在另一个函数的范围内调用它,比如_ready。根据您描述的流程,听起来 launched 应该是 烹饪台 中定义的信号,而不是 recipe,因此您可能会遇到这样的情况:

  • CookingStation(定义信号launched
    • 面条(定义函数_on_launched

Noodles.gd中:

extends Recipe

func _on_launched():
    print("Noodles was launched")

func _ready():
    var cooking_station = get_parent()
    cooking_station.connect("launched", self, "_on_launched")  

这里真的不需要单例。如果您认为您需要一个 Singleton 以便 Noodles 可以继承自 Recipe,则没有必要。只需添加 class_name Recipe 指令即可将 Recipe 置于全局范围内,以便其他脚本可以继承它。