[Feature Request] Store ItemDefinition Name in Custom Name Property

airoll

Member
Looking through the source code for ItemDefinition, it looks like we store the name of the item definition in the object.name property. However, this property creates garbage (per https://forum.unity.com/threads/request-gc-free-gameobject-name-api.517378/). Therefore, I'd like to request the addition of a new property that stores the name such that we don't need to create a new string / generate garbage every time we want to request the name of the item definition. Further, all instances of calling the name property in the UIS project (not sure if it all this is done) should reference the new Name property instead.
 
I was not aware of this. But I am not sure about making the change due to potential issues with names becoming out of sync.
Can I ask when and why you are using ItemDefinition Names? When possible you should use the ID if you are concerned about garbage.
If you have an Item you can use the name no problem since Items are not UnityObjects. They store the ItemDefinition name when they are initialized.
 
So one thing for the FPS we are building is when a character kills another character in the game, we would like to show a UI element showing what ammo type killed the player. Now, currently, DamageData doesn't pass this, so we modified it to add a string field called ItemName. We are populating DamageData.ItemName with the ItemDefinition.Name field that we created.

An ID is not necessarily useful here unless there's a mechanism to look up the name from the ID that doesn't create GC. We use the item name and translate the name to a sprite name that is shown in the UI element.
 
I see...
For now I would recommend you use a dictionary<itemDefinition, string> and populate it as you need them. (you can pass in the ItemDefinition, instead of the name in the DamageData).
Another solution is to use a string attribute containing a string key for your translation table.
I'll add this feature to the things to investigate since it will touch many things in the system. There is no garantee it will make it in the next few updates because of that.
 
I had initially thought about passing ItemDefinition. However, that would mean creating a dependency on UltimateInventorySystem from UltimateCharacterController, which is probably not desirable. That's why I opted for the string approach instead.

What do you mean by using a string attribute containing a string key for my translation table?
 
I had initially thought about passing ItemDefinition. However, that would mean creating a dependency on UltimateInventorySystem from UltimateCharacterController, which is probably not desirable. That's why I opted for the string approach instead.
I don't see why that would be undesirable, but it's up to you.

What do you mean by using a string attribute containing a string key for my translation table?
I meant an ItemDefinition attribute that you set in the Inventory system editor. You can have one of type string, set a unique name to it and use that as your string. Of course that would require you to set those manually. Or you can have a script that initializes those string attributes when the game starts... but I would go wiht the dictionary approach if I were you. I think that's probably the easiest to implement and the most performant.
 
Top