Forum rules - please read before posting.

ls there any way to make the inventory items show their freshness?

edited November 2022 in Technical Q&A

https://streamable.com/zf4603
https://streamable.com/nihwe8
Like the Don't Starve, each slot of the container shows its inventory item's freshness. In my project,I have given each inventory item a freshness property, but l don't know how to translate this attribute into a visual effect.
Are there any direct or indirect ideas that can help me achieve this?

Comments

  • You can have a custom script read the item's "freshness" property, and alter its main texture accordingly.

    What kind of property are you using, and what are the threshold values that would cause an item to change graphic?

  • I'm using integer property for the "freshness".
    If there are four kinds of freshness, then each inventory item needs four corresponding textures, which feels a great consumption of resources.
    Can I change the background color of the slot to indicate freshness without changing its main texture?

  • You can use a script to modify an Item's texture, according to its property value:

    using UnityEngine;
    using AC;
    
    public class FreshnessTextures : MonoBehaviour
    {
    
        public FreshnessItem[] items;
    
        public void RebuildFreshness ()
        {
            foreach (FreshnessItem item in items)
            {
                item.RebuildFreshness ();
                PlayerMenus.ResetInventoryBoxes ();
            }
        }
    
    
        [System.Serializable]
        public class FreshnessItem
        {
    
            public string itemName;
            public string freshnessProperty = "Freshness";
            public Texture[] textures;
    
            public void RebuildFreshness ()
            {
                if (textures.Length == 0 || string.IsNullOrEmpty (itemName)) return;
    
                InvInstance[] invInstances = KickStarter.runtimeInventory.PlayerInvCollection.GetAllInstances (itemName);
                foreach (InvInstance invInstance in invInstances)
                {
                    int freshness = invInstance.GetProperty (freshnessProperty);
                    freshness = Mathf.Clamp (freshness, 0, textures.Length - 1);
                    Texture texture = textures[freshness];
                    invInstance.InvItem.tex = texture;
                    break;
                }
    
            }
    
        }
    
    }
    

    You can attach this to a GameObject and fill in the texture data for each item, with each texture slot representing the freshness at that index (texture 0 => freshness = 0, texture 1 => freshness = 1) etc.

    Whenever you update the freshness property, call this script's RebuildFreshness function to update their textures.

  • Oh! Chris, I haven't read the script above closely yet, but from your explanation, does that mean it could be modified/repurposed to allow item stacks with different quantities to display different icons? When we spoke about it, you suggested having different items with different icons and then using a script to replace them, but that would clash with the scripts I use to handle several of my containers/inventories, so I decided against it.

  • It comes down to whether you have multiple instances of the same item in the same inventory, because the script above modifies the original item's texture.

    If you only have one of each, then the above should be fine - but using different items for each "state" would be the more robust approach as you could then have multiple entries of the same (perceived) type in the inventory.

  • Ah, right, then it still comes down to that one limitation. I've just noticed the "invInstance.InvItem.tex" line. Thanks for the answer.

    How complicated would it be to add an option like "invInstance.tex" which defaults to the InvItem texture, but can be modified without changing the InvItem's or other InvInstances' textures?

  • edited November 2022

    Possibly not so much. I shall give it some thought.

    Such overrides, however, would likely not carry over to the save system - so they'd need to be re-applied after loading a save-file.

  • Thank you two. I know how to write it now. I should use different items for each "state".

  • I can confirm that the next release will include the ability to override an item's textures on a per-instance basis.

  • That's great news. I'm looking forward to it!

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.