Forum rules - please read before posting.

Determine when Inventory Item is Deselected via Mouse Click

I want to be able to "drop" inventory items into the world, if the player no longer wants to carry the inventory item.

I decided that if an Inventory Item is selected and not hovering over an active hotspot, and the player clicks the mouse, the player should drop the item. 

Please see the following example, the sword cursor is circled with green:

Is the image, the sword has been selected and is NOT hovering over any hotspots. So if the player clicks the mouse, the sword will be de-selected and the cursor will return to normal. At this event, I want the player to "drop" the sword into the world.

So the question is, how do I know when this event has occurred? 

I think the event, OnInventoryDeselect, could occur for many circumstances, not only the situation where user has clicked the mouse button while cursor is an active Inventory Item.

How do I know when the player:
A. Has selected an inventory item (cursor is an inventory item)
B. The cursor is not currently over any hotspot (no interaction)
C. The mouse button has been pressed to cause the inventory item to become deselected.

Thank you!

Comments

  • A. Has selected an inventory item (cursor is an inventory item)

    The currently-selected item can be read with:

    AC.KickStarter.runtimeInventory.SelectedItem;

    (See the Manual's 'Inventory scripting' chapter for more)

    B. The cursor is not currently over any hotspot (no interaction)

    The currently-active Hotspot can be read with:

    AC.KickStarter.playerInteraction.GetActiveHotspot ();

    (See the Manual's 'Interaction scripting' for more)

    C. The mouse button has been pressed to cause the inventory item to become deselected.

    The OnInventoryDeselect event you mentioned may work in conjunction with checking the condition of B.  The even includes the deselected item as a parameter, so A wouldn't be necessary in that case.  Otherwise, just reading Unity's standard Input.GetMouseButtonDown should work.

    You'll also need to check the game-state to prevent anything unwanted during e.g. Cutscenes:

    AC.KickStarter.stateHandler.IsInGameplay ();

    (See the Manual's 'Custom scripting' chapter for more)
  • Thanks Chris, I have this working with the following check in PlayerInput.cs at line 291:

    else if (CanClick ())
    {
        dragStartPosition = GetInvertedMouse();
        ResetClick();
        ResetDoubleClick();
        // Check for dropping an item
        if (KickStarter.runtimeInventory.SelectedItem != null) {
            if (KickStarter.playerInteraction.GetActiveHotspot() == null) {
                GameController.control.DropItem();
            }
            else {
                mouseState = MouseState.SingleClick;
            }
        }
        else {
            mouseState = MouseState.SingleClick;
        }

    I modified PlayerInput.cs because I didn't want a "click" to occur, as that would cause a "Click Marker" visual to show, and perhaps trigger player movement. Any advice on how to do this a better way, or avoid modifying AC scripts?
  • If you want to intercept the mouse click to prevent other scripts from using it, call:

    KickStarter.playerInput.ResetClick ();
    KickStarter.playerInput.ResetMouseClick ();

    If you call it in a new script, you might have to play around with its Execution Order so that it's Update() function runs before StateHandler's every frame.  StateHandler calls the Update() functions of all of AC's main scripts.
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.