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

Expose processor on an action

Discussion in 'Input System' started by Arcalise, Sep 15, 2020.

  1. Arcalise

    Arcalise

    Joined:
    Sep 27, 2019
    Posts:
    20
    Just a quick question, Maybe a stupid one!(Hopefully not) Is there a way to easily expose a Vector2 property on a processor of an action? I'm only really seeing the option of cloning the action with the changes needed then deleting the old one which seems... strange.. I cant really find anymore information on processor actions then whats in the documentation and it only mentions making new ones.

    Any help would be much appreciated! Thanks!
     
  2. Arcalise

    Arcalise

    Joined:
    Sep 27, 2019
    Posts:
    20
    After spend a lot of time getting to know the new input system. I do believe that creating a new action, cloning bindings from old action, then adjusting the processors is the way to adjust the processors through script. I dont see any easier way of doing it. It seems strange. So Ive decided not to use processors.

    Though if anyone does figure out an easier way, I'd love to know!
     
    valentin56610 likes this.
  3. PalmGroveSoftware

    PalmGroveSoftware

    Joined:
    Mar 24, 2014
    Posts:
    17
    Hi !
    I was struggling last week end a bit myself to figure out best way to implement invert axis for look in player options and switch depending on prefs.. the only way I could do it at the end was to duplicate axis X input and add processor on it, then switch to correct action instead of trying to add/remove processor, Hopefully such a feature is doable for the team down the road, as I used project assets and do not create actions from code, it is probably a constraint by design ?
    hope it helps for your issue ?
    cheers
     
  4. jukibom

    jukibom

    Joined:
    Aug 31, 2015
    Posts:
    52
    Just chipping in here because this drove me a bit nuts and I finally found out how to get at, and directly manipulate, processors after banging my head against it for a few hours.

    In it's most basic form, this code overwrites all other processors with invert but should be enough to bootstrap whatever you're doing.

    Code (CSharp):
    1.        
    2. // Toggle invert on this binding
    3. var binding = action.bindings[bindingIndex];
    4. // ^ this is a list of structs and we just made a copy!
    5.  
    6. if (binding.overrideProcessors != null) {
    7.     binding.overrideProcessors = null;
    8.     action.ChangeBinding(bindingIndex).To(binding);
    9.     Debug.Log("Remove Invert");
    10. }
    11. else {
    12.     binding.overrideProcessors = "Invert";
    13.     action.ChangeBinding(bindingIndex).To(binding);
    14.     Debug.Log("Add Invert");
    15. }
    16.  
    What was tripping me up was that InputBinding is a Struct and is copied when accessed. The BindingSyntax returned from ChangeBinding has no setter for the binding but To() will replace it. I hope this helps somebody.

    It's painfully simple once you know how and correctly serialises to json and everything but I couldn't find this in the docs anywhere.
     
    Last edited: Apr 26, 2021
  5. cassius

    cassius

    Joined:
    Aug 5, 2012
    Posts:
    124
    I'm wondering if there some trick to this? It's pooched my action map a few times (needed to manually set them up again) and doesn't actually do the invert on the PlayerInput.
     
  6. jukibom

    jukibom

    Joined:
    Aug 31, 2015
    Posts:
    52
    Wait, it actually modified your action set asset? That doesn't sound right!

    The code I used for this is up on github, feel free to have a poke around https://github.com/jukibom/FlyDange...ts/Scripts/Menus/Options/RebindAction.cs#L177

    I'm going to have to extend this for deadzones and stuff soon (it currently only allows for an invert processor and nothing else) and I'm pretty sure input system preview 4 added a proper API for retrieving processors so this is definitely not a great way of doing this! But should give you a bit of a thread to pull on.
     
  7. cassius

    cassius

    Joined:
    Aug 5, 2012
    Posts:
    124
    Turned out this didn't modify the asset - the asset seems to have gotten corrupted somehow else, a few times, and was just a timing coincidence. :( But it also didn't appear to do the invert either. Weird.

    Thanks for the link! Seems you've also got some other useful stuff there too, although right now I'm only looking to invert. Now to troubleshoot why it's not actually working. Will report back if I manage to figure it out.
     
    jukibom likes this.
  8. DSivtsov

    DSivtsov

    Joined:
    Feb 20, 2019
    Posts:
    150
    @jukibom It's Cool
    But I think it will change also the corresponding asset (on disk), not only "variables in current session".
    In the case of temporary change the current "InvertY processor" for some InputAction, I more prefer the other solution:
    Code (CSharp):
    1.                 Vector2 mouseMovement = movementMouseXY.ReadValue<Vector2>();
    2.                 //by default movementMouseXY have processor "InvertY"
    3.                 if (invertY)
    4.                     mouseMovement *= new Vector2(1, -1);
    P.S>
    The Original from
    SimpleCameraController.cs
    line 1 (old Input library), line 2 (the same base on the new Input.System)
    Code (CSharp):
    1. mouseMovement = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y") * (invertY ? 1 : -1));
    2. Vector2 mouseMovement = new Vector2(mouse.delta.x.ReadValue(), mouse.delta.y.ReadValue() * (invertY ? 1 : -1));
     
  9. Mariusz-Born7

    Mariusz-Born7

    Joined:
    Sep 4, 2015
    Posts:
    40
    You can do it like this (I found it on the Internet and it works):

    Code (CSharp):
    1. action.ApplyBindingOverride(new InputBinding {overrideProcessors = "invertVector2(invertX=true,invertY=true)" });
    or

    Code (CSharp):
    1. action.ApplyBindingOverride(new InputBinding { overrideProcessors = "StickDeadzone(min=0.25,max=0.95)" });
    The Processor doesn’t need to already exist for this method to work so you won’t need to add a Processor before you use an Override.

    Binding Overrides don’t stack up, which means that to add a new configuration you don’t need to worry about removing the old Override, it’s simply replaced.

    Multiple Processors can be added to the same Action, they just need to be added in one Override via a comma separated list:

    Code (CSharp):
    1. action.ApplyBindingOverride(new InputBinding {overrideProcessors = "scale(factor=10), invertVector2(invertX=true,invertY=true)" });
     
    ejoflo, florianBrn and jukibom like this.
  10. jukibom

    jukibom

    Joined:
    Aug 31, 2015
    Posts:
    52
    Yep, that's definitely a nicer interface! Shame there's no way to handle the overrides independently and have the string be generated internally