Attention: Here be dragons

This is the latest (unstable) version of this documentation, which may document features not available in or compatible with released stable versions of Godot.

实例化与信号

信号提供了一种解耦游戏对象的方式, 让你避免强行强制固定节点排列. 当使用 get_parent() 时, 你会发现可能需要调用信号的一个标志, 直接引用一个节点的父节点意味着你不能轻易地将该节点移动到场景树的另一个位置. 当你在运行时实例化对象, 并且可能想把它们放在运行中的场景树的任意位置时, 这可能是个问题.

下面我们将考虑这种情况的一个例子: 发射子弹.

射击示例

考虑一个可以旋转并向鼠标射击的玩家角色. 每次点击鼠标按钮, 我们就在玩家的位置创建一个子弹的实例. 详见 创建实例 .

我们用 Area2D 来表示子弹, 它以给定的速度直线运动:

extends Area2D

var velocity = Vector2.ZERO

func _physics_process(delta):
    position += velocity * delta

然而, 如果子弹作为游戏角色的子节点添加, 那么当它旋转时, 子弹将仍然保持 "附着" 在游戏角色身上:

../../_images/signals_shoot1.gif

相反, 我们需要子弹独立于游戏角色的移动——一旦发射, 子弹将持续沿着直线运动, 游戏角色就不能再影响它们了. 与其作为游戏角色的子节点被添加到场景树中, 不如将子弹作为 "主" 游戏场景的子节点添加上去更有意义, 后者可能是游戏角色的父节点, 甚至可能是更高层级的树.

你可以通过将子弹直接添加到主场景中来做到这一点:

var bullet_instance = Bullet.instantiate()
get_parent().add_child(bullet_instance)

然而, 这将导致一个不同的问题. 现在, 如果你试图独立测试你的 "Player" 场景, 它将在射击时崩溃, 因为没有父节点可以访问. 这使得独立测试你的游戏角色代码变得更加困难, 也意味着如果你决定改变你的主场景的节点结构, 游戏角色的父节点可能不再是接收子弹的适当节点.

解决这个问题的方法是使用一个信号来 "发射" 游戏角色的子弹. 游戏角色不需要 "知道" 子弹在那之后发生了什么——任何连接到信号的节点都可以 "接收" 子弹并采取适当的行动来产生它们.

下面是游戏角色使用信号发射子弹的代码:

extends Sprite2D

signal shoot(bullet, direction, location)

var Bullet = preload("res://bullet.tscn")

func _input(event):
    if event is InputEventMouseButton:
        if event.button_index == MOUSE_BUTTON_LEFT and event.pressed:
            shoot.emit(Bullet, rotation, position)

func _process(delta):
    look_at(get_global_mouse_position())

In the main scene, we then connect the player's signal (it will appear in the "Node" tab of the Inspector)

func _on_player_shoot(Bullet, direction, location):
    var spawned_bullet = Bullet.instantiate()
    add_child(spawned_bullet)
    spawned_bullet.rotation = direction
    spawned_bullet.position = location
    spawned_bullet.velocity = spawned_bullet.velocity.rotated(direction)

现在子弹将自行运行,独立于游戏角色的旋转:

../../_images/signals_shoot2.gif