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

Godot 4.4中get_tree().change_scene_to_file无法切换场景求助

Godot 4.4 场景切换失败问题解决
  • 核心问题:按钮信号回调函数签名错误
    Button节点的pressed信号是无参数的,但你的_on_button_pressed函数声明了event : InputEvent参数,这会导致Godot无法正确匹配信号回调,点击按钮时这个函数根本不会执行。

  • 修复步骤

    1. 修改回调函数的签名,去掉多余的参数:
      func _on_button_pressed() -> void:
          # 使用你导出的scenePath变量,避免硬编码路径出错
          get_tree().change_scene_to_file(scenePath)
      
    2. 确保按钮的pressed信号已正确绑定到这个函数:
      • 在编辑器中选中Button节点,打开信号面板,找到pressed信号,拖拽到脚本所在节点,选择_on_button_pressed函数完成绑定。
      • 或者在_ready()中用代码绑定:
        func _ready():
            start.grab_focus()
            start.pressed.connect(_on_button_pressed)
        
    3. 处理_process的警告:
      由于你没用到delta参数,按照提示改成_delta即可:
      func _process(_delta):
          pass
      
    4. 验证场景路径:
      确保@export_file("scene.tscn")导出的scenePath已经正确选择了目标场景文件,避免路径拼写错误或文件不存在。
  • 修改后的完整代码

extends Control

@export var button: Button
@export_file("scene.tscn") var scenePath : String

@onready var start = $Button

func _ready():
    start.grab_focus()
    # 可选:代码绑定信号,确保不会漏绑
    start.pressed.connect(_on_button_pressed)

func _process(_delta):
    pass

func _on_button_pressed() -> void:
    # 使用导出的路径变量,更灵活且不易出错
    get_tree().change_scene_to_file(scenePath)

func get_scene() -> PackedScene:
    return load(scenePath)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.12 12:42:43