Help with getting strings from strrefs

Probably very basic… I have a function that, given an item, is to generate a human readable string with an item name and the item’s type in parenthesis. I am sure there are lots of ways to do this. But, I am having trouble getting the object type string from the talk table.
Here is the function code:

string ShowNameAndType(object oItem)
    {
    string sItemName = GetName(oItem);
    int nType = GetBaseItemType(oItem);
    int nTypeNameStrref = StringToInt(Get2DAString("baseitems", "Name", nType));
    string sTypeName = GetStringByStrRef(nTypeNameStrref);
    string sNameAndType = sItemName + " (" + sTypeName + ")";
    PrintString("ShowNameAndType: oItem name = "+GetName(oItem) + " nType = "+IntToString(nType) + " nTypeNameStrref = "+IntToString(nTypeNameStrref)+" sTypeName =<"+sTypeName+">, full sNameAndType = <"+sNameAndType+">");
    return sNameAndType;
    }

The PrintString is for debugging. But, when I run this on a standard item, sTypeName is often an empty string. For example, if oItem is a Sword Saint Legacy +8 (resref: x2_wswmka005), this is what shows up in my log:

ShowNameAndType: oItem name = Sword Saint Legacy +8 nType = 41 nTypeNameStrref = 1535 sTypeName =, full sNameAndType =

So, why am I not getting the actual talk table entry (“Katana”) for that item type assigned to my sTypeName string? And, even if it’s an empty string (which maybe it isn’t?), why doesn’t sNameAndType at least have the item name? Do I have to convert the talk table string somehow before NWScript will let me work with it like a normal string?

BTW, something inconsistent seems to be happening, as well. If I run the same code snippet above from the in-game RunScriptChunk console, but I replace the PrintString line with

SendMessageToPC(GetFirstPC(),"ShowNameAndType: oItem name = "+GetName(oItem) + " nType = "+IntToString(nType) + " nTypeNameStrref = "+IntToString(nTypeNameStrref)+" sTypeName =<"+sTypeName+">, full sNameAndType = <"+sNameAndType+">");

then I get this odd behavior: As before, what shows up in the actual nwclientLog1.txt log is

[CHAT WINDOW TEXT] [Thu Jun 23 02:37:15] ShowNameAndType: oItem name = Sword Saint Legacy +8 nType = 41 nTypeNameStrref = 1535 sTypeName =, full sNameAndType =

But, what shows up at the in-game chat log is
image
which is what I want those strings to be.

I am a little baffled here.

Your script works fine for me:

In game I get

Sword Saint Legacy +8 (Katana)

and in the log file I get

ShowNameAndType: oItem name = Sword Saint Legacy +8 nType = 41 nTypeNameStrref = 1535 sTypeName =<Katana>, full sNameAndType = <Sword Saint Legacy +8 (Katana)>

What happens if you do not use the ‘<’ and ‘>’ as they often get misinterpreted.

1 Like

Ha! I had forgotten that those can indicate special formatting (colors) for strings. Thank you!