Nodragem Posted July 22, 2018 Share Posted July 22, 2018 Hello, I am starting to use BabylonJS and can't find an input manager in the documentation ? Let's say I have a main character that can jump and I want my game to be playable on PC, mobile without/with a controller. I would like the main character to jump whenever the player presses the xbox controller's button "A" or the keyboard's key "space" or tap on the screen. Is it possible to register these events under the same name "Jump" and write something like the following? if inputManager.GetButtonJustPressed("Jump"){ mainCharacter.jump() } Thank you very much for the help Quote Link to comment Share on other sites More sharing options...
brianzinn Posted July 23, 2018 Share Posted July 23, 2018 search for gamepad - also keypress events: https://doc.babylonjs.com/how_to/how_to_use_gamepads The observable is called when a controller is dis/connected, then you attach the behaviour you want for your player. Hope that makes sense. Otherwise make a playground. Quote Link to comment Share on other sites More sharing options...
Nodragem Posted July 26, 2018 Author Share Posted July 26, 2018 If I understand, you would manage the keyboard with the ActionManager and the gamepad with the GamepadManager/Observable? The documention says: Quote There are two features of Babylon.js that handle events, actions and observables. It is also possible to use plain Javascript. It seems to me that: - there is no actions in the ActionManager related to the gamepad, - there is no observable related to the Keyboard - there are actions in the ActionManager related to the keyboard, - there is an observable related to the Gamepad Maybe it could make sense to have actions/observables for both? Furthermore, it could be nice to have actions/observables that do not care from where the event is trigger. For instance: I would attach an event "Jump" to the Button A of my Gamepad and to the space key of my keyboard. Then, I would either use an Observable or the ActionManager to trigger some code when the "Jump" event is triggered. That way you could easily change your input settings by changing which inputs (e.g. key press, button press, click) triggers the "Jump" event. Quote Link to comment Share on other sites More sharing options...
JohnK Posted July 26, 2018 Share Posted July 26, 2018 16 minutes ago, Nodragem said: there is no observable related to the Keyboard In the API http://doc.babylonjs.com/api/classes/babylon.scene#onkeyboardobservable from scene observables http://doc.babylonjs.com/api/classes/babylon.scene Playground example using WASD keys https://www.babylonjs-playground.com/#K1LI48#6 Quote Link to comment Share on other sites More sharing options...
Guest Posted July 26, 2018 Share Posted July 26, 2018 If you want to add a gamepad trigger for actions, I will gladly merge it as I think this is a good idea to add Quote Link to comment Share on other sites More sharing options...
Nodragem Posted July 28, 2018 Author Share Posted July 28, 2018 Thank you JohnK, however, if gamepads has the right to have their own BABYLON.GamepadManager(), wouldn't be more consistent to move the keyboard observable to a BABYLON.KeyboardManager()? Why not do the same for the pointer and make a PointerManager()? Thank you Deltakosh, I am happy to try where do I start (and what about the above?)? Next to that, do you think it could be nice to have events/observables that aren't specific to keyboard/gamepad/mouse? We could have an InputManager creating a layer of abstraction with the input sources. The InputManager could be just a relay that would registers to many Observables and be itself an Observable. For instance, BabylonJS's users would register: - "Keyboard KeyDown Space" as "Jump" - "TouchScreen PointerDown" as "Jump" - "Gamepad ButtonDown A" as "Jump" Then, when the users are writing their jump mechanics, they wouldn't need to check for all these input sources, they could just test: if inputManager.wasJustTriggered("Jump") { doJump() } or: inputManager.onEvent.add((event_name, state)=>{ //event was triggered if(event_name === "Jump") doJump() }) (Note that there could even be a Json import/export to store the input settings) We could also have a common interface for directional input (controlling up/down with gamepads' analog stick vs keyboards' keys) I am happy to try doing a PR Quote Link to comment Share on other sites More sharing options...
aWeirdo Posted July 28, 2018 Share Posted July 28, 2018 I'm not sure i understand the need, seems like reinventing the wheel? no? observers can do everything you want, as you write the logic. Observers simply tell you, "this key was pressed" "this key was released" http://playground.babylonjs.com/#J7HA4U and here with a "keyIndex", space = "jump", w = "forward" http://playground.babylonjs.com/#J7HA4U#2 The json idea is nice, but a simple extension based on keyboard observers seems more reasonable... I might be misunderstanding something.. Quote Link to comment Share on other sites More sharing options...
brianzinn Posted July 28, 2018 Share Posted July 28, 2018 I think he's asking for more than key observables, but a way to map and integrate the various input sources. ie: space bar, trigger button or left mouse click - triggers a "jump" event. I am not aware of a way to do a clean mapping from different sources. The webxr spec looks like it has provided space for 'gestures', so it's only getting more complicated. It doesn't feel to me like a reinventing of the wheel, but adding an abstraction layer as nodragem said. Although I'm not sure it belongs in the core, seems not too hard to write a mapping. // setup. I just made up variables. inputManager.addAction(PointerEvent.Down, MouseButton.Left & MouseClick.Single, "jump"); inputManager.addAction(KeyboardEvent.KeyDown, 32 /* space */, "jump"); inputManager.addAction(GamePad.TriggerButton, "jump"); // add listener inputManager.eventObservables.add("jump", (evt) => { doJump(); }) All the needed code is in the Observable<T> already. There is a mask there that kinds works the same - as a 'filter' on what is triggered, but this is for strings. I think just a simple object dictionary to list would suffice. The observable could be pattern matching (ie: "jum*") or function as well. inputManager.eventObservables.add((evt) => evt.name.startsWith("jum"), () => { doJump(); // ie: will also be triggered on "jumparound" }) I would start here - you need something like a 'mask': https://github.com/BabylonJS/Babylon.js/blob/master/src/Tools/babylon.observable.ts#L87 edit: this input manager also needs to listen for gamepad de/registrations. that code is not too much either. Nodragem and GameMonetize 2 Quote Link to comment Share on other sites More sharing options...
brianzinn Posted August 9, 2018 Share Posted August 9, 2018 @Nodragem If you do end up building something for this - there is an XR concept called "Action Sets" and I think that is a useful concept to include with any implementation. Basically, depending if you are playing the game or navigating the menu (context), then the bindings are different. ie: trigger button is "jump" during game play, and "select" when navigating menus. If you watch this video for a couple of minutes then you can see it being explained with kittens (yes, kittens) https://www.youtube.com/watch?v=U-CpA5d9MjI&feature=youtu.be&t=31m23s Also, there is a question at 57:30 about action sets, where he discusses the global action set and I think that is key as well. I am researching XR, so wouldn't expect you to make it that far! Quote Link to comment Share on other sites More sharing options...
Nodragem Posted August 10, 2018 Author Share Posted August 10, 2018 Yep, I see what you mean. That would definitely be nice to have! I just need to find some time to start program it Sebavan 1 Quote Link to comment Share on other sites More sharing options...
Nodragem Posted September 8, 2018 Author Share Posted September 8, 2018 I just had a look at the Unity toolkit (https://doc.babylonjs.com/resources/intro) and the Scene Manager extension that it uses (https://github.com/BabylonJS/Extensions/blob/master/SceneManager/src/babylon.scenemanager.ts). Unity does have an high-level/flexible input manager as described above (see https://docs.unity3d.com/Manual/class-InputManager.html ) : Hence I was thinking that maybe the Unity toolkit / exporter already deals with Unity Input Manager, which would mean that somewhere in the Scene Manager extension, there is already a high-level/flexible Input Manager. The question is: Do we need to program an input manager if it was already implemented in the Scene Manager? However, I tried to read through the source code and nothing looks like a Hashmap or structure, which I would have expected to see. Quote Link to comment Share on other sites More sharing options...
brianzinn Posted September 8, 2018 Share Posted September 8, 2018 7 hours ago, Nodragem said: Hence I was thinking that maybe the Unity toolkit / exporter already deals with Unity Input Manager, I don't see that as a supported feature: https://doc.babylonjs.com/resources/intro I can see in the scene manager they are being put into UserInputOptions from metadata, but didn't look further, but that's probably the structure you are after: https://github.com/BabylonJS/Extensions/blob/f018e82197c2d1de1149b5e3ae589df978fab4f2/SceneManager/src/babylon.scenecomponents.ts#L984 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.