[SOLVED] How to change displayed item name?

I'm attempting to write an unidentified items script, and this includes potentially changing the displayed names of unidentified items (and also other stuff like description, price, icon index, for example, eventually).


However, I'm having problems working out how to change the displayed name of an item, conditionally or at all. At first I thought I'd be able to override Game_Item.prototype.name with a function that under the right conditions returns a different name, but that doesn't seem to work.


What I have for conditionally replacing the name currently looks like this (without a check for whether it's identified, because right now I really just want this to work; without being able to conditionally change the displayed name, the rest is kind of pointless), and is probably stupid in at least one way:


Game_Item.prototype.name = function() {
if (this.meta.unidentified_name){
return this.meta.unidentified_name;
}
else {
return this.name;
}
};


With this, in-game dialogues, like the inventory or item select windows, still display the proper name of an item even when unidentified_name has been supplied in the item's note, and '$dataItems[1].meta.unidentified_name' returns the supplied alternate name for the item (because I'm lazy and test with potion).


Is this even the right place to be looking? Or am I on the right track, but doing something wrong?
Well, what you're checking for is "unidentified_name", not "unidentified_item". Check your notetags.
That unfortunately is just a typo in the post (now corrected), not in the notetags. Item 1's notetags are as follows:


<unidentified_name😛otion>


<unidentified_desc:A bottle of liquid.>


And, as I said, '$dataItems[1].meta.unidentified_name' does return the unidentified name properly.
I can't seem to find the original Game_Item.prototype.name function.. Maybe that has something to do with it? Like it doesn't exist so you can't overwrite it... That or the condition you supplied doesn't return true... Try putting a console error message before the return line when the condition is true then test with the debug console open to see if it reaches that part.


Also, won't this.name return a call to Game_Item.prototype.name, causing an infinite loop?
That's possible, though I was reasonably sure in another script I was overriding a variable with a function with no issue, though I may be dumb and that might not be what I was actually doing. $dataItems[1].name does work, though, in this case, so it's not that the item doesn't have a name.


If I'm barking up the wrong tree, do you have any idea what I should be looking at to vary the displayed name?
I don't even see a .name variable on Game_Item too... It's actually puzzling me. XD or maybe it takes it directly from the database or something


btw, can you try to use this code instead just to see if the function and the conditions even run? change your code to this and run the game and open the debug console


Game_Item.prototype.name = function() {
//this is to check if the function even runs
console.error('notice','run');
if (this.meta.unidentified_name){
//this is to check if the condition returns true
console.error('notice','true');
return this.meta.unidentified_name;
}
else {
return this.name;
}
};




if the console will show run, it means your override works... if the console then says true, then it means the condition check also runs and is returning true... In that case it means the problem is elsewhere.


If the run message don't even show then we know for sure that the override doesn't work
Well, it's more that items aren't really exposed in the code, in this case. The only reason I know items have a name is I can do console.log([item]) and that'll spit out the item's variables and functions to the console.


With your code, nothing new is making its way to the console. Guess it really isn't doing a thing.
If it doesn't show a message when you view that item then it means your override isn't even running. So you need to find how they're actually finding the name of the items. My hunch right now is that they take it directly from the database items.


Yeah, seems like it's taking them directly from the database, which explains why Game_Item doesn't even have a name variable or function.. The functions in the game that shows the name of items etc actually uses the items from $dataItems rather than Game_Item. Seems like Game_Item is just there to provide a modifiable item object in-game and a link to the actual items in $dataItems.
I'm not sure that's even going to be workable if I have to find and replace every single place they get the item name.


Hmm. Do you know where the inventory interface is in the code? So I can check there first?
I wonder why they didn't do it like Game_Actors where they actually made a .name function... Maybe they never thought someone would try to change item names in-game.


I think everything that shows item names etc are in the rpg_windows.js or rpg_scenes.js


I think this line of code is of interest, here is where windows draw item's name, try this code that I edited for you


Window_Base.prototype.drawItemName = function(item, x, y, width) {
width = width || 312;
if (item) {
var iconBoxWidth = Window_Base._iconWidth + 4;
this.resetTextColor();
this.drawIcon(item.iconIndex, x + 2, y + 2);
if (item.meta.unidentified_name){
this.drawText(item.meta.unidentified_name, x + iconBoxWidth, y, width - iconBoxWidth);
}
else {
this.drawText(item.name, x + iconBoxWidth, y, width - iconBoxWidth);
}

}
};






PS: Tested it and it works... It showed watda on the item that I noted in the database with <unidentified_name:watda>


but how do you plan to be able to change it to identified mid-game?
Instead of 'if (item.meta.unidentified_name)', I'd probably have something like the following:


if (item.meta.identified == "false" && item.meta.unidentified_name && [something indicating item hasn't been ID'd])


My original idea was to add an 'identified' boolean to items, but that didn't pan out. What I'm currently planning is each item that is picked up unidentified is indicated by a notetag (<identified:false> - there's also a default for whether items start identified or unidentified, so it may be useful for developers to say <identified:true>). I'm probably going to keep an array (or list, or dict, or whatever; I more frequently work with python) of items that have been identified, and check against that.


Then identifying an item becomes a matter of just writing a method that adds the item to the array/list/whatever on demand, and making sure that's used where appropriate (if I want to automatically identify a thing on usage, for example, needs to be somewhere in the usage code).


Edit:


Okay, I've tested it and it works. I've also been able to use this method to switch the item icon, and found where the item description was added, too. Thank you very much for your help.
glad to be of help, plus I learned a thing or two about MV from this. XD

Original thread: https://forums.rpgmakerweb.com/threads/solved-how-to-change-displayed-item-name.63827/