+1 vote

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 onPlaypressed():
var game = preload("res://Escenas/Main.tscn").instance()
get
tree().getroot().addchild(game)
hide()

The function "onPlayed_pressed():" is from a button

THX

Godot version v3.2.3.stable.official
in Engine by (30 points)
recategorized by

2 Answers

+1 vote
Best answer

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

get_tree().change_scene("res://Escenas/Main.tscn")
by (81 points)
selected by

THX
I will try it

+1 vote

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.

by (16 points)

THX
I will try it

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.