preloading a png into a button icon with path based on arraydata does not work

Godot Version

4.7.2.

Question

Hi! I am building a trade RPG where you buy and sell items from one NPC to another. For the trade menu’s I am loading in sprites into the icons of a button. This is so its clear what someone is buying. I am having trouble with it however.

This is the current way I am doing it:

	$buyBox/ColorRect/marginContainer/interacteables/tradeables/VBoxContainerTradeables/npcItemBox2/item3R1.icon = preload("res://art/Beans.png")

But, because there will be quite some NPC’s in this game, I want it to be that loading the picture is more standard and is based on which NPC is active. This is already in the files via an global data file with NPC data in it.

var activatedNPC = 0

var VeggieTrader = [ "vegetableFarmer", ["Cabbage", 2, "1", "3", "Carrot", 1, "1", "2", "Beans", 1, "1", "3"], 
["generic greeting", "didnt buy something", "bought something text"],
 ]
var WheatTrader = ["GrainFarmer", ["Wheat", 2, "1", "3", "Oats", 1 , "1" , "3", "Rye", 1 , "1", "3"], ["generic greeting", "didnt buy something", "bought something text"]]

var NPCs = [VeggieTrader, WheatTrader]

The activated NPC variable is set when entered the colisionbody of that NPC.

What I wanted to do was:

$buyBox/ColorRect/marginContainer/interacteables/tradeables/VBoxContainerTradeables/npcItemBox2/item3R1.icon = preload("res://art/"+NPCDataFile.NPCs[NPCDataFile.activatedNPC][1][0]+.png")

But that leads to the debugger saying it needs to be a full string. If I print this via print(), it gives me the path that I need. How can I do this??

preload() requires a constant String. For a variable path, use load().