Search code examples
godotgdscriptgodot4

Godot Parse Error: expected string constant as 'preload' argument


Error: expected string constant as 'preload' argument

--- Debugging process started ---
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

--- Debugging process stopped ---
 res://scripts/test.gd:66 - Parse Error: expected string constant as 'preload' argument.

Code Snippet

# this is use for set backgound phato
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()

Make sure to review line 66 in your actual code and ensure that the 'preload' argument is a string constant as the error suggests. If you need further assistance, you can provide the relevant code snippet around line 66 for more detailed help.

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")  # Replace with your actual image path


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

# Window initialization
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")

# Label initialization
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)

# Button initialization
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

# Window size change handler
func _on_size_changed():
    center_label()

# Center the 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)  # Set a z-index higher than the background


# this is use for set backgound phato
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()



# Button pressed handler
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  # Handle unexpected button_id values if necessary

# Update label with mouse position
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)

What I Tried and Expected

I attempted to set a background photo for the scene's background, but encountered errors in the process.

I expected the background image to be set without any issues.


Solution

  • The preload will be resolved when parsing the script, and the resource will be loaded along with the script... Which is before the script gets to run, and thus before Godot knows the value of your variable.

    If you need to load Resources dynamically use load or use ResourceLoader. Godot will use a cache so it does not have to load the same Resource more times than necessary. Be aware that if the Resource was freed between calls, Godot will load it again (just to be clear: it will be a different instance in this case).

    If you want to skip the cache, you can use ResourceLoader.load which lets you specify CacheMode.CACHE_MODE_IGNORE.