set Texture from String

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

Hello community, I have a problem again.

var TextureReference = "[StreamTexture:1458]"

I have String variable called TextureReference that I want to set to Sprite, something like this:

$Sprite.texture = TextureReference

Any idea of how to accomplish that? I have tried:
str2var(TextureReference) but function str2var can’t convert string into a object

how do you get the string value? is it hard coded or do you str() an existing variable?

rustyStriker | 2020-11-18 17:47

:bust_in_silhouette: Reply From: Zylann

[StreamTexture:1458] cannot be used to get your texture, it is only a debug representation made by the print() or str() functions. If you got it from a file, this won’t work either.

To obtain the texture you have to know its path:

var TextureReference = load("res://path/to/texture.png")

Or you may want to assign it from the inspector in the editor, by using export:

export(Texture) var TextureReference