Search Unity

Changing which PlayerInput sends UI navigation events

Discussion in 'Input System' started by replicaJunction, Sep 24, 2020.

  1. replicaJunction

    replicaJunction

    Joined:
    May 28, 2017
    Posts:
    2
    I'm building a turn-based game, and I'm having trouble changing which controller has control of a shared UI.

    In some scenes (like a character setup menu), each player will have their own UI container, and I've gotten things to work just fine using the MultiplayerEventSystem component. However, in the main game scene, I only want one player at a time to be allowed to send navigation events, and I want to change this dynamically to whichever player's turn it is. In other words, on P1's turn, P2 shouldn't be able to navigate menus, but on P2's turn, P1 shouldn't be able to navigate menus.

    I have a manager object / script set to DontDestroyOnLoad, and individual GameObjects below that with each PlayerInput component that joined the game in my character setup. After loading a new scene, I note with some interest that the UI Input Module field in each PlayerInput component changes to "Missing," which makes sense if I unloaded the scene where it was created.

    upload_2020-9-24_14-34-45.png

    If I leave things like this, each PlayerInput seems to send navigation events to my UI.

    I've tried fixing this in code by setting that field to the UI Input Module in the current scene using something like this:

    _player.Input.uiInputModule = inputSystemUIInputModule;


    After making that change, I start seeing an error message in the Player Input component: "The referenced InputSystemUIInputModule is configured using different input actions than this PlayerInput. They should match if you want to synchronize PlayerInput actions to the UI input."

    upload_2020-9-24_14-57-53.png

    For the record, I only have one InputActions asset, so I don't understand why I'm getting that error.

    I tried setting the PlayerInput.uiInputModule field back to null when a player's turn should end, but then I get a NullReferenceException that goes back to the auto-generated C# class from the input actions asset. For reference, the line that actually got the exception was this:

    var newActionMap = m_ActionsAsset.FindActionMap(oldActionMap.name);


    Do I need to be using MultiplayerEventSystem even when I'm using a shared UI like this? If so, I will need to create them at runtime to support a flexible number of players, so how would I "link" them back to the correct PlayerInput components?
     
  2. Rehcub

    Rehcub

    Joined:
    Nov 10, 2020
    Posts:
    1
    You probably found a solution by your self, but for everyone who has the same question (as i did) here is a solution.

    When you have a standard Eventsystem in your scene to control your shared ui you just need to change
    the input.uiInputModule once for every playerinput.
    And than everytime a specific player should control the ui you must set the uiInputModule.actionsAsset to the corresponding playerinput.actions.

    as i understand it every playerinput has its own actionsAsset and only that player can control the ui whitch acionsAssets is set in the InputModule.

    hope this helps someone.
     
  3. Diego_Santiago

    Diego_Santiago

    Joined:
    Jan 5, 2022
    Posts:
    3

    THANKKKKK YOUUU, I couldnt find it on the doc.