S&box Wiki

Revision Difference

Input_System#546540

<cat>Code.Misc</cat> <title>Input System</title> Input is part of [The Game Loop](GameLoop), every frame our clients build their input, converting your mouse, keyboard and controller inputs into a **Command** that is sent to the server each tick. # Building Input Every frame `BuildInput( InputBuilder )` gets called clientside. [InputBuilder](Sandbox.InputBuilder) consists of inputs and outputs that are sent to the server such as InputBuilder.ViewAngles. ```csharp // This could be overriding within a Game, Entity or Camera class. public override void BuildInput( InputBuilder inputBuilder ) { // Only set new view angles if right mouse is held if ( inputBuilder.Down( InputButton.Attack2 ) ) { inputBuilder.ViewAngles += inputBuilder.AnalogLook; inputBuilder.InputDirection = inputBuilder.AnalogMove; } } ``` <warning>You should not use the static class Input within BuildInput, everything should be from the InputBuilder.</warning> # Using Input After the input is built the values are passed to the Input class both serverside and clientside. [Input](Sandbox.Input) can be used anywhere clientside, however serverside it can only be used in Simulate( Client ). ## Simulate Every tick client's send a **Command** that contains the values constructed from BuildInput. This command then gets simulated in [Game.Simulate( Client )](Sandbox.GameBase.Simulate) for each client. Within Simulate( Client ) the static [Input](Sandbox.Input) class contains inputs specific to the currently simulated client, this allows you to run the same code on both the server and client for [Prediction](Prediction). ```csharp // This could be overriding within a Game, Entity or Camera class. public override void Simulate( Client cl ) { if ( Input.Pressed( InputButton.Jump ) ) { Velocity += Vector3.Up * 100.0f; } } ``` # Keys with InputButton s&box does not provide raw access to the user's keyboard, instead there is a set of standard bindings your game can make use of. ## Default Bindings | InputButton | Binding | Default | Controller Default | | ------------------------ | ---------------- |:-------:|:------------------:| | `InputButton.Forward` | `+iv_forward` | W | Unbound ( Analog ) | | `InputButton.Back` | `+iv_back` | S | Unbound ( Analog ) | | `InputButton.Left` | `+iv_left` | A | Unbound ( Analog ) | | `InputButton.Right` | `+iv_right` | D | Unbound ( Analog ) | | `InputButton.Jump` | `+iv_jump` | Space | A | | `InputButton.Duck` | `+iv_duck` | Ctrl | B | | `InputButton.Run` | `+iv_sprint` | Shift | Left Stick Press | | `InputButton.Walk` | `+iv_walk` | Alt | Unbound | | `InputButton.Attack1` | `+iv_attack` | Mouse1 | RT ( Soft press ) | | `InputButton.Attack2` | `+iv_attack2` | Mouse2 | LT ( Soft press ) | | `InputButton.Reload` | `+iv_reload` | R | X | | `InputButton.Grenade` | `+iv_grenade` | | | | `InputButton.Drop` | `+iv_drop` | G | | | `InputButton.Use` | `+iv_use` | E | Y | | `InputButton.Flashlight` | `+iv_flashlight` | F | DPAD UP | | `InputButton.View` | `+iv_view` | C | Right Stick Press | | `InputButton.Zoom` | `+iv_zoom` | Mouse3 | Unbound | | `InputButton.Menu` | `+iv_menu` | Q | Pause Button | | `InputButton.Score` | `+iv_score` | Tab | Back Button | | `InputButton.Chat` | `?` | | | | `InputButton.Voice` | `+iv_voice` | V | | | `InputButton.SlotNext` | `+iv_slotnext` | | RB | | `InputButton.SlotPrev` | `+iv_slotprev` | | LB | | `InputButton.Slot1` | `+iv_slot1` | 1 | DPAD LEFT | | `InputButton.Slot2` | `+iv_slot2` | 2 | DPAD RIGHT | | `InputButton.Slot3` | `+iv_slot3` | 3 | DPAD DOWN | | `InputButton.Slot4` | `+iv_slot4` | 4 | | | `InputButton.Slot5` | `+iv_slot5` | 5 | | | `InputButton.Slot6` | `+iv_slot6` | 6 | | | `InputButton.Slot7` | `+iv_slot7` | 7 | | | `InputButton.Slot8` | `+iv_slot8` | 8 | | | `InputButton.Slot9` | `+iv_slot9` | 9 | | | `InputButton.Slot0` | `+iv_slot0` | 0 | | <note>You should rely on what the action actually does rather then binding to any specific keys.</note> ## Getting bound keys You can get the key the user has bound to a button with Input.GetButtonOrigin( InputButton), this can be used to prompt the user what key or controller button to press. ```csharp string useButton = Input.GetButtonOrigin( InputButton.Use ); ``` ⤶ ⤶ ## Getting glyphs⤶ ⤶ Glyphs can be used to show the user which button relates to which action they should press. These automatically figure out the input device the user is using and show appropriate textures for them.⤶ ⤶ <upload src="a5727/8d9db91362fc064.png" size="66153" name="image.png" />⤶ ⤶ ```csharp⤶ Texture JumpButton = Input.GetGlyph( InputButton.Jump );⤶ ```⤶ ⤶ <note>You should set your UI image every tick with Input.GetGlyph, this is because the user can rebind keys or the input device can change part way through a game.</note>⤶ ⤶ ### Glyph Styles⤶ ⤶ <upload src="a5727/8d9db912ef1066d.png" size="1213646" name="image.png" />⤶ ⤶ There's some different styles and style modifiers you can chain to get the right look for your UI.⤶ ⤶ * Knockout: Face buttons will have colored labels/outlines on a knocked out background ⤶ * Light: Black detail/borders on a white background ⤶ * Dark: White detail/borders on a black background ⤶ ⤶ Modifiers ( Default ABXY/PS equivalent glyphs have a solid fill w/ color matching the physical buttons on the device )⤶ ⤶ * WithSolidABXY: ABXY Buttons will have a solid fill⤶ * WithNeutralColorABXY: ABXY Buttons will match the base style color instead of their normal associated color⤶ ⤶ <note>With neutral colored glyphs you can always use SCSS to tint the glyph colors to better suit your game's UI.</note>