S&box Wiki

Revision Difference

Input_System#545905

<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.Mouse2 ) )⤶ {⤶ 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 |⤶ | ------------------------ | ---------------- |:-------:|⤶ | `InputButton.Attack1` | `+iv_attack` | Mouse1 |⤶ | `InputButton.Attack2` | `+iv_attack2` | Mouse2 |⤶ | `InputButton.Jump` | `+iv_jump` | Space |⤶ | `InputButton.Duck` | `+iv_duck` | Ctrl |⤶ | `InputButton.Forward` | `+iv_forward` | W |⤶ | `InputButton.Back` | `+iv_back` | S |⤶ | `InputButton.Left` | `+iv_left` | A |⤶ | `InputButton.Right` | `+iv_right` | D |⤶ | `InputButton.Use` | `+iv_use` | E |⤶ | `InputButton.Run` | `+iv_sprint` | Shift |⤶ | `InputButton.Walk` | `+iv_walk` | Alt |⤶ | `InputButton.Reload` | `+iv_reload` | R |⤶ | `InputButton.Zoom` | `+iv_zoom` | Mouse3 |⤶ | `InputButton.Score` | `+iv_score` | Tab |⤶ | `InputButton.Drop` | `+iv_drop` | G |⤶ | `InputButton.View` | `+iv_view` | C |⤶ | `InputButton.Menu` | `+iv_menu` | Q |⤶ | `InputButton.Voice` | `+iv_voice` | V |⤶ | `InputButton.Flashlight` | `+iv_flashlight` | F |⤶ | `InputButton.Slot1` | `+iv_slot1` | 1 |⤶ | `InputButton.Slot2` | `+iv_slot2` | 2 |⤶ | `InputButton.Slot3` | `+iv_slot3` | 3 |⤶ | `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 |⤶ | `InputButton.Cancel` | `?` | ? |⤶ | `InputButton.Alt1 ` | `?` | ? |⤶ | `InputButton.Alt2` | `?` | ? |⤶ | `InputButton.Weapon1` | `?` | ? |⤶ | `InputButton.Weapon2` | `?` | ? |⤶ | `InputButton.LookSpin` | `?` | ? |⤶ | `InputButton.Grenade1` | `?` | ? |⤶ | `InputButton.Grenade2` | `?` | ? |⤶ | `InputButton.Next` | `?` | ? |⤶ | `InputButton.Prev` | `?` | ? |⤶ ⤶ ## Getting Bound Key⤶ ⤶ You can get the key the user has bound to a button with Input.GetKeyBinding( string ), this can be used to prompt the user what key to press.⤶ ⤶ ```csharp⤶ string useButton = Input.GetKeyBinding( "+iv_use" );⤶ ```⤶