How to set an image.jpg as a variable?

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

Greets!

In my character creation, one has to select a portrait. Got a global autoload and a sprite where to load the portrait, but what’s the syntax to save it please?

:bust_in_silhouette: Reply From: jgodfrey

You want to load the texture for a Sprite, from code? If that’s the case, something like this should work:

func _ready():
	var theTexture = preload("res://icon.png")
	$Sprite.texture = theTexture

Basically, you need to load/preload the texture and then assign it to the sprite’s texture property.

Can’t make it work. Look, got 3 scripts:

One from my collisionshape after the selectin click:

extends CollisionShape2D

func _on_Area2D_input_event(viewport, event, shape_idx):
	if event is InputEventMouseButton:
		if event.button_index == BUTTON_LEFT and event.pressed:
			GlobalP.portrait = ("res://Textures/Icons/Aventuriers/Men/reddy.jpg")
			get_node("AnimationPlayer").play("change")

On on my GlobalP:

var portrait = null

:s

And one on my stage scene parent node2D:

func _ready():
var portrait = preload("GlobalP.portrait")
$Portrait(#my sprite).texture = portrait

:s But i can’t preload the resource with such a path, and i don’t know how to reach it…

Syl | 2020-02-09 17:03

You don’t want to preload "GlobalP.portrait". You want to preload GlobalP.portrait (note, no quotes)

jgodfrey | 2020-02-09 18:18

Ok, but then i got ‘expected string constant as preload argument’ error…

Otherwise, the ‘var portrait = null’ is correct?

Syl | 2020-02-09 18:30

Ah, I see… From the docs.

You can also preload resources. Unlike load, this function will read the file from disk and load it at compile-time. As a result, you cannot call preload with a variable path: you need to use a constant string.

So, to use preload, the name has to be known at compile time, so a variable won’t work here…

jgodfrey | 2020-02-09 18:50

load will work with a variable name, if that suits your needs.

jgodfrey | 2020-02-09 18:52

Almost, but now i don’t find the node Portrait… And get_node needs a method…

Syl | 2020-02-09 19:52

Shot of my scene, Stage/Node2D and Portrait:

Imgur: The magic of the Internet

Syl | 2020-02-09 20:02

Now you’re just having basic node access issues it seems?

It’s difficult to nail down the problem without some more details about your scene setup.

That said, I’m not sure if this is pseudo-code or what, but it looks suspicious:

$Portrait(#my sprite).texture = portrait

Also, I’m not sure what you mean by “get_node needs a method” .

You’ll probably need to post the specific code that’s causing problems, along with the exact reported error, and maybe a shot of your scene tree for further assistance.

jgodfrey | 2020-02-09 20:05

Sry,

$Portrait(#my sprite).texture = portrait

is just:

$Portrait.texture = portrait

to specify my sprite displayin the portrait.

Tried to change $Portrait with get_node, but i got that method error. And i posted the shot just before your last reply.

This is the script from the Stage/node2D:

extends Node2D

func _ready():
	var portrait = load(GlobalP.portrait)
	$Portrait.texture = portrait

that should load the Portrait below, but don’t know how to reach it…

Syl | 2020-02-09 20:18

Just noticed your posted scene tree. And, where does the script that references $Portrait live? On the scene parent? So, Stage in that screenshot? If that’s the case, your reference would be…

$Player/Camera2DPlayer/Portrait.texture = portrait

If the script lives instead on Player, then it’d be $Camera2DPlayer/Portrait.texture = portrait

jgodfrey | 2020-02-09 20:19

Looks like we replied at the same time again, but see where the above gets you…

jgodfrey | 2020-02-09 20:20

Here.We.Are. Cheeeeers! Though now i’ll have to set all my portraits to the same size so that they fit equally the sprite, but that’s another matter. Cheers again! :slight_smile:

Syl | 2020-02-09 20:27

Nice - glad it’s worked out. Good luck with the rest of it.

jgodfrey | 2020-02-09 20:30