Setting Sprite2D texture from resource

Godot Version

4.7

Question

I have a resource that holds a character’s stats, graphics, and other info. I also want the Sprite2D representing the character to change, depending on the resource it is taking its data from.

I see there is a way to load images and convert them to sprite2D textures with load(), but that seems to be for when you want to load a specific image file with a path, like so:

var texture = load("res://icon.svg")

How do you load it when it’s from a resource, and you don’t always know what image it is ahead of time?

What do you mean by “from a resorce”. Image files are resources.

Your resource can export a texture resource directly, which can be assigned in-editor

extends Resource
@export var my_character_texture: Texture2D

# later your player script can use the resource
$Sprite2D.texture = my_resource.my_character_texture

Or a filepath to the resource if that’s more aligned with your example

extends Resource
@export_file("*.svg") var my_svg_path: String

# Load the filepath
var texture = load(my_resource.my_svg_path)