Referencing an array in a dictionary to assign properties to a node?

Let me know if this is OK, as my question does not pertain directly to the Godot course content, though I am taking that course. I’m trying to create an inventory UI in Godot (strictly for demo purposes, not to use in an actual game), and specifically to create an Item scene that I can instance and populate at runtime based on what the item is.

I created a dictionary with the item names for keys and an array for the values. I’m trying to use the item names to access the values in the arrays and fill in the values.

So far I’m still stuck trying to assign the texture. This is my Item script:

extends Node2D

export var itemName = ""

var itemIcon : Texture

var descLine1 = ""
var descLine2 = ""
var descLine3 = ""

var itemType = ""
var itemRangeXY = ""
var itemRangeZ = ""
var itemDict = {
	"Repair (Standard)": ["res://GFX/Item Icons/wrench.png", "Restores 100 HP to a single part.", "Does not work on destroyed parts.", "", "Heal", "1", "1"],
	"Shiva's Flashblade": ["res://GFX/Item Icons/sword.png", "Electrical energy honed to a fine, cruel edge.","Blessed by Lord Shiva.", "Offense: 150", "Equip", "1", "2"],
	"Electroshield": ["res://GFX/Item Icons/shield.png", "An electrical interference field that blocks incoming attacks.", "May nullify electrical attacks.", "Defense: 210", "Equip", "N/A", "N/A"],
	"Night Vision Goggles": ["res://GFX/Item Icons/nvg.png", "Improves accuracy during nighttime skirmishes by 40%", "Single use.","", "Support", "N/A", "N/A"],
	"Surya Missile 10":  ["res://GFX/Item Icons/missile.png", "Refills 10 rounds of missile ammo.", "Range stacks with weapon range.", "Offense: 35 + weapon offense.", "Ammo", "3", "4"],
	"Landmine": ["res://GFX/Item Icons/landmine.png","Deals explosive damage when triggered by any unit.","","Offense: 200","Offense","N/A","N/A"],
	"Grenade 5": ["res://GFX/Item Icons/grenade.png", "Refills 5 rounds of grenade ammo.", "Range stacks with weapon range.", "Offense: 44 + weapon offense.", "Ammo", "2", "2"],
	"Ravana Grenade Launcher": ["res://GFX/Item Icons/g launcher.png","A grenade launcher imbued with demonic power.", "", "Offense: 309", "Equip", "15", "3"],
	"Fuel Cell": ["res://GFX/Item Icons/fuel cell.png", "Grants 5 extra AP on the target's next turn.", "", "", "Support", "1", "2"],
	"Delivery Drone": ["res://GFX/Item Icons/drone.png", "Sends an item to an allied unit anywhere on the battlefield.", "", "", "Support", "∞", "∞"],
	"Communications Unit": ["res://GFX/Item Icons/comms.png", "Issues a command to any allied unit to act in the user's stead.", "Target may have acted already, but must have enough AP to act.", "", "Support", "∞", "∞"],
	"Sniper Bullets 50": ["res://GFX/Item Icons/bullet.png", "Refills 50 rounds of sniper rifle ammo.", "Takes weapon range.", "Offense: 35 + weapon offense.", "Ammo", "N/A", "N/A"]

# Called when the node enters the scene tree for the first time.
func _ready():
	if itemName == "Repair (Standard)":
		itemIcon == load(itemDict.get("Repair (Standard)"[0]))
		descLine1 == itemDict.get("Repair (Standard)"[1])
		descLine2 == itemDict.get("Repair (Standard)"[2])
		descLine3 == itemDict.get("Repair (Standard)"[3])
		itemType == itemDict.get("Repair (Standard)"[4])
		itemRangeXY == itemDict.get("Repair (Standard)"[5])
		itemRangeZ == itemDict.get("Repair (Standard)"[6])
	$Icon.texture = itemIcon

I imagine I’m not referencing the array values correctly, but I haven’t been able to find clear instructions on how to get items from an array when the array itself is a value in a dictionary.

This is a screenshot of my inspector with the error at the bottom (Invalid type in built-in function ‘load’. Cannot convert argument 1 from Nil to String.)

Thanks for reading! Any help is appreciated, and happy to provide more information if needed.

had a look and to load the image icon you could use the following
itemIcon = load(itemDict["Repair (Standard)"][0])

theres alot of == in there and there should only be one at the if statement.

remember that == is for an equality check and = is for assignment.


1 Like

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