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

Godot中剩余冷却时间为何同时为0和非0?代码异常求助

Godot冷却计时器异常:剩余冷却时间同时呈现0和非0状态

问题说明

在Godot中实现冷却计时器功能时遇到异常:旧项目里完全相同的代码可正常运行,将旧项目的同款场景导入新项目也没问题,但把函数复制粘贴到新项目的新场景后,剩余冷却时间(remaining_cooldown)出现同时为0和非0的矛盾状态。

相关代码

func updateLabel(label: Label, cooldown: float, current_time: float, last_time: float) -> void:
    var elapsed_time: float = current_time - last_time
    var remaining_cooldown: float = max(0.0, cooldown - elapsed_time)
    print("remaining_cooldown:", remaining_cooldown)
    if remaining_cooldown!= 0:
        label.text = str(remaining_cooldown)
    else:
        label.text = ""

排查方向

  • 浮点数精度误差:Godot的浮点数计算存在精度偏差,remaining_cooldown可能是极小的非0值(如1e-10),打印时被近似显示为0,但!=0判断仍会返回真。可改为判断remaining_cooldown > 0.01这类阈值来规避。
  • 参数传递异常:检查调用updateLabel时传入的current_time和last_time是否正确,比如新项目是否混用了不同时间源(如OS.get_ticks_msec()和Time.get_ticks_msec()),或last_time未正确更新导致elapsed_time计算异常。
  • 节点引用问题:确认新场景中的Label节点是否正确关联,是否存在多个同名节点,导致打印结果和显示的Label不是同一对象。
  • 函数重复调用:排查是否存在多次调用updateLabel的情况,一次计算出0,一次计算出非0,造成打印与显示结果不一致。

内容的提问来源于stack exchange,提问作者Reincarnated as a worldbuilder

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 12:22:05