Forum rules - please read before posting.

How to access the number of units in the Inventory?

Let's say the Player has N units of an inventory item. How can I make him use the value of N in an Action?

Example: The player has N gems. How to make him say "I have -N- gems"?


Comments

  • Use custom tokens.

    An inventory item's number of units is stored in it's count variable.

    You can reference an inventory item by it's ID number, e.g. 4:

    int numUnits = AC.KickStarter.runtimeInventory.GetItem (4).count;
  • Sorry, I didn't mention that my question was about the Dialog>Play Speech action. Is it possible to get the inventory count included in the line text?
  • Yes - and that's how.  With custom tokens, you can insert whatever you like into either menu labels or speech text.
  • edited November 2015
    Does that involve making changes in the RuntimeVariables.cs?
  • No - did you look at the tutorial I linked to?  The advantage of custom tokens is that they don't require editing of AC's core files.
  • edited November 2015
    Wow! Got that working. Sorry, missed the tutorial link.
  • I've followed the solution suggested above as closely as I can, as I have the same requirements.

    I however have the following issue.

    I have the following lines as an update function in a script and the token is clearly recognised in my menu label (it also give an exception error when the inventory count is zero - easily fixed, so no problem with that - which again shows that the menu sees the token, but the token is not displaying the actual inventory count, it's just stuck at '1') but it only displays '1' even though my inventory item is set to carry multiple and to carry 5 units on start. The number of items in the inventory decreases by one after every 5 seconds and this is programmed by a timer linked to a trigger. I had hoped that by using the custom token I would be able to have the inventory count decremented in the menu label without having to set a separate global variable just for that, which would be unwieldy since the variable count is being affected by different triggers. Any help?

    void Update()
        {
            int accessCardsCount = AC.KickStarter.runtimeInventory.GetItem(0).count;
            AC.KickStarter.runtimeVariables.SetCustomToken(0, accessCardsCount.ToString());
        }
  • First of all, and I'm sure you've checked this but I have to suggest it for completeness, make sure that your access card item indeed has an ID of zero.

    The next thing to do is to work out if the fault lies with the token, or the Update function.  After the two lines you've posted, insert:

    Debug.Log ("Carrying " + accessCardsCount + " access cards.");

    Check the Console window while running - is it correct there?  If so, let's see how you're displaying this token.  If not, there's an issue with getting the correct count.

    Though you have multiple Triggers that decrease the number of cards, an easier way to sync up a variable would be to have a common ActionList asset that each Trigger calls.  This ActionList would have both the Inventory: Add or remove and the Variable: Set Action, so that one is not called without the other - and only one of each exists.
  • Thanks Chris, the ID is indeed '0' and the Debug.Log does not change, it also returns '1' so it's not correct. Thanks for the tip about using the ActionList asset to set the inventory add / remove and count at the same time, I've since learnt about using ActionList assets that way and only wish I had thought about it earlier for handling the inventory because now I have to go back to all those triggers to reprogram them. 
  • Are you using the Player-switching feature?

    Amend your Debug.Log to show us some more information:

    InvItem item = AC.KickStarter.runtimeInventory.GetItem (0);
    if (item != null)
    {
      Debug.Log ("Carrying item " + item.id + ", label: " + item.label + ", Can carry multiple? " + item.canCarryMultiple + ", Count: " + item.count);
    }


    What's the output?  Please copy/paste it from the Console.
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.