Forum rules - please read before posting.

Different icons for same item

Is there an easy way to have multiple icons for the same item depending on the stack's item count? Thinking about doing this with coins specifically (4 icons: 1 coin, 2 coins, 3 coins and 4+ coins). It should be easy enough to swap the icon via code (I haven't tried yet), but the issue I'm foreseeing is that the player can have several stacks of money in the inventory, and they would each need to have a different icon.

This isn't a must-have, and I might well give up on the idea if it proves too complicated to integrate, but I think it'd look nice.

Comments

  • You could indeed change it through script, but each item instance relies on the original item data for its graphic. Doing so would affect each instance of that item.

    What you could do is create 4 coin items, each with their own graphic, and then have a script search for instances of them in your inventory, and replace them with the correct item based on how many are carried.

    Untested, but something like this:

    using System.Collections.Generic;
    using UnityEngine;
    using AC;
    
    public class CoinItemGraphics : MonoBehaviour
    {
    
        public List<int> coinItemIDs = new List<int> ();
    
        private void OnEnable ()
        {
            EventManager.OnInventoryAdd += UpdateCoinItems;
            EventManager.OnInventoryRemove += UpdateCoinItems;
        }
    
        private void OnEnable ()
        {
            EventManager.OnInventoryAdd -= UpdateCoinItems;
            EventManager.OnInventoryRemove -= UpdateCoinItems;
        }
    
        private void UpdateCoinItems (InvItem invItem, int amount)
        {
            bool didUpdate = false;
            for (int i = 0; i < KickStarter.runtimeInventory.PlayerInvCollection.InvInstances.Count; i++)
            {
                InvInstance invInstance = KickStarter.runtimeInventory.PlayerInvCollection.InvInstances[i];
                if (InvInstance.IsValid (invInstance) && coinItemIDs.Contains (invInstance.ItemID))
                {
                    int correctIndex = Mathf.Min (invInstance.Count - 1, coinItemIDs.Count - 1);
                    if (invInstance.Count > 5)
                    {
                        int correctItemID = coinItemIDs[correctIndex];
                        if (correctItemID != invInstance.ItemID)
                        {
                            InvInstance replacement = new InvInstance (correctItemID, invInstance.Count);
                            didUpdate = true;
                        }
                    }
                }
            }
    
            if (didUpdate)
            {
                PlayerMenus.ResetInventoryBoxes ();
            }
        }
    
    }
    
  • edited September 2022

    Thanks, Chris! That's a good idea, but I think I'll set it aside for now because I think it conflicts with two custom actions I wrote just yesterday:

    (1) Custom 'add': adds a user-specified number of items by going through the InvInstance array of the item in the inventory, checking for incomplete stacks, and adding items to the incomplete arrays. If more items are being added than the existing InvInstances of said item can take, the action checks if there are less than 8 occupied inventory slots (I need an inventory size limit for my game). If yes, create a new instance. If not, don't add the rest (items are lost).

    (2) Check for free space: the user selects an item from a dropdown menu and inputs a number. The action goes through the item's InvInstance array, subtracting the item count from each instance from the max stack capacity, and adding those numbers together (freeSpace int). Then it determines the number of empty slots (the inventory size limit minus the number of all invinstances in the inventory), and adds the maximum stack size * the number of empty slots to freeSpace. Finally, it compares freeSpace to the number the player has entered, returning true or false. It reads "Is there space for [4] [coins]?" and splits the actionlist node according to the result.

    Basically, I think that having items with a different ID in the inventory that are actually the same item and should be counted as such will be a bit of headache in terms of wiring the gameplay. I think I'll set the idea aside for now and return to it later if I have time.

  • A suggestion for future development though, not sure how feasible it is:

    Allow item icon animation in the exact same way as cursor animation, and then allow us, through script, to switch to a specific still frame for a given item instance. This would allow us to (1) animate all icons (a cool feature!), and (2) display different icons under different conditions via script.

  • Thanks for the feedback.

    If your Custom 'add' Action is a reference to your bug report here, this should have been addressed in v1.75.8.

  • Thanks for that fix! The containers are working perfectly now. The custom add action is related to it only in the sense that I wanted the same behaviour for the player inventory (you can limit the number of slots for containers, but not the player inventory, as far as I'm aware? I'd feel silly if this feature now existed in AC; it didn't used to).

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.