Jump to content
  • 0
Kavaline

Newbie question: how get an item ID by item name?

Question

I have a string var with the item name stored (a Jellopy, for example). How I get the ID of the Jellopy by their name?

I need do this in this way, because the name that I get is dynamic, based on a long list of items.

I want something like "getitemname(<item id>)", but more like "getitemid(<item name>)", or a way to get it, without using "searchitem(<array name>, "<item name>")".

 

Edited by Kavaline
Fix a typo

Share this post


Link to post
Share on other sites

5 answers to this question

Recommended Posts

  • 0

As promised over at Discord, a patch to extend getiteminfo() script command, to get an item's ID by its name:getiteminfo_extension.diff

 

Spoiler

diff --git a/doc/script_commands.txt b/doc/script_commands.txt
index 8308f4771..e24c6b1d4 100644
--- a/doc/script_commands.txt
+++ b/doc/script_commands.txt
@@ -3277,6 +3277,7 @@ Example:
 ---------------------------------------
 
 *getiteminfo(<item ID>, <type>)
+*getiteminfo("<item name>", <type>)
 *setiteminfo(<item ID>, <type>, <value>)
 
 This function will look up the item with the specified ID number in the
@@ -3285,6 +3286,9 @@ It will return -1 if there is no such item.
 
 Valid types are:
 
+	ITEMINFO_ID                      - Item ID (getiteminfo() only!)
+	ITEMINFO_AEGISNAME               - Unique name to reference the item (getiteminfo() only!)
+	ITEMINFO_NAME                    - Display name (getiteminfo() only!)
 	ITEMINFO_BUYPRICE                - Buy Price
 	ITEMINFO_SELLPRICE               - Sell Price
 	ITEMINFO_TYPE                    - Item Type
