Trying to load SpriteFrames manually in script

Godot Version

Godot 4.2.1

Question

Hello! New to godot so if there’s an easier way to do what I’m trying to do, please let me know :slight_smile:
Will include code snippet here. The short of it is that I’m trying to have it so that the scene only ever needs to have one character portrait node (3dspriteanimation) at a time, and can load different SpriteFrames as needed. This is my attempt, after finding another tutorial on a similar topic:

func setPortrait(tempName):
	# see if we even need to run this
	if characterName != tempName:
		characterName = tempName
	else:
		return
	# get our spriteframes
	tempName = tempName.to_lower()
	var path = ''.join([spriteFoldersLocation,tempName,'/',tempName,'.tres'])
	var preloader = ResourcePreloader.new()
	var frames = preloader.get_resource(path)
	print(frames)
	self.set_sprite_frames(frames)
	print(characterName + " " + path)

However, I get the following error when the game tries to load the portraits. Additionally, frames prints as a null object.

E 0:00:00:0741   characterPortraits.gd:21 @ setPortrait(): Condition "!resources.has(p_name)" is true. Returning: Ref<Resource>()
  <C++ Source>   scene/main/resource_preloader.cpp:118 @ get_resource()
  <Stack Trace>  characterPortraits.gd:21 @ setPortrait()
                 textbox.gd:218 @ _ready()

I am both not entirely sure what this error means, nor am I sure how to fix it, so that is where I come to the community for help. It is currently 2am, so if this seems unclear or garbled at all I am more than willing to elaborate! Also, again, if there is a better way to do what I am trying to do, I am all ears. Thank you !

ResourcePreloader is a node to pre-load a list of Resources in the editor. It does not load Resources by itself. You need to use load()

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.