How to find out if an item was renamed or uses default from itemdesc.cfg ?

Here you can post threads specific to the current release of the core (099)

Moderator: POL Developer

Post Reply
OWHorus
Grandmaster Poster
Posts: 105
Joined: Sat Feb 04, 2006 1:24 pm
Location: Vienna, Austria

How to find out if an item was renamed or uses default from itemdesc.cfg ?

Post by OWHorus »

Hello,

sorry for this - I did not use newer cores and did not read the core-changes.txt, at least not in detail.

Now I have a problem in a lot of scripts:

Old method to find out if an item was renamed (by a GM) in game:

Code: Select all

if (item.name)
  print("Item renamed = "+item.name);
else
  print("Item uses default = "+item.desc);
endif
But:
09-18-2013 Tomi:
Changed: Item.name now returns itemdesc.Desc without singluar/plural formatting if no name is set and name from tiledata if no itemdesc entry found

Now all my scripts which use the check do no longer work. Great...

Now I am forced to look it up in itemdesc.cfg every time (even in scripts which do not need to access itemdesc.cfg!), by comparison

Code: Select all

var idesc := ReadConfigFile("itemdesc");
if (item.name == idesc[item.objtype].desc)
  print("item uses default = "+item.name);
else
  print("Item renamed = "+item.name);
endif
That means, that simple and short scripts now have to search the entire itemdesc without need, just to see if the item was renamed or not...

Or is there a better way now? In several places an situations I need to know if an item was renamed or not, since renamed items have a special status in the game (quest items, specail items, and so on). Why was this done anyway???

OWHorus

EDIT:

It is even worse...
We have items which use the default name from tiledata.mul, then we have items, which we renamed in itemdesc.cfg (using Desc Entry), and we have manually renamed items, which names where set in game.

An item, which uses its tiledata.mul name has no Desc entry in itemdesc.cfg. So the simple check, which worked in all POL releases since at least 0.92 or so (see first code sample) must now be coded so:

Code: Select all

	var idesc := ReadConfigFile("itemdesc");
	var tdesc := ReadConfigFile("tiles");
	var iname := idesc[item.objtype].desc;
	if (iname)
		if (item.name == iname)
			print("Uses default = "+item.name);
		else
			print("Name changed = "+item.name);
		endif
	else
		if (item.name == tdesc[item.objtype].desc)
			print("Uses default = "+item.name);
		else
			print("Name chnaged = "+item.name);
		endif
	endif
Uhm. Was this really necessary? To be forced to look up two huge config files just to find out if an item was renamed in game?

I really hope there is a better way, but at the moment I am at a loss...

OWH
OWHorus
Grandmaster Poster
Posts: 105
Joined: Sat Feb 04, 2006 1:24 pm
Location: Vienna, Austria

Re: How to find out if an item was renamed or uses default from itemdesc.cfg ?

Post by OWHorus »

Just to answer my own question - in a way - or to show what I found:

Method 1 - complex, but clean:

Code: Select all

	// itref = itemref in question
	var tname;
	if (itref.obtype < 0x4000)		// We use UO ML, i.e. max graphic is 0x3fff
		// Only 'real' UO items are in tiledata.mul
		var tdesc := ReadConfigFile("tiles");
		tname := tdesc[itref.objtype].desc;
	endif
	var itdesc := GetItemDescriptor(itref);
	var iname := itdesc.Desc;
	if (iname)
		if (itref.name == iname)
			print("Name is default");
		else
			print("Name was set manually");
		endif
	elseif (tname)
		if (itref.name == tname)
			print("Name is default");
		else
			print("Name was set manually");
		endif
	else
		print("Error - name undefined?");
	endif
Method 2 - simple, but a bit ugly, because the item is changed just to test it

Code: Select all

	var oldname := item.desc;
	SetName(item, "");
	var newname := item.desc;
	if (oldname == newname)
		print("Uses default = "+item.desc);
	else
		// Change it back again
		SetName(item, oldname);
		print("Name was set manually = "+item.desc);
	endif
Both methods work, and are tested. I am unsure about method 2, because we change the item (and back again) just to test if the name was set manually, but it is charmingly simple and short...

Any comments or better ideas?

OWHorus
OWHorus
Grandmaster Poster
Posts: 105
Joined: Sat Feb 04, 2006 1:24 pm
Location: Vienna, Austria

Report, what I had to do...

Post by OWHorus »

Hello,

first - I decided to use method 1. Why? Because every test, if an items name is set (and stored in the worldsave) or if it uses a default (from itemdesc or tiles) method 2 increments the revision, the item is changed and changed back. Since we do this sometimes on a lot of items I decided to use a test which does not change the item.

The old test - working for 10 or so years in POL - was:

Code: Select all

if (item.name)
	// item has an individual name
else
	// item uses default
