Forum rules - please read before posting.

Variable linked to inventory item

Hi all,

Let me start by saying I have a feeling I am missing something simple so forgive me for the daft query.

I have created a global variable as an integer (it is the result of adding 2 other global variables) I would like that answer to convert into coins in my inventory. Is there a way to do this?

PS Thanks so much for making Adventure Creator it amazes me every time I use it.
AC version 1.79.3

Comments

  • Welcome to the community, @arrakis338.

    A simple custom script can be used to update the amount of items based on an Integer variable value, but you'll need to use it carefully: since the item itself controls its own amount through Inventory: Add or remove Actions, you won't want this link to be constant - but only updated at certain times, i.e. after adding the two other variables together.

    This should do it:

    using UnityEngine;
    using AC;
    
    public class SyncItemCount : MonoBehaviour
    {
    
        public int variableID;
        public int itemID;
    
        public void DoSync ()
        {
            int amount = GlobalVariables.GetIntegerValue (variableID);
            InvInstance existingInstance = KickStarter.runtimeInventory.GetInstance (itemID);
            if (InvInstance.IsValid (existingInstance))
            {
                existingInstance.Count = amount;
                return;
            }
    
            InvInstance newInstance = new InvInstance (itemID, amount);
            KickStarter.runtimeInventory.Add (newInstance);
        }
    
    }
    

    To use, copy/paste into a C# script named SyncItemCount, attach to a GameObject in the scene, then make that object a prefab and remove from the scene. Update its Inspector with the ID values of your variable and item, and then - when you wish to sync the two together - use the Object: Call event Action to call the prefab's (not the script's) DoSync method.

  • Thank you so much.

  • PS forgot to say that solution worked perfectly (not that there was any doubt) and the step by step instructions were perfect.

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.