How to check if a particular item was selected?

I'm starting an event command... 'Select Item'

Then I store the item id in a variable. I want to check if the item selected was 'Sword'. How do I do this?

I don't know how to access the id of the 'Sword' item. Do I have to store it in another variable, then compare the two variables?

Thanks.

EDIT: Never mind. I completely forgot that every item has an id that is accessible in the Database/Items menu.
Hey, how did you actually do a check though? Is there a command to check for which ID (item) was selected?
Hey, how did you actually do a check though? Is there a command to check for which ID (item) was selected?

So first you select an item and put it into a variable... say the variable is:

0003 item_selected
as listed in the variables

Also supposed the item you want to check for is

0002 Magic Water
as listed in items

So after you select the item, you can put in a conditional branch with script and in the script you can put:

$dataItems[$gameVariables.value(3)].name=='Magic Water'

So if you selected magic water it will go into the conditional branch. That's how I did it.
Oh perfect! Going to try this as soon as I can!
Thanks for the help!
No problem.
Got this working beautifully, so thank you for that!
I was wondering what I could do with this line of code: "$dataItems[$gameVariables.value(10)].name == "item_Name""
to modify it, such that if the player presses the Escape key while 'Select Item' is being used.
I'm getting an "null name" error since I click the Escape key before making a selection of an item (thus not giving a name to the variable which is searching for the ID of the item).
Is there a way to recognize the escape key (WHILE being in item select)?
The way I handled that is... at the top you first set your variable (where you store your item) to -1... or some dummy value that indicates a selection hasn't been made.

When a valid selection is made the variable will be 0 or greater. When cancel is hit the variable stays at -1.

So after the select item command, you use a conditional branch... and select your variable and set condition >= constant 0.

And then you put all your code that you want to run after a valid selection under that conditional branch.

If you hit cancel, your variable stays at -1 and it should skip over the whole branch.
This doesn't seem to be working for me. I have multiple items being selected from and I want the player to choose of the 7 or 8 choices of items to give. Each item will affect a meter in a different way.

I have it set up in order of logic:
set variable to -1
Select Item
If Button is Cancel (keeps variable @ -1, can include break loop)
If variable >= 0, then perform all the gamedata code and item code)

This doesn't seem to work though, and still gives me name null.
Right idea, just change your condition to "variable > 0" and I think it should work out. 🙏

($dataItems[0] is null, which explains the error. 😅)

Also, you don't need to set the variable value beforehand: it should set it to 0 if you don't pick an item, or the item ID if you do. ^_^
Perfect! Just got it fixed and working! Appreciate the help!
Ah, caethyril is right. I'm still a noob at this. No need to set the variables in advance... and cancel sets the variable to 0, so we need >0 as the condition.
Yea! Checks out!
No worries, I appreciate your help either way!
$dataItems[$gameVariables.value(3)].name=='Magic Water'
and that is a really cumbersome way to test.

you can test the item ID directly with the conditional branch, just use variable(ID) equals the IDnumber of the magic water directly, which is the number of it in the database list.
Absolutely no need to compare the names - just remember to remove any leading zeroes. If the database lists 0002 Magic Water, then the ID itself is 2
Ah so just checking the values together and seeing if the ID number matches? Gotcha! Thanks for the advice!
and that is a really cumbersome way to test.

you can test the item ID directly with the conditional branch, just use variable(ID) equals the IDnumber of the magic water directly, which is the number of it in the database list.
Absolutely no need to compare the names - just remember to remove any leading zeroes. If the database lists 0002 Magic Water, then the ID itself is 2

You're right. Not sure why I did it this way.

Original thread: https://forums.rpgmakerweb.com/threads/how-to-check-if-a-particular-item-was-selected.108243/