endif
This does not work anymore. It broke around 10% of our scripts (we have over 1200, 115 were broken), which had to be changed. I hope my full text search found all the places, there may be some I overlooked...

Here is what I use at the moment:

Code: Select all

use polsys;
// Parameter is itemref, returns the name, if set, else returns undef
function nameIsSet(itref)
	var retval;
	var idesc := GetItemDescriptor(itref.objtype);
	var cfgname;
	if (idesc.Desc)
		cfgname := idesc.Desc;
	endif
	// If a desc entry exists, it overrides tiledata.mul entry, if not, tiles.cfg must be checked
	if (!cfgname)
		if (itref.objtype < 0x4000)		// UO ML Graphic Limit, could be higher for newer clients
			var tcfg := ReadConfigFile("tiles");
			cfgname := tcfg[itref.objtype].Desc;
		endif
	endif
	// cfgname now has the name from the configs
	if (cfgname)
		if (cfgname == itref.desc)
			// name is standard - return undef
			return retval;
		else
			// name is set, return it
			return itref.desc;
		endif
	endif
	// This is an error catcher, can only go here if neither tiledata.mul nor itemdesc.cfg name is defined - should not happen
	return itref.desc;
endfunction
This function is outright _UGLY_, compared to 'if (item.name)'.

If I am not being silly and overlooking something obvious - an easier test for all this - then I would ask for a new core function to test the origin of the name of an item!!

I mean really - this ugly function is the best I found to test if an item was renamed in game (i.e. stores its name in the worldfile) or if it uses a default name from the configuration. I am very surprised that no scripter has yelled yet! We use this quite often, and not for exotic things. Our plant growing system depends on it (plants are renamed, but sometimes a default is created, renamed again, and so on), secret doors have, when closed the exact name of the wall part, when opened the change the name to 'secret door'. We use player usable multi items (for things which have more than one tile, such as beds), and if one part is renamed, the player is not allowed to pack it. We check for renamed items in our building system, and so on. A lot of GM commands use it also. I cannot imagine, that nobody found this to be a problem...

So I propose a new core function:

polsys::ItemNameOrigin( item )
Returns one of 3 Constants, or an error
const ITEM_NAME_TILES // Name not set in itemdesc and not individually set, comes from tiledata
const ITEM_NAME_CONFIG // Name comes from itemdesc.cfg
const ITEM_NAME_CUSTOM // Name was set and is stored in the worldfile

Or something like this. This way the change would stand, but an easy and fast test would exist again. It would force everybody to rewrite a lot, but at least not with such an ugly function...

Or - see above - I am being silly and are not seeing the obvious :-)

OWHorus
Nando
POL Developer
Posts: 282
Joined: Wed Sep 17, 2008 6:53 pm
Contact:

Re: How to find out if an item was renamed or uses default from itemdesc.cfg ?

Post by Nando »

I'm afraid the developer who changed the code wasn't aware of that use for item.name. If I recall correctly, the idea was that it was odd to have no "item.name" for an item.

I like you idea of a function to test where the name is coming from. I'll look into it.
OWHorus
Grandmaster Poster
Posts: 105
Joined: Sat Feb 04, 2006 1:24 pm
Location: Vienna, Austria

Re: How to find out if an item was renamed or uses default from itemdesc.cfg ?

Post by OWHorus »

Hallo Nando,

thank you for looking into this. How about a new member for the class item, item.named?

boolean item.named, false if item has a standard name and true, if the item was renamed in game, i.e. its name is stored in the save.

So the checks could be

if (item.named)

instead of the old

if (item.name)

Would be better to read anyway :-)

OWHorus
Turley
POL Developer
Posts: 670
Joined: Sun Feb 05, 2006 4:45 am

Re: How to find out if an item was renamed or uses default from itemdesc.cfg ?

Post by Turley »

Please checkout latest revision. I added specific_name member. It's untested since I'm not on my PC, but should work.
OWHorus
Grandmaster Poster
Posts: 105
Joined: Sat Feb 04, 2006 1:24 pm
Location: Vienna, Austria

Re: How to find out if an item was renamed or uses default from itemdesc.cfg ?

Post by OWHorus »

Turley wrote:Please checkout latest revision. I added specific_name member. It's untested since I'm not on my PC, but should work.
Just saw it - super! Thank you!

I am compiling it just now - I also will test your fix for elemental resistances and damages.

OWHorus
OWHorus
Grandmaster Poster
Posts: 105
Joined: Sat Feb 04, 2006 1:24 pm
Location: Vienna, Austria

Re: How to find out if an item was renamed or uses default from itemdesc.cfg ?

Post by OWHorus »

Hello,

the new member item.specific_name works fine. I tested it with several items, no problems.

Thank you for adding this (again), it is really practical to have a fast check if an item was renamed in game (i.e. has its name stored in the save) or if it uses its standard name from itemdesc or tiledesc.

OWHorus
Post Reply