S&box Wiki

Revision Difference

Input_System#560528

<cat>Code.Input</cat> <title>Input System</title> ⤶ Input is part of [The Game Loop](https://wiki.facepunch.com/sbox/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. ⤶ # Actions ⤶ To convert inputs from your keyboard, mouse and controller into the game we have a system called Actions. ⤶ You can edit your actions in your Project Settings.⤶ ⤶ # Input⤶ ⤶ Input is handled using the `Input` class. ⤶ ## Input Actions ⤶ You can configure Input Actions for your project in the Project Settings. <upload src="1/8db41a83ec8a45f.png" size="98728" name="image.png" /> ## Using Actions The Input class has a number of methods to help you use Actions. The Input class has a number of methods to help you use your input actions. ⤶ | Method | Description |⤶ |--------|-------------|⤶ | Input.Pressed( "jump" ) | input was just pressed this frame |⤶ | Input.Released( "jump" ) | input was just released this frame |⤶ | Input.Down( "jump" ) | input was held down |⤶ ⤶ It is common to query for input within `OnUpdate` or `OnFixedUpdate`.⤶ ```csharp ⤶ //⤶ // view button was just pressed this tick⤶ //⤶ if ( Input.Pressed( "view" ) )⤶ protected override void OnUpdate()⤶ { // open a menu⤶ if ( Input.Released( "jump" ) )⤶ {⤶ CharacterController.Punch( Vector3.Up * 256f );⤶ CharacterController.IsOnGround = false;⤶ }⤶ } ⤶ //⤶ // attack1 button is down⤶ //⤶ if ( Input.Down( "attack1" ) )⤶ {⤶ // open a menu⤶ }⤶ ⤶ //⤶ // the jump button was just released this tick⤶ //⤶ if ( Input.Released( "jump" ) )⤶ {⤶ // open a menu⤶ }⤶ ⤶ ⤶ ``` ⤶ ⤶ ## When To Use Actions⤶ ⤶ On the client you can use the `Input` methods anywhere. ⤶ ⤶ On the server you should only call Input during a `Simulate` call on a pawn. In this instance the `Input` will return the correct values for the owner of that pawn.⤶ # AnalogMove ## AnalogMove `Input.AnalogMove` is generated automatically from controller input and any actions you have named "forward", "backward", "left" and "right". This is a `Vector3` representing the move input. You can safely use this for something like player movement input but you should sanity check it first. For example, you might want to normalize it. ⤶ # AnalogLook ⤶ ## AnalogLook `Input.AnalogLook` is generated automatically from controller and mouse input. This represents the view angles. This could be used in something like a first person shooter to represent the eye angles or rotation of an entity. This represents the view angles. This could be used in something like a first person shooter to represent the eye angles or rotation of a player's object. ⤶ ## MouseWheel⤶ `Input.Mousewheel` is a Vector2 to represent the scroll wheel. This is a Vector2 because many mouses have horizontal and vertical scrolling.⤶