> For the complete documentation index, see [llms.txt](https://netick.gitbook.io/v1/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://netick.gitbook.io/v1/understanding-client-side-prediction/writing-client-side-prediction-code.md).

# Writing Client-Side Prediction code

## **Network Input** <a href="#network-input" id="network-input"></a>

Network Input describes what the player wants to do, which will be used to simulate the state of objects they want to control. This ensures that the client can’t directly change the state – the change happens by executing the input, which, even if tampered with, won’t be game-breaking.

## **Defining Inputs** <a href="#defining-inputs" id="defining-inputs"></a>

To define a new input, create a class that inherits from NetworkInput:

```csharp
public class MyInput : NetworkInput
{
    public bool       ShootInput;
    public float      MoveDirX, MoveDirY;
}
```

## **Setting Inputs** <a href="#setting-inputs" id="setting-inputs"></a>

To set the variables of an input, you first need to acquire the input object of the next tick, using `Sandbox.GetInput`.\
Then, you can set it inside `NetworkUpdate` on NetworkBehaviour:

```csharp
public override void NetworkUpdate()
{
    var input = Sandbox.GetInput<MyInput>();

    input.MoveDirX = Input.GetAxis("Horizontal");
    input.MoveDirY = Input.GetAxis("Vertical");
}
```

You could also set them on `OnInput` of NetworkEventsListner, which is preferred.

## **Simulating/Executing Inputs**

To drive the gameplay based on the input object, you must do that in `NetworkFixedUpdate`:

```csharp
public override void NetworkFixedUpdate()
{
    if (FetchInput(out MyInput input))
    {
        // movement
        var movement = transform.TransformVector(new   Vector3(input.MoveDirX, 0, input.MoveDirY)) * Speed;
        movement.y       = 0;
  	    _CC.Move(movement * Time.fixedDeltaTime);

	    // shooting
        if (input.ShootInput == true && !IsResimulating)
            Shot();
    }
}
```

{% hint style="warning" %}
Don’t confuse Sandbox.GetInput with FetchInput.\
\&#xNAN;**– Sandbox.GetInput is used to read/set the user inputs into the input object.**\
\&#xNAN;**– FetchInput is used to actually use the input** **object in the simulation.**
{% endhint %}

FetchInput tries to fetch an input for the state/tick being simulated/resimulated. It only returns true if either:

1. We are providing inputs to this object – meaning we are the Input Source of the object.
2. We are the owner (the server) of this object – receiving inputs from the client who’s the Input Source. And only if we have an input for the current tick being simulated. If not, it would return false. Usually, that happens due to packet loss.

And to avoid the previous issue we talked about, we make sure that we are only shooting if we are simulating a new input, by checking `IsResimulating`.

## **Input Source**

For a client to be able to provide inputs to be used in an Object’s `NetworkFixedUpdate`, and hence take control of it, that client must be the Input Source of that object. Otherwise, `FetchInput` will return false. To check if you are the Input Source, use IsInputSource.

The server can also be the Input Source of objects, although it won’t do any CSP, since it needs not to, after all, it’s the server.

You can set the Input Source of an object when instantiating it:

```csharp
sandbox.NetworkInstantiate(PlayerPrefab, spawnPos, Quaternion.identity, client);
```

To set/remove the Input Source (must only be called on the server):

1. Call `PermitInput` to set the Input Source of the object: `Object.PermitInput(client);`
2. Call `RevokeInput` to remove the Input Source of the object: `Object.RevokeInput();`

There are several methods you can override to run code when Input Source is assigned/removed or has left:

1. `OnInputPermitted`: called on the player who was given permission to provide inputs to this object.
2. `OnInputRevoked`: called on the player whose permission to provide inputs to this object has been revoked by the owner (server).
3. `OnInputSourceLeft`: called on the owner (server) when the Input Source client has left the game.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://netick.gitbook.io/v1/understanding-client-side-prediction/writing-client-side-prediction-code.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
