Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question How to handle different ActionMaps over several gameObjects ?

Discussion in 'Input System' started by Ennocu, Apr 12, 2024.

  1. Ennocu

    Ennocu

    Joined:
    Dec 4, 2022
    Posts:
    10
    Hello everyone, thank you in advance for your time.

    I'm prototyping a small game where 4 players can control different modules/turrets. As each module has different controls, I've created an ActionMap for each using the new InputSystem.
    The players use the PlayerInput component (with the "SendMessage" behavior), with a default map ("Convoy" in this context) and a script to handle default actions like moving themselves or enter in a module. Something classic so far.
    Here's a little preview of my InputAction asset if it could help to understand :



    My question is about the modules. They have their own script with the methods for their respective Actions (in distinct ActionMap) but I discovered that these methods will only be called if the gameObject itself has a PlayerInput component.
    In my head, I believed only my players should/would have the component attached.
    So I'm quite confused about handling my ActionMaps because there is nothing like global active map, even less per players, right ?
    So am I twisting myself in knots over something simple or do you have other recommendations for handling theses players and these module's ActionMaps ?

    Thank you very much for your knowledge and have a good day !
     
  2. Abdo-Reda

    Abdo-Reda

    Joined:
    Oct 8, 2015
    Posts:
    18
    Hi,

    I am not entirely sure how you have set things up. But there is SwitchCurrentActionMap(string) that you can call per playerInput.

    My general flow would be the following
    • Player Performs EnterModule Action
    • You grab the module reference that the player enters, and check its type (Laser, Shield, Generator, DroneControler).
    • Depending on the type, you will switch the action map of the Player, and make its actions call the respective functions on each module.

    This may be a bit too complicated. There is probably a simpler way, but I am not sure.
     
    Ennocu likes this.
  3. Ennocu

    Ennocu

    Joined:
    Dec 4, 2022
    Posts:
    10
    Thanks a lot for you answer !
    Yeah, you're right, the SwitchCurrentActionMap will tell to the PlayerInput component to swith to another ActionMap and to send messages for other actions. But as I understand right now, all actions should be inside the Player script, because the PlayerInput will only send messages to the gameObject it's on.
    My question was more about organizing code now that I think about it, because if wanted my modules to execute some code and answer that message, it's likely I have to add another PlayerInput component on them but doing that might just be overcomplicated for nothing.

    I'm quite reticent to dig through C# events but, as you suggested, this might be the "simpler" way to make actions, regardless what object execute it as long as I take care to put everything together.
     
    Abdo-Reda likes this.
  4. Abdo-Reda

    Abdo-Reda

    Joined:
    Oct 8, 2015
    Posts:
    18
    Ah I get your issue now.

    Well, I think the problem with having another PlayerInput component on another gameObject is that it can mess things up with the PlayerInput & PlayerInputManager. So, if you try to print something like playerCount, you will find that it will count all gameObjects with PlayerInput even if they are not players in your case.

    Btw, in the new beta version of the input system, they have project-based actions or some sort of global actions, I haven't tried them, but maybe its just what you need.

    Also, I found this forum which talked about a similar issue to yours. People there suggested using Unity or C# events. Hopefully this helps, and good luck : )

    Also, one final thing. This is a great article that talk about delegates, events, and actions in unity
     
    Last edited: Apr 12, 2024
    Ennocu likes this.
  5. Ennocu

    Ennocu

    Joined:
    Dec 4, 2022
    Posts:
    10
    Thanks a thousand times for your help and for all of these resources !
    I think I will use them and give a try to C# events, they seems more robust once set up.
    Thanks again and have a great day !
     
  6. Abdo-Reda

    Abdo-Reda

    Joined:
    Oct 8, 2015
    Posts:
    18
    Its fine, I am glad I was able to help. Have a great day and good luck with your project.
     
    Ennocu likes this.