Forum rules - please read before posting.

How do I open the selected document?

edited January 2022 in Technical Q&A

It's not Auto-open for selection. I want to use the clicked document item in the script and open the document. But I can't find the relevant function.

    public void ChangeDocument()
{
    KickStarter.runtimeDocuments.OpenDocument(KickStarter.runtimeInventory.LastSelectedItem.???); 
    print("Click the" + DocumentName.text);
}

Comments

  • Welcome to the community, @zfh2773.

    Though Documents are defined in the Inventory tab, they are separate to inventory items. Instead of LastSelectedItem, you can access a Document by passing its ID number to the InventoryManager's GetDocument function:

    public void ChangeDocument()
    {
        Document document = KickStarter.inventoryManager.GetDocument (2); // ID = 2
        KickStarter.runtimeDocuments.OpenDocument(document); 
        Debug.Log ("Click the" + document.Title);
    }
    

    A Document's ID number is written to the left of its name in the Inventory Manager.

  • Thank you. I mean, how do I know which document the item slot I clicked on is? Is there a function method that opens the document of the clicked item?

  • Inventory items are not linked to Documents inherently - you need to create a link manually.

    The easiest way to do this is though use of the Document: Open Action inside the inventory item's "Use interaction" ActionList asset, so that clicking it in the menu will open your intended Document.

    This can be handled through scripting, by hooking into the OnMenuElementClick custom event, but not necessary just to get documents to open when clicking items.

    Collected Documents can, however, be displayed as their own list. An InventoryBox menu element's Inventory box type property can be set to Collected Documents, which will cause it to display all Documents collected using the Document: Add or remove Action. Clicking a Document from this list will automatically open it.

  • Thank you! I succeeded! I made a stupid mistake!

    private Document clickedDocument;
    private MenuInventoryBox document;
    private Animator anim;
    private void Awake()
    {
        anim = GetComponent<Animator>();
    }
    
    private void OnEnable() { EventManager.OnMenuElementClick += DocumentClick; }
    private void OnDisable() { EventManager.OnMenuElementClick -= DocumentClick; }
    
    private void DocumentClick(Menu menu, MenuElement element, int slot, int mouseButton)
    {
        if (menu.title == "Document" && element.title == "Document")
        {
            document = element as MenuInventoryBox;
            if (clickedDocument != document.GetDocument(slot))
            {
                anim.Play("DocumentOutof");
            }
            clickedDocument = document.GetDocument(slot);
    
            print("clicked Document is" + clickedDocument);
        }
    }
    
    public void ChangeDocument()
    {
        KickStarter.runtimeDocuments.OpenDocument(clickedDocument);
    }
    
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.