You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Godot 4.0中GDScript类与Node信号连接后函数不执行问题

问题分析与解决方案

从你的代码和调试输出来看,信号已成功连接且触发,但目标函数未执行,主要排查以下几点:

1. 显式声明信号

Godot 4.0要求发射信号前必须在脚本顶部先声明信号。在Player类脚本开头添加:

signal remove_dash

2. 修正代码缩进错误

你的Player类check_for_dash_input函数缩进混乱,会导致逻辑执行异常,修正后代码如下:

func check_for_dash_input(delta, player_resource: Resource, dash_timer: Timer, input: String, dash_power: int):
    dash(delta, player_resource, dash_timer, dash_power)
    
    if Input.is_action_just_pressed(input):
        dash_timer.start()
        emit_signal("remove_dash")
        print("signal emit")

(原代码中if语句缩进层级错误,即便输出了signal emit,也可能是缩进错误引发的意外执行,修正后才能保证逻辑按预期触发)

3. 确认信号连接的对象正确性

Character2D脚本中的player变量需确保指向当前Player实例。如果Player类继承自Character2D,父类脚本中应使用self指代自身,修改连接代码为:

func _ready():
    self.remove_dash.connect(_remove_all_dash)
    print("is connected: " + str(self.remove_dash.is_connected(_remove_all_dash)))

(若player是场景中引用的节点,需检查节点路径是否正确,避免引用空对象或错误实例)

4. 排查变量作用域问题

Character2D中的dash_power变量需确保是父类可访问的全局变量,避免Player类中使用的dash_power参数覆盖父类变量。如果需要修改父类的dash_power,Player类无需将其作为参数传递,直接访问父类变量即可。

内容的提问来源于stack exchange,提问作者MyBoi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 18:23:11