I’m doing the Complete Godot 2D course and exploring a little. The Shooting Rockets lecture describes loading a rocket scene. I thought I’d try loading asynchronously but I think I may be misunderstanding how it works. Any advice on why this doesn’t work would be greatly appreciated.
const ROCKET_SCENE_FILE: String = "res://scenes/rocket.tscn"
func _ready():
ResourceLoader.load_threaded_request(ROCKET_SCENE_FILE) # asynchronously load the rocket scene
func fire_rocket() -> void:
var rocket_loading_status: ResourceLoader.ThreadLoadStatus = ResourceLoader.load_threaded_get_status(ROCKET_SCENE_FILE)
if rocket_loading_status == ResourceLoader.THREAD_LOAD_LOADED:
var rocket_scene: Resource = ResourceLoader.load_threaded_get(ROCKET_SCENE_FILE) # get the loaded scene
var rocket_instance: Node = rocket_scene.instantiate() # instance the scene
add_child(rocket_instance) # add to this scene
else:
assert(rocket_loading_status == ResourceLoader.THREAD_LOAD_IN_PROGRESS, "FAULT failure loading rocket resource: ResourceLoader.ThreadLoadStatus = " + str(rocket_loading_status))
func _process(delta: float) -> void:
if _handle_actions:
_process_actions() # read input and set state/triggers
if _shoot == true:
fire_rocket()
_shoot = false # reset the shoot trigger
In _ready() the resource loading is initiated. In _process(), fire_rocket() is called if the shoot button is pressed [some code omitted]. In fire_rocket() loading status is retrieved and if loaded, the rocket is instantiated. If not loaded and the loading status is showing an error the game exceptions.
The first time the rocket is loaded this works. The second time I get an error with the status being 0, which means “The resource is invalid, or has not been loaded with load_threaded_request().”.
I thought that the resource is cached by default - so I should be able to reuse it without loading again. Am I mistaken or doing this wrong or is it something else?
Update: I got the code to work by storing the rocket scene in a global variable. However, I thought it would be kept in the resource loader so I shouldn’t need to do that.
Thanks.