ResourceLoader issue after Godot automatically updated my game to 4.0

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

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 :slight_smile:

:bust_in_silhouette: Reply From: Enfyna

ResourceLoader — Godot Engine (stable) documentation in English

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 ( ResourceLoader — Godot Engine (stable) documentation in English ). 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.

1 Like

Review the parts of your code where the ResourceLoader is used. Ensure it aligns with the updated API changes in Godot 4.0. Sometimes, small adjustments in function names or parameters