Forum rules - please read before posting.

Item Deselect

edited June 2018 in Technical Q&A
Hi,

I've started creating my own custom inventory for a desired special behavior. It's an Unity UI + AC invetory system.
I've run into a problem with deselecting items.

I'm using this because i'm hiding the item's sprite in the inventory while the item is being selected.

Why the OnInventoryDeselect event runs 10 times when an item is being deselected?
Printing a value inside this also cause the vale sometimes print empty.

Why the OnInventoryDeselect event isn't running at all when an item is being deselected by combine action?

Can i somehow prevent the default right click deselect?
Right click on button ( inventory slot ) calls RunExamineInteraction() - I want to be able to prevent this if any item is currently being selected.
if(KickStarter.runtimeInventory.SelectedItem == null) Won't do the trick as the item gets deselected before running this code.

Thanks.

Unity 2018.1.1f1
AC v1.63.2

Comments

    • Why the OnInventoryDeselect event runs 10 times when an item is being deselected?  Printing a value inside this also cause the vale sometimes print empty.
    It could be a bug, but I'll need to know more details before I can clarify.  How are you de-selecting the Inventory item?  If you can share the stacktrace via a Debug.Log statement in your event to output it to the Console, that would be great.

    • Why the OnInventoryDeselect event isn't running at all when an item is being deselected by combine action?
    The event should be called when an item is deselected as its own command.  The OnInventoryCombine event can be used to run custom code when combining items.

    • Can i somehow prevent the default right click deselect?
    What's your Interaction method?  Look for the Right-click active item field in the Settings Manager, which by default is set to Deselects Item.

    • Right click on button ( inventory slot ) calls RunExamineInteraction() - I want to be able to prevent this if any item is currently being selected.
    You could begin your Examine interaction with an Inventory: Check selected Action and only run subsequent Actions if none is selected.

    • if(KickStarter.runtimeInventory.SelectedItem == null) Won't do the trick as the item gets deselected before running this code.
    Try KickStarter.runtimeInventory.LastSelectedItem - that'll retain the value of SelectedItem's last non-null value.

  • edited June 2018
    Not sure what a stacktrace is, hope this helps:
    http://pasteall.org/996685

    Anyway the OnInventoryCombine runs 10 times as well. Not sure if i'm doing something wrong or it's some global bug. ( OnMenuTurnOn runs once )

    I'm not gonna need those though as i solved the "deselect detection and item's sprite reappearing" in the Update method
    http://pasteall.org/997110.

    Interaction method:
    I'm using Contex sensitive

    There's the deselect and examine options in the settings manager. A do nothing option would be what i want. ( I would then have to make my own deselect action for the right click though )

    • You could begin your Examine interaction with an Inventory: Check selected Action and only run subsequent Actions if none is selected.
    The Item still gets deselected first before executing the right click action that calls the examine action.

    I thought that i could set a variable to true when i set item as selected and then when i "deselect" the item in the Update method i could Invoke this variable for a few ms before setting it to false and check this variable under the right click action. Not sure how clean solution this is though.
    (The first tries ignored my bool var completely for some reason inside the right click method - it always returns false)

  • That's the stacktrace, yes.  And that's the trace that's being called 10x?

    There's a hard-check in the SetNull method that's triggering it to ensure that it doesn't run if no item is selected - so I don't know how the value could be empty.  If you'd like to post the code you're using to hook into the event, I'll look to see if anything's wrong.

    What I'm confused about is why you're saying RunExamineInteraction is being run - that's a function only present for the benefit of custom scripts - it's not called by any of AC's own scripts.

    Also, if you're using Context Sensitive mode, then right-clicking an item while another is selected will cause the item to become de-selected - not the new one to be examined.  I can look into adding additional options here, but I feel like not all the details are on the table yet.  What else in the way of custom code have you added?
  • edited June 2018
    I think there's a confusion about what i'm doing.
    I'm not using AC's menu system. This is Unity UI only. But it works with the AC's inventory system.

    A panel with 10 buttons (slots). Each slot has its child image object which represents the item's sprite.

    Left click on slot calls KickStarter.runtimeInventory.SelectItemByID(itemID);
    Left click if any item is selected calls KickStarter.runtimeInventory.SelectedItem.CombineWithItem(KickStarter.inventoryManager.GetItem(itemID));
    Right click calls KickStarter.inventoryManager.GetItem(itemID).RunExamineInteraction();

    Why?
    1. I want to have a scale animaton of the item's sprite when being hovered
    2. I want to hide shift buttons if shifting is not avaiable
    3. I want to hide inventory on pointer exit instantly but open with pan animation but not while pointer enters the whole inventory area but just a narrower area at the top of the screen.
    And it all seems to be working just fine expect the right click on item when another is being selected. That calls the examine action everytimes.

    But i have found a workaround. When i select an item a set a bool value to true.
    When i deselect an item i set this bool value back to false with delay using Invoke function.

    The right click then checks if this value is false and only then calls the examine action. So only a second click works as there's a few ms delay.

    I just don't see this as "clean" solution. I would rather disable the AC's right click deselect completely and make a custom solution.

    EventManager

    The code is simple as this:

    private void OnEnable() {
        EventManager.OnInventoryDeselect += OnItemDeselect;
    }

    public void OnItemDeselect(InvItem Item) {
        Debug.Log(Item.label);


    Item's label gets printed 10 times in the console.

  • If you're not un-setting the event when the game ends, it may be that they're becoming compounded.  It's best practice to do this, either way:

    private void OnDisable() {
        EventManager.OnInventoryDeselect -= OnItemDeselect;
    }

    (A restart might be required too).

    If adding a "Do nothing" option to the "When right-click inventory" field is all you need, I can certainly add that.  However, the three reasons you listed should all be viable with AC's standard inventory menu:

    1. A Unity UI linked to AC's Menu manager will still obey the "Transition" settings you define in the UI Button component - setting it to Animation should allow you to play a "scaled up" animation of the Button's RectTransform.

    Alternatively, you can hook into the OnMouseOverMenu custom event to determine if the mouse is over an inventory slot and do something more custom.

    2. AC Button elements that have Click type of Offset Element Slot have an additional Only show when effective? option.  Just be sure to also set its When invisible field to Disable Object when working with Unity UI.

    3. If a Menu's Appear type is set to Manual, then you can use a custom script to control when its shown.  See the Manual's "Menu scripting" chapter and Scripting Guide's Menu page for more, but the following script can show/hide a Menu according to the mouse position:


  • edited June 2018
    I did use the onDisbale method as well i just didn't posted here to keep the post shorter. I also have just one scene in my game yet.

    I don't know what you mean by restarting.

    Please then excuse my lack of knowledge. I didn't know all of these were possible with the AC's menu system. Yes i think the "Do nothing" option could come handy.

    I must have been doing something wrong as i tried to call a click action on the button in the inventory. ( Unitu UI prefab inventory ) And it didn't seem to be working before, now i have tried it again. Yes it does work the way you say.

    I'm probably gonna stick with the Unity UI only for now as i like it better when only the item's sprite is being scaled not the whole interaction area of the slot itself. But i'm glad to see how much customizable Adventure Creator is.

    Thank you for the info & help you have provided.
  • By restarting, I just meant restarting Unity - so that any "hooks" into the custom event are refreshed.

    Is that script present on each UI Button?  The OnEnable method will hook into the event for each component individually - so 10 instances of the same component would cause the event to be called 10 times.
  • Yes. You have just solved the mystery. Alright in that case it has an easy solution then. Thanks :smile:
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.