Forum rules - please read before posting.

How to toggle inventory and cursor without pausing and how to change the cursor icon when unlocked?

edited January 2023 in Technical Q&A

I'm making a first-person direct control game using AC UI. My inventory is a small bar at the bottom of the screen. At first, I had it set to appear On Gameplay and I used a ToggleCursor input to unlock the mouse. That was good at first but I have decided that I don't want to see the inventory all the time. I also want to be able to toggle it on or off via action lists which doesn't seem to work with On Gameplay.

So I changed the inventory appear type to On Input Key. The problem is I also want the appearance of the inventory to unlock the cursor but without pausing the game. And I want to make sure that if I use Menu: Change State in an action list to hide the inventory, that would also toggle the cursor. Basically, anytime the inventory is open the cursor is unlocked. If it is closed the cursor is locked.

Lastly, I was wondering if there is a way that I can toggle the cursor icon, depending on if the cursor is locked. Like in normal gameplay, I am using a tiny dot crosshair but when the cursor is unlocked I want to use a hand icon.

Thanks!

Comments

  • edited January 2023

    I also want to be able to toggle it on or off via action lists which doesn't seem to work with On Gameplay.

    You can lock a Menu to prevent it from showing even when its "Appear type" condition is met.

    If you set it back to During Gameplay, you can use the Menu: Change state Action's "Lock Menu" / "Unlock Menu" commands to turn it on and off. These are best run via Active Inputs, so that you can control exactly when such input has an effect.

    Basically, anytime the inventory is open the cursor is unlocked. If it is closed the cursor is locked.

    The Player: Constrain Action can be used to disable free-aiming, which should also release the cursor. If necessary, the Engine: Manage systems Action can be used to lock/unlock the cursor.

    To run specific Actions when a menu is turned on or off, regardless of how, you can assign ActionList assets in the menu's ActionList when turn on / ActionList when turn off property fields. Set these ActionLists' When running fields to Run In Background to prevent them from interrupting gameplay.

    I was wondering if there is a way that I can toggle the cursor icon, depending on if the cursor is locked.

    You can override the cursor when over a specific Menu element, but otherwise you'd have to rely on some simple custom scripting.

    Try this script (InventoryCursor.cs) in your scene: it hooks into the OnMenuTurnOn/Off custom events to alter the main cursor's texture when the Inventory menu is turned on and off:

    using UnityEngine;
    using AC;
    
    public class InventoryCursor : MonoBehaviour
    {
    
        public Texture inventoryIcon;
        private Texture normalIcon;
    
        private void OnEnable ()
        {
            normalIcon = KickStarter.cursorManager.pointerIcon.texture;
            EventManager.OnMenuTurnOn += OnMenuTurnOn;
            EventManager.OnMenuTurnOff += OnMenuTurnOff;
        }
    
        private void OnDisable ()
        {
            EventManager.OnMenuTurnOn -= OnMenuTurnOn;
            EventManager.OnMenuTurnOff -= OnMenuTurnOff;
        }
    
        private void OnDestroy ()
        {
            KickStarter.cursorManager.pointerIcon.ReplaceTexture (normalIcon);
        }
    
        private void OnMenuTurnOn (AC.Menu menu, bool isInstant)
        {
            if (menu.title == "Inventory")
            {
                KickStarter.cursorManager.pointerIcon.ReplaceTexture (inventoryIcon);
            }
        }
    
        private void OnMenuTurnOff (AC.Menu menu, bool isInstant)
        {
            if (menu.title == "Inventory")
            {
                KickStarter.cursorManager.pointerIcon.ReplaceTexture (normalIcon);
            }
        }
    
    }
    
  • Thank you. This is very helpful. I was able to get exactly the functionality that I wanted. There is one small issue with the InventoryCursor script. It works perfectly when I test the game but when I stop the game, the main cursor (normalIcon) inherits the texture assigned to the inventory cursor script (inventoryIcon).

    So each time after I test the game, I have to go into the AC Game Editor and change the main cursor back to my chosen default.
  • edited January 2023

    Maybe I didn't think this through completely because I have another issue. When I use other in-game menus the game uses the main cursor icon rather than my inventory pointer. I guess what I really wanted was a dot crosshair when the cursor is locked and a hand pointer anytime when the cursor is unlocked, regardless of what menu is in use. Sorry, I was unclear about that.

    Would it make more sense to use the pointer as my main cursor and simply replace it with the dot when the cursor is locked? Of course, I still want it to change when over a hotspot...

    Otherwise, I guess I could add all my mouse-driven menus to the InventoryCursor script so it triggers the cursor swap for any menu.

    What is the best way to accomplish this?

  • edited January 2023

    I figured out how to add other menus to your InventoryCursor script. Not sure if this is the best way to do it but it works. I could probably simplify things with a script like:

    On cursor lock replace with custom cursor.
    On cursor unlock revert to normal cursor.

    But I'm not sure how to refer the cursor being locked in C#

    And I still have the issue of the custom cursor overwriting the main cursor every time I exit the game. Not sure if this would be alleviated by using this other approach.

  • But I'm not sure how to refer the cursor being locked in C#

    Currently there is no custom event hook for the case that the cursor is locked or unlocked, but I saw the need for this as I was writing the script above. I shall look to see if this can be introduced in the next update.

    And I still have the issue of the custom cursor overwriting the main cursor every time I exit the game.

    That's my fault - the script has an OnDestroy function to revert the texture after exiting, but it was just setting it back to the inventory icon. I've amended it above - give it a try now.

  • That fixed it. I feel a little dumb for not figuring that out myself. All is working fine now.

    For people making first-person games, the ability to set a locked cursor as distinct from the main cursor could be a useful feature to have in the game editor.

    Thanks for everything!
  • I have added a script to the AC wiki that allows you to separate the two more easily:

    https://adventure-creator.fandom.com/wiki/Adding_a_separate_crosshair

  • I haven't tested this yet but wouldn't checking Hide cursor when locked in screen's centre? prevent the cursor from appearing over hotspots? That wouldn't work for my purposes because, while I am using a minimal crosshair when the cursor is locked, I still need the cursor to change over hotspots to show the player what they can interact with.

    For now, I'm happy with how things are working. Thanks for your time looking into this issue!

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.