Forum rules - please read before posting.

Referencing inventory items / creating inventory items programmatically

Hi,

I'm relatively new to Unity and new to Adventure Creator, though I am a programmer full-time so I'm comfortable with scripting if necessary. My main problem right now is not knowing the limits of Adventure Creator when it comes to a game like this, since it's not a traditional adventure game - I'm unsure whether I can implement the game mechanics I want in Adventure Creator already, or whether I should look to custom scripting.

The overall idea is that a player examine objects in a room and add them as evidence (inventory). The player can talk to NPCs visual novel style and present the evidence to contradict the NPCs current statement. Similar to Phoenix Wright and Paradise Killer.

I have a few things I'm unsure of how to handle with regards to the inventory.

First, my idea is that the player would be able to examine almost anything that exists in-game and add it as evidence in their inventory. However, adding maybe over a hundred inventory items to the Inventory Manager seems unproductive. I'm thinking a better way would be for every object with a hotspot to also have some script/component that has the details (just name, image, and description is all I need I think) for an inventory item, which is created when that object is interacted with. However, the manual only describes scripting new instances of an inventory item, not creating a new inventory item entirely, so I'm not sure if this is possible.

Second, I don't want inventory items to have interactions, I just want the user to open up the inventory menu and hover over the items to display a description of the item. I think this forum post describes what I want, however, so I can try it out first. Though I'm not familiar with Unity UI, and I didn't find any straightforward tutorials from a quick search, so if anyone has helpful guides there please let me know.

Lastly, I'm wondering if there's a way to reference inventory items directly. This might not be an issue depending on issue #1, but if I do have to create inventory items through the AC Game Editor, then I want to be able to have a game object reference that inventory item in a script, so the game knows which inventory item to add. I could technically do this through just using the name of the item or the index, but that seems extremely fragile if I rename things or shift things around. I would imagine an inventory item as something as something similar to a prefab that I could drop into a script as a public variable? Is that possible?

Thanks!

Comments

  • Welcome to the community, @BokchoyBoy.

    First, my idea is that the player would be able to examine almost anything that exists in-game and add it as evidence in their inventory.

    It is possible to generate new Inventory items through script at runtime, though be advised that doing so will interfere with AC's ability to save the state of your inventory - since these items will no longer be present upon restarting the game.

    If necessary, it is possible to generate items - and assign e.g. Description property data - via an Editor script that extracts such data from e.g. components in the scene.

    If it's not an issue, however, then you can generate a new Inventory Item with the InvItem class:

    InvItem invItem = new InvItem (ID);
    

    Where "ID" is a unique integer used to identify it.

    You can then assign it's various properties:

    invItem.label = "Apple";
    invItem.RebuildProperties ();
    invItem.GetProperty (propertyID).TextValue = "It's a juicy red apple";
    

    Where "propertyID" is the ID value of a Description property defined in the Inventory Manager's Properties tab.

    To give a brief outline of how the Inventory system normally works: the InventoryManager stores a list of InvItem classes, with each InvItem representing an inventory item. At runtime, when an item is added to the Player's inventory, an InvInstance class is created that represents a unique instance of that item - i.e. how much of the item is held, where it's placed, etc. These instances are stored in an InvCollection - a classe that stores all items currently held by a character / container.

    To have a new Inventory item appear in the Player's inventory, then, you first need to create an InvInstance of that item:

    InvInstance invInstance = new InvInstance (invItem);
    

    This can then be added to the Player's Inventory:

    KickStarter.runtimeInventory.PlayerInvCollection.Add (invInstance);
    

    The PlayerInvCollection is the player's InvCollection class, from which you can manipulate exactly what the Player is holding.

    Second, I don't want inventory items to have interactions, I just want the user to open up the inventory menu and hover over the items to display a description of the item

    I'd recommend trying this out with regular inventory items first - i.e. default ones created in the Inventory Manger in the usual way - but it's possible to override the Hotspot label's contents by hooking into the OnRequestMenuElementHotspotLabel event.

    You can use this, for example, to get the property value of an item being hovered over:

    using UnityEngine;
    using AC;
    
    public class MenuHotspotOverrideExample : MonoBehaviour
    {
    
        private void OnEnable () { EventManager.OnRequestMenuElementHotspotLabel += My_Override; }
        private void OnDisable () { EventManager.OnRequestMenuElementHotspotLabel -= My_Override; }
    
        private string My_Override (Menu menu, MenuElement element, int slot, int language)
        {
            if (menu.title == "Inventory" && element is MenuInventoryBox)
            {
                MenuInventoryBox inventoryBox = (MenuInventoryBox) element;
                InvInstance hoverInstance = inventoryBox.GetInstance (slot);
                return hoverInstance.GetProperty (propertyID).TextValue;
            }
            return string.Empty;
        }
    
    }
    

    For more on custom events, see this tutorial.

    Lastly, I'm wondering if there's a way to reference inventory items directly.

    If you're not generating them at runtime, the intended way to reference items is via their ID value. This is a fixed number unique to that item, provided it's been generated using the Inventory Manager. It's the number to the left of it's name in the Editor. You can get the original item declared in the Manager with:

    KickStarter.inventoryManager.GetItem (ID);
    
  • Thanks for the response Chris. I hadn't thought about how runtime creation of inventory items would interfere with loading a save. I'll have to think about it more, but I guess for now I'll just create all the items in the AC Game Editor and reference them by ID. It just seems like it'll get out of hand quickly once I have a lot of different items, but I suppose I'll cross that bridge when I get to it. Thanks again!

  • If you're concerned about referencing items in e.g. Actions, do know that you can group them into categories by putting forward-slashes in their names. i.e. "Fruit/Apple" and "Fruit/Orange".

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.