Subscribe to specific Actions via InputMap #9635
Replies: 2 comments
-
See also: |
Beta Was this translation helpful? Give feedback.
-
I know this thread hasn't been touched in a while, but I do think this is worth discussing. We began integrating ReactiveX at work and I've gotten a nice taste of event driven programming. While we wouldn't use ReactiveX necessarily for controller input, implementing some kind of observable pattern on the InputMap and exposing the ability to subscribe to it could be really beneficial. For now I'm just reading input in the _PhysicsProcess function like the tutorials have suggested, but that can easily become very cumbersome. I thought about some kind of registry, but you still need to scan for the input press. I think it's far more intuitive for the input action calls to fire events that you can subscribe to. To be honest I was surprised this wasn't already a feature. |
Beta Was this translation helpful? Give feedback.
-
Context
This is a thought experiment about extending the InputMap API for users. I'm not sure if, where or how this is achievable but I feel it's worth looking at.
Currently, what appears to be the way to go about handling input is either by polling the Input API or using one of the event based methods and then testing the event for an action to act upon.
So regardless of how you will approach it, you end up with an if/else construction or similar.
And I feel that there is room for improvement.
Unless I'm missing something. In which case this whole discussion probably makes a lot less sense. But at that point I might learn something new!
Some ideas
It's all in C# because, admittedly, it's my programming language equivalent of comfort food.
But I'm certain it will get the point across regardless :)
So - Subscribing to Actions.
I was thinking there can be a set of Subscribe methods on the InputMap to allow this. Of course, the entire reason for that being to avoid polling and event action testing. Bringing about cleaner input setup code.
Button Actions
InputMap.Singleton.Subscribe("Jump", ActionType.JustPressed, OnJump);
This would invoke my
OnJump()
method when the bound button was pressed this frame. It's parameterless because the information is encoded in the binding. If you will.Now I would not have to be concerned about testing an InputEvent or keep polling the Input API to detect a, perhaps infrequent, button event.
The
ActionType
being an enum describing the different phases of a button action.Now I can subscribe and unsubscribe my action callbacks and inside I know: In this callback all I need to do is to deal with jumping logic.
Axis
It could work for axis as well. I am imagining something like this:
InputMap.Singleton.SubscribeAxis("MoveRight", "MoveLeft", OnMoveHorizontal);
Where
OnMove
would accept a floatvoid OnMoveHorizontal(float axisValue)
.One could also go real wild and combine two axis for the combined input of a joystick, for instance.
InputMap.Singleton.SubscribeAxis("Right", "Left", "Up", "Down", OnMove);
Now
OnMove
would look like this:void OnMove(Vector2 axis)
.The comfort in using that seems tremendous.
I feel like the effort of actually implementing that might also be tremendous, given the way that it conceptually functions.
But something like this could possibly sit in between shortcut input and unhandled key input.
That would imply that anything that wasn't handled by a gui event, shortcut or subscriber is unhandled input and can be pushed forward to the unhandled-input phases.
Of course, this assumed that when a listener is bound to an action, that it will consume the event implicitly.
Certainly there are also other ways to go about this. But I feel like this isn't the worst of them.
And I think that would make a great addition to the input system.
Beta Was this translation helpful? Give feedback.
All reactions