Return image ID of an item

Is there a way to input an item's ID and have the image it's currently using get returned? I tried setting up a dictionary but that isn't working, and I figure the game somehow already knows so there must be a way to get that, right?
Do you mean the icon or what?
If you want to put the icon ID in a variable, you can use this in a Control Variables script call:

JavaScript:
$dataItems[42].iconIndex

Replace 42 with the ID of the item. If the ID is in a variable, you can use this instead:

JavaScript:
(item => (item ? item.iconIndex : 0))($dataItems[$gameVariables.value(42)])

That'll return 0 if the variable doesn't have a valid item ID in it.
Do you mean the icon or what?
yeah

If you want to put the icon ID in a variable, you can use this in a Control Variables script call:

JavaScript:
$dataItems[42].iconIndex

Replace 42 with the ID of the item. If the ID is in a variable, you can use this instead:

JavaScript:
(item => (item ? item.iconIndex : 0))($dataItems[$gameVariables.value(42)])

That'll return 0 if the variable doesn't have a valid item ID in it.
wait so that first one will always return the icon ID of the item ID put into the square brackets? and the second one has a safeguard and instead looks at the game variable for the item ID of which to get the icon ID of right? Just making sure I'm reading this properly

If you want to put the icon ID in a variable, you can use this in a Control Variables script call:

JavaScript:
$dataItems[42].iconIndex

Replace 42 with the ID of the item. If the ID is in a variable, you can use this instead:

JavaScript:
(item => (item ? item.iconIndex : 0))($dataItems[$gameVariables.value(42)])

That'll return 0 if the variable doesn't have a valid item ID in it.
sorry for the double reply but I also want to know; is there a separate method to do this with weapons and armor too?
wait so that first one will always return the icon ID of the item ID put into the square brackets?
Correct.

the second one has a safeguard and instead looks at the game variable for the item ID of which to get the icon ID of right?
Correct.

is there a separate method to do this with weapons and armor too?
You can replace $dataItems with one of $dataWeapons, $dataArmors, $dataStates, or $dataSkills.
Correct.


Correct.


You can replace $dataItems with one of $dataWeapons, $dataArmors, $dataStates, or $dataSkills.
awesome!! thank you so much for the help!!!

Original thread: https://forums.rpgmakerweb.com/threads/return-image-id-of-an-item.183464/