The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

The Issue
so i noticed a lil bit ago my project automatically updated to Godot 4, without me actually touching it. and ever since that, it didn't even update my code, so there were numerous errors. and while i've managed to fix most of them, i'm having a problem with my ResourceLoader-based scene changing script.

The Code

extends Node

@export var maxLoadTime = 15000

func gotoScene(nextPath, currentScene):
var loader = ResourceLoader.load_threaded_request(nextPath)

var loadIcon = load("res://Scenes/Menus/LoadingIcon.tscn").instance()
get_tree().get_root().call_deferred("add_child", loadIcon)

var curLoadTime = Time.get_ticks_msec()

if loader == null:
    print("Resource Loader failed to load resource @ " + nextPath)
    return

while Time.get_ticks_msec() - curLoadTime < maxLoadTime:
    var prog = loader.poll()

    if prog == ERR_FILE_EOF:
        var scene = loader.load_threaded_get()
        get_tree().get_root().call_deferred("add_child", scene.instance())
        loadIcon.queue_free()
        currentScene.queue_free()
        break

    elif prog == OK:
        var loadProg = float(loader.get_stage()) / loader.get_stage_count()
        loadIcon.value = loadProg * 100

    else:
        print("There was an unexpected error loading file @ " + nextPath)

    await(get_tree().process_frame)

Lines with Errors
the particular lines that show errors are:

var prog = loader.poll()

(Above gives "Cannot call function on enum value")

var scene = loader.load_threaded_get()

and:

var loadProg = float(loader.get_stage()) / loader.get_stage_count()

i've looked everywhere and cannot even find a solution to the first issue. and i'm talking godot documentation, this very q&a forum or even reddit (it was my last try).

If anyone could help in any way, i would really appreciate it as fixing this code is crucial for me to get the project back to full functionality again :)

Godot version 4.1
in Engine by (21 points)

1 Answer

0 votes

https://docs.godotengine.org/en/stable/classes/class_resourceloader.html

ResourceLoader changed so looking at the documentation could help.

For your errors ResourceLoader.load_threaded_request(nextPath) this doesnt return a loader object it only returns a error code. If you want to get the load status later you can call it like this :
var status = ResourceLoader.load_threaded_get_status(nextPath)
will return a ThreadLoadStatus enum ( https://docs.godotengine.org/en/stable/classes/class_resourceloader.html#enum-resourceloader-threadloadstatus ). Basically if this value is 3 it is loaded.

poll() isnt needed anymore just check status.

To get the resource you call ResourceLoader.load_threaded_get(nextPath). I think you are loading a node so you would probably do it like this :
var scene = ResourceLoader.load_threaded_get(nextPath).instantiate()

To get the percentage progress you can use this again :
var status = ResourceLoader.load_threaded_get_status(nextPath,[])
when you pass a array as a second argument it returns a array with the progress value like [0.77] so if you do : print(status[0]) it will print 0.77.

by (600 points)
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 Frequently asked questions and 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 [email protected] with your username.