HOW I CAN SOLVE THIS PROBLEM

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Kao_Shinpa

In my game I have a “welcome” scene that allows you to enter the “Main Scene”
The problem is that when I start the game I get this

Parser Error: Can’t preload resource at path: res://Escenas/Main.tscn

(This is my Code)

func _on_Play_pressed():
var game = preload(“res://Escenas/Main.tscn”).instance()
get_tree().get_root().add_child(game)
hide()

The function “_on_Played_pressed():” is from a button

THX

:bust_in_silhouette: Reply From: Roshi

Did you try to use load instead of preload ?
Or maybe this solution:

get_tree().change_scene("res://Escenas/Main.tscn")

THX
I will try it

Kao_Shinpa | 2021-04-05 23:58

:bust_in_silhouette: Reply From: Robo0890

There is a better way to do this with a loading screen, just in case the scene takes a while to load. You don’t want people playing your game and think that it crashed!

var loading_target_value = 0

onready var buttons = $UI/Buttons #This would be the parent node to your welcome screen
onready var loading = $UI/Loading #The parent node to a ProgressBar node
onready var loading_progress = $UI/Loading/ProgressBar #ProgressBar node

func interactive_load(loader):
print("starting...")
loading.show()
buttons.hide()
while true:
    var status = loader.poll()
    if status == OK:
        loading_target_value = (loader.get_stage() * 100) / loader.get_stage_count()
        continue
    elif status == ERR_FILE_EOF:
        loading_target_value = 100
        loading_done_timer.start()
        break
    else:
        print("Error while loading level: " + str(status))
        buttons.show()
        loading.hide()
        break


func loading_done(loader):
    loading_thread.wait_to_finish()
    replace_main_scene(loader.get_resource())
    res_loader = null
    # Weirdly, "res_loader = null" is needed as otherwise
    # loading the resource again is not possible.
    print("done")


func replace_main_scene(resource):
    change_scene(resource)


func change_scene(resource : Resource):
    var node = resource.instance()
    for child in get_children():
        remove_child(child)
        child.queue_free()
    add_child(node)




func _process(_delta):
    if loading_progress != null:
        loading_progress.value = lerp(loading_progress.value,loading_target_value,.1) + 1


func onPlaypressed():
    var path = "res://Escenas/Main.tscn" #The scene you want to load
    res_loader = ResourceLoader.load_interactive(path)
    loading_thread = Thread.new()
    #warning-ignore:return_value_discarded
    loading_thread.start(self, "interactive_load", res_loader)


func _on_LoadTimer_timeout():
    loading_done(res_loader)

It worked really well for my project, especially since the scene I’m loading is REALLY big.

THX
I will try it

Kao_Shinpa | 2021-04-22 17:52