Revision Difference
GameLoop#548306
<cat>Code.Game</cat>
<title>The Game Loop</title>
⤶
⤶
# Every Tick
## Simulate
<upload src="1/8d91e88fe13b9a2.png" size="6435" name="image.png" />
<upload src="aa125/8dad928c44339ac.png" size="12517" name="image.png" />
⤶
**Reference:**⤶
- [BaseGameManager.Simulate( Client )](https://asset.party/api/Sandbox.Game.BaseGameManager.Simulate(Client))⤶
- [Entity.Simulate( Client )](https://asset.party/api/Sandbox.Entity.Simulate(Client))⤶
Every Tick (60 times a second by default), the client will send a **Command** to the server. This command gets simulated. This means it takes the inputs from the command (ie, new `ViewAngles`) and applies them to the Pawn (ie, set rotation to `ViewAngles`)
While that's being sent to the server the Client also runs the same command locally. This is called Prediction. This allows inputs like walking, shooting and jumping to feel instant, with no ping delay.
After the client runs the command it stores all of the changed entity variables and when the result for that tick comes back from the server it compares them. If they're different then a prediction error happened.
# Every Frame
These things are called every frame on the client.
## BuildInput
<upload src="1/8d91e85ff2d0ffa.png" size="6916" name="image.png" />
<upload src="aa125/8dad9296cb9a25d.png" size="9975" name="image.png" />
⤶
**Reference:**⤶
- [BaseGameManager.BuildInput()](https://asset.party/api/Sandbox.Game.BaseGameManager.BuildInput())⤶
- [Entity.BuildInput()](https://asset.party/api/Sandbox.Entity.BuildInput())⤶
Every frame we build our input. This converts input from your mouse, keyboard and controller into a Command that can be sent to the server on the next tick.
⤶
Within `BuildInput` you can use [Input](https://asset.party/api/Sandbox.Input) functions in order to manipulate input - for example, clearing buttons using [Input.ClearButtons()](https://asset.party/api/Sandbox.Input.ClearButtons()) - and in order to assign values to properties marked with [[ClientInput]](https://asset.party/api/Sandbox.ClientInputAttribute).⤶
This is done by passing a `InputBuilder` class down to the gamemode - which then passes it to the current camera and pawn. These callbacks are generally used to do things like change mouse movement into view angles.
You can override all of this behaviour in your Game class by overriding `BuildInput`.
⤶
⤶
## BuildCamera⤶
⤶
<upload src="1/8d91e8781053b7d.png" size="19199" name="image.png" />⤶
⤶
Every frame we update the camera position. First we find the active `Camera` (Looking at `Client.Camera` first, if that was null we use `Pawn.Camera`) - which sets the position, rotation, fov etc.
⤶
After the camera is set up we call `Game.PostCameraSetup`. This is a useful place to set things up based on camera position, like the viewmodel positions.
⤶
⤶
## Frame Simulate⤶
⤶
<upload src="1/8d91e89fc9b23ab.png" size="7588" name="image.png" />⤶
⤶
## Frame Simulate⤶
⤶
<upload src="aa125/8dad929a6454e87.png" size="13852" name="image.png" />⤶
⤶
**Reference:**⤶
- [BaseGameManager.FrameSimulate( Client )](https://asset.party/api/Sandbox.Game.BaseGameManager.FrameSimulate(Client))
- [Entity.FrameSimulate( Client )](https://asset.party/api/Sandbox.Entity.FrameSimulate(Client))
Because `Simulate` only happens once every tick (60fps by default) and players may have monitors capable of showing framerates much higher than that, things like view direction are going to feel pretty choppy clientside. To get around that mismatch, `FrameSimulate` is called every clientside frame to give the game an opportunity to smooth out anything that would otherwise be stuttery or choppy.
This is commonly used to update things based on view direction (such as Pawn.EyeRot).
This is commonly used to update things based on view direction (such as [Pawn.AimRay](https://asset.party/api/Sandbox.Entity.AimRay)).
It's important to realize that **this is clientside only**, so if you're setting something here then it needs to be updated in Simulate. If you can't comment out your `FrameSimulate` code and have the game work normally then you're doing something wrong.
# Events
You can also have your game code be triggered on [events](https://wiki.facepunch.com/sbox/EventSystem) such as the tick event, allowing you to run logic server side independent of clients.
You can also have your game code be triggered on [events](https://wiki.facepunch.com/sbox/EventSystem) such as the tick event, allowing you to run logic server side independent of clients.⤶
⤶
<note>⤶
Wiki editors: I re-made the images on this page in Figma so they should be pretty easy for other people to edit when necessary. Here's the exported Figma file:⤶
⤶
<upload src="aa125/8dad929e9751dae.fig" size="25727" name="GameLoop.fig" />⤶
⤶
Please keep this up to date if you change anything on this page.⤶
</note>