Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

InputSystem - StackOverflow using SwitchActionMap

Discussion in 'Scripting' started by Johnny9Rodriguez, Apr 13, 2021.

  1. Johnny9Rodriguez

    Johnny9Rodriguez

    Joined:
    Mar 3, 2019
    Posts:
    10
    Hi everyone,

    I'm trying to get used to the InputSystem working on a free flying camera, but I have a problem when switching the ActionMaps.

    Basically I have a "GameDirector" object that holds the PlayerInput with my InputActionAsset. It then invokes unity events which I've linked to the objects with its corresponding methods to perform.

    upload_2021-4-13_19-30-0.png

    The methods I have assigned all work perfectly and I am able to move the camera and fly around, etc.

    This is for my default map "Fly Camera". I have a second ActionMap called "Cube" that holds for now only two actions "Ko" and "Switch".

    "Ko" is linked to a method that for now only prints something in the console.

    The problem now is that I get a StackOverflowException everytime I press the button that'll perform the switch. After the exception was raised everything works and I can, for example, press the button "K" to print something in the console. I also can switch back - wait until the exception was raised - and then fly around.

    upload_2021-4-13_19-35-54.png

    Am I doing something wrong or is there a workaround or something?

    Eventually I want to use this to change the ActionMaps according to the state the game is in. E.g. gameplay, inventory, menu, etc.
     
  2. Johnny9Rodriguez

    Johnny9Rodriguez

    Joined:
    Mar 3, 2019
    Posts:
    10
    Oh, I also tried to change the interaction type of the switch button to "Press only". In the exception it says something about "canceled" so I figured that might change something.

    It does ... but I still get an error.

    upload_2021-4-13_19-40-42.png
     
  3. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    It'll be helpful if you add the code that's supposed to handle the switching logic, probably PlayerInput.SwitchCurrentActionMap?
    Could you also include the entire stack trace that's logged to the console? It might contain important information, too.

    Have you already stepped through your code with the debugger to see if you can find some infinite recursion or cycle calls? Have you added some logs to your logic?
     
  4. Johnny9Rodriguez

    Johnny9Rodriguez

    Joined:
    Mar 3, 2019
    Posts:
    10
    There's not really code involved here. I can easily recreate this error.

    I use version 2020.3.1f1, create a new 3D project. Install InputSystem 1.0.2 then restart. I create a new object with a PlayerInput component and create a new InputActionAsset. I create 2 maps and map a button to each. Then in the inspector with the object selected I change the behavior type to InvokeUnityEvents and in the corresponding field (like in the og image) I drag the object with the component itself and choose in the drop down field PlayerInput > SwitchCurrentActionMap and type in the name of the action map I want to switch to in the argument field.
     
  5. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Oh I see, my fault, sorry.

    I thought that was your own custom handling, I didn't realize it's a package offered by UT.

    According to the issue tracker it's known & fixed in 1.1.0 but I guess that version is not available in the package manager yet. There're probably workarounds, too, but I haven't switched to the new input system so I'm not familiar with all the issues. Here's the repo of the input system if you want to follow the development.
     
    RogDolos likes this.
  6. Johnny9Rodriguez

    Johnny9Rodriguez

    Joined:
    Mar 3, 2019
    Posts:
    10
    No problem :) I just found a workaround. I wrote a script for the switch to only perform just after the button press started ("context.started"). There seems to be a problem with that. But thank you for reading and trying :D
     
    RogDolos likes this.
  7. RogDolos

    RogDolos

    Joined:
    Oct 16, 2012
    Posts:
    42
    I managed to work around this by switching to PlayerInput invoking C# Events instead of Unity Events. This way, "context.started" still works.

    Sigh tho, big sigh. =/