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

Godot引擎preload参数报错:期望传入字符串常量

Godot 3.5.2 背景图片设置报错:preload参数需为字符串常量

错误详情

--- 调试进程已启动 ---
Godot Engine v3.5.2.stable.custom_build - https://godotengine.org
OpenGL ES 3.0 Renderer: Mesa Intel(R) HD Graphics 620 (KBL GT2)
Async. shader compilation: OFF

--- 调试进程已停止 ---
 res://scripts/test.gd:66 - 解析错误:preload参数需为字符串常量。

问题代码片段

错误出现在initialize_background函数中,使用变量作为preload的参数:

# 用于设置背景图片
func initialize_background(image_path: String):
    var background = TextureRect.new()
    texture = preload(image_path) # 触发错误的代码行
    background.texture = texture
    background.rect_min_size = get_viewport_rect().size
    add_child(background)
    background.raise()

完整代码

extends Node2D

var label: Label
var button1: Button
var button2: Button
var button3: Button
var texture : Texture


func _ready():
    initialize_window()
    initialize_label("Hello, Godot!", Vector2(500, 200), Label.ALIGN_CENTER)
    initialize_buttons()
    initialize_background("res://icon.png")  # 替换为实际图片路径


func _process(delta: float) -> void:
    update_label_with_mouse()

# 窗口初始化
func initialize_window():
    OS.set_window_resizable(false)
    OS.set_window_minimized(false)
    OS.set_window_maximized(false)
    OS.set_window_title("NOSTALGIA WARP WORLD")

# 标签初始化
func initialize_label(text: String, min_size: Vector2, alignment: int):
    label = Label.new()
    label.text = text
    label.rect_min_size = min_size
    label.align = alignment
    center_label()
    add_child(label)

# 按钮初始化
func initialize_buttons():
    button1 = create_button("Button 1", [1], -50)
    button2 = create_button("Button 2", [2], 0)
    button3 = create_button("Button 3", [3], 50)

func create_button(text: String, arguments: Array, y_offset: float) -> Button:
    var button = Button.new()
    button.text = text
    button.rect_min_size = Vector2(150, 30)
    button.connect("pressed", self, "_on_button_pressed", arguments)
    button.rect_position.x = (get_viewport_rect().size.x - button.rect_min_size.x) / 2
    button.rect_position.y = (get_viewport_rect().size.y - button.rect_min_size.y) / 2 + y_offset
    add_child(button)
    return button

# 窗口尺寸变化处理
func _on_size_changed():
    center_label()

# 居中标签
func center_label():
    label.rect_position.x = (get_viewport_rect().size.x - label.rect_min_size.x) / 2
    label.rect_position.y = (get_viewport_rect().size.y - label.rect_min_size.y) / 2
    label.set_z_index(1)  # 设置比背景更高的z-index


# 用于设置背景图片
func initialize_background(image_path: String):
    var background = TextureRect.new()
    texture = preload(image_path)
    background.texture = texture
    background.rect_min_size = get_viewport_rect().size
    add_child(background)
    background.raise()



# 按钮点击处理
func _on_button_pressed(button_id):
    match button_id:
        1:
            label.text = "Button 1 Pressed!"
        2:
            label.text = "Button 2 Pressed!"
        3:
            label.text = "Button 3 Pressed!"
        _:
            pass  # 必要时处理意外的button_id值

# 更新标签显示鼠标位置
func update_label_with_mouse():
    var mouse_position = get_global_mouse_position()
    var mouse_text = "Mouse X: " + str(mouse_position.x) + "Mouse Y: " + str(mouse_position.y)
    # label.text = mouse_text
    # center_label()
    print(mouse_text)

错误原因

Godot中的preload()是编译期加载函数,它会在项目编译阶段提前加载指定资源,因此要求参数必须是硬编码的字符串常量,无法接受运行时动态传入的变量作为路径参数。

解决方法

方法1:改用load()支持动态路径

如果需要保留动态传入路径的功能,使用load()函数替代preload()。load()是运行时加载函数,可以接受变量作为参数:

func initialize_background(image_path: String):
    var background = TextureRect.new()
    texture = load(image_path)
    # 可选:添加加载失败的判断逻辑
    if not texture:
        print(f"背景图片加载失败:{image_path} 不存在或路径错误")
        return
    background.texture = texture
    background.rect_min_size = get_viewport_rect().size
    add_child(background)
    background.raise()

方法2:使用字符串常量配合preload()

如果背景图片路径固定,不需要动态切换,直接将路径写为字符串常量传入preload(),同时可以简化函数参数:

# 修改函数,不需要传入路径参数
func initialize_background():
    var background = TextureRect.new()
    texture = preload("res://icon.png") # 硬编码常量路径
    background.texture = texture
    background.rect_min_size = get_viewport_rect().size
    add_child(background)
    background.raise()

# 调用时直接执行函数
func _ready():
    # ...其他初始化代码
    initialize_background()

内容的提问来源于stack exchange,提问作者MD. SABBIR HOSHN HOWLADR

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 13:20:59