diff --git a/src/map/script.c b/src/map/script.c
index c1eb2e8b7..4c485ec28 100644
--- a/src/map/script.c
+++ b/src/map/script.c
@@ -14788,9 +14788,15 @@ static BUILDIN(getitemslots)
  *------------------------------------------*/
 static BUILDIN(getiteminfo)
 {
-	int item_id = script_getnum(st, 2);
 	int n = script_getnum(st, 3);
-	struct item_data *it = itemdb->exists(item_id);
+	struct item_data *it;
+
+	if (script_isstringtype(st, 2)) { /// Item name.
+		const char *name = script_getstr(st, 2);
+		it = itemdb->search_name(name);
+	} else { /// Item ID.
+		it = itemdb->exists(script_getnum(st, 2));
+	}
 
 	if (it == NULL) {
 		script_pushint(st, -1);
@@ -14928,6 +14934,15 @@ static BUILDIN(getiteminfo)
 	case ITEMINFO_GM_LV_TRADE_OVERRIDE:
 		script_pushint(st, it->gm_lv_trade_override);
 		break;
+	case ITEMINFO_ID:
+		script_pushint(st, it->nameid);
+		break;
+	case ITEMINFO_AEGISNAME:
+		script_pushstr(st, it->name);
+		break;
+	case ITEMINFO_NAME:
+		script_pushstr(st, it->jname);
+		break;
 	default:
 		ShowError("buildin_getiteminfo: Invalid item type %d.\n", n);
 		script_pushint(st,-1);
@@ -26970,7 +26985,7 @@ static void script_parse_builtin(void)
 		BUILDIN_DEF(setnpcdisplay,"sv??"),
 		BUILDIN_DEF(compare,"ss"), // Lordalfa - To bring strstr to scripting Engine.
 		BUILDIN_DEF(strcmp,"ss"),
-		BUILDIN_DEF(getiteminfo,"ii"), //[Lupus] returns Items Buy / sell Price, etc info
+		BUILDIN_DEF(getiteminfo,"vi"), //[Lupus] returns Items Buy / sell Price, etc info
 		BUILDIN_DEF(setiteminfo,"iii"), //[Lupus] set Items Buy / sell Price, etc info
 		BUILDIN_DEF(getequipcardid,"ii"), //[Lupus] returns CARD ID or other info from CARD slot N of equipped item
 		BUILDIN_DEF(getequippedoptioninfo, "i"),
@@ -27615,6 +27630,9 @@ static void script_hardcoded_constants(void)
 	script->set_constant("ITEMINFO_ITEM_USAGE_FLAG", ITEMINFO_ITEM_USAGE_FLAG, false, false);
 	script->set_constant("ITEMINFO_ITEM_USAGE_OVERRIDE", ITEMINFO_ITEM_USAGE_OVERRIDE, false, false);
 	script->set_constant("ITEMINFO_GM_LV_TRADE_OVERRIDE", ITEMINFO_GM_LV_TRADE_OVERRIDE, false, false);
+	script->set_constant("ITEMINFO_ID", ITEMINFO_ID, false, false);
+	script->set_constant("ITEMINFO_AEGISNAME", ITEMINFO_AEGISNAME, false, false);
+	script->set_constant("ITEMINFO_NAME", ITEMINFO_NAME, false, false);
 
 	script->constdb_comment("getmercinfo options");
 	script->set_constant("MERCINFO_ID,", MERCINFO_ID, false, false);
diff --git a/src/map/script.h b/src/map/script.h
index 857d22c61..ed860368c 100644
--- a/src/map/script.h
+++ b/src/map/script.h
@@ -484,6 +484,9 @@ enum script_iteminfo_types {
 	ITEMINFO_ITEM_USAGE_FLAG,
 	ITEMINFO_ITEM_USAGE_OVERRIDE,
 	ITEMINFO_GM_LV_TRADE_OVERRIDE,
+	ITEMINFO_ID,
+	ITEMINFO_AEGISNAME,
+	ITEMINFO_NAME,
 	ITEMINFO_MAX
 };
 

 

 

Usage:

.@item_id = getiteminfo("item name", ITEMINFO_ID);

 

 

~Kenpachi

Share this post


Link to post
Share on other sites
  • 0

instead of using item names in your array you should be using item constants if what you want is item IDs, so something like

setarray(.@items$,
	"Jellopy",
	"Large_Jellopy",
	...);

becomes

setarray(.@items,
	Jellopy,
	Large_Jellopy,
	...);

so you use the unquoted AegisName from the item db, which is a constant of its ID
 

 

but anyway, the getitem() command does work with both the item ID and the AegisName so if you are using the name it should work fine.

 

// all of these are equivalent:
getitem(501, 1);
getitem(Red_Potion, 1);
getitem("Red_Potion", 1);

// but this won't work:
getitem("Red Potion", 1);

 

Share this post


Link to post
Share on other sites
  • 0

@meko

But have a way to get the AegisName of the item?

My script gets a name of an item to appoint to another item. I used the "getequipname()", that returns the "name" field, not the AegisName. With this name, I need get the ID of this item.

.@item$ = getequipname(EQI_HAND_R)+"_item";
.@item_id = .@item$;
3 hours ago, meko said:

// but this won't work:

getitem("Red Potion", 1);

Alread tried before with "delitem" (delitem (getequipname(EQI_HAND_R)+"_item"), 1;) works, but I saw that isn't recommended. And I need to use with others commands too.

I will test some tricks here, you gave me an idea of how I can do it. The "name" field can have "_" character without problem? And same name of AegisName? Doing this, I think I can solve, but I don't know if will have some problem. Is for a custom item.

 

@Edit

I need a solution that fit this kind of case:

//Database modifications:
//A equipped weapon called "Red Potion" in field "name"
//In potion, ID 501, change the field "name" from "Red Potion" to "Red Potion_item"
.@item$ = getequipname(EQI_HAND_R)+"_item";
.@item_id = ~magic_code~;
if (.@item_id == 501) mes "Code works.";
close;

//But, this code works (with same database modifications):
delitem (getequipname(EQI_HAND_R)+"_item"),1;
close;

But not only this case, that I can compare (.@item$ == wanted_name) instead. I need the ID number.

Edited by Kavaline

Share this post


Link to post
Share on other sites
  • 0

Hi.

 

If you're only working with equipped items, you can use the getequipid() script command to read the item's ID.

Spoiler
Quote

*getequipid(<equipment slot>)

 

This function returns the item ID of the item equipped in the equipment
slot specified on the invoking character. If nothing is equipped there, it
returns -1. Valid equipment slots are:

 

EQI_HEAD_TOP (1)          - Upper head gear
EQI_ARMOR (2)             - Armor (Where you keep your Jackets and Robes)
EQI_HAND_L (3)            - What is in your Left hand.
EQI_HAND_R (4)            - What is in your Right hand.
EQI_GARMENT (5)           - The garment slot (Mufflers, Hoods, Manteaus)
EQI_SHOES (6)             - What foot gear the player has on.
EQI_ACC_L (7)             - Accessory 1.
EQI_ACC_R (8)             - Accessory 2.
EQI_HEAD_MID (9)          - Middle Headgear (masks and glasses)
EQI_HEAD_LOW (10)         - Lower Headgear (beards, some masks)
EQI_COSTUME_HEAD_LOW (11) - Lower Costume Headgear
EQI_COSTUME_HEAD_MID (12) - Middle Costume Headgear
EQI_COSTUME_HEAD_TOP (13) - Upper Costume Headgear
EQI_COSTUME_GARMENT (14)  - Costume Garment
EQI_SHADOW_ARMOR (15)     - Shadow Armor
EQI_SHADOW_WEAPON (16)    - Shadow Weapon
EQI_SHADOW_SHIELD (17)    - Shadow Shield
EQI_SHADOW_SHOES (18)     - Shadow Shoes
EQI_SHADOW_ACC_R (19)     - Shadow Accessory 2
EQI_SHADOW_ACC_L (20)     - Shadow Accessory 1

 

Notice that a few items occupy several equipment slots, and if the
character is wearing such an item, 'getequipid' will return it's ID number
for either slot.

 

Can be used to check if you have something equipped, or if you haven't got
something equipped:



	if (getequipid(EQI_HEAD_TOP) == Tiara) {
		mes("What a lovely Tiara you have on");
		close();
	}
	mes("Come back when you have a Tiara on");
	close();

 

You can also use it to make sure people don't pass a point before removing
an item totally from them. Let's say you don't want people to wear Legion
Plate armor, but also don't want them to equip if after the check, you
would do this:



	if (getequipid(EQI_ARMOR) == Full_Plate_Armor || getequipid(EQI_ARMOR) == Full_Plate_Armor_) {
		mes("You are wearing some Legion Plate Armor, please drop that in your stash before continuing");
		close();
	}
	if (countitem(Full_Plate_Armor) > 0 || countitem(Full_Plate_Armor_) > 0) {
		mes("You have some Legion Plate Armor in your inventory, please drop that in your stash before continuing");
		close();
	}
	mes("I will lets you pass");
	close2();
	warp("place", 50, 50);
	end;

 

 

 

But there's no script command to generally get an item's ID from its name.

 

 

~Kenpachi

Share this post


Link to post
Share on other sites
  • 0

@Kenpachi

I need exactly for healing/misc items 😕

And is a useful function. I can manually add a new field in the items from item_db2.conf? Some kind of "comment" field? And a function to read this field with "getiteminfo", something like "getiteminfo(<item ID>, ITEMINFO_COMMENT)"?

With this, solve my problem.

Edited by Kavaline

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.