> For the complete documentation index, see [llms.txt](https://modding.corekeepergame.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://modding.corekeepergame.com/mod-code-examples/system-example.md).

# System Example

The System Example showcases how to make a client-side system, as well as a server-side system. Client-side acts on only your player, whereas server-side acts on all players.

```csharp
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using Unity.Entities;
using Unity.Mathematics;
using Unity.NetCode;
using Unity.Transforms;

[WorldSystemFilter(WorldSystemFilterFlags.ClientSimulation)]
[UpdateInGroup(typeof(RunSimulationSystemGroup), OrderLast = true)]
[UpdateAfter(typeof(SendClientInputSystem))]
public partial class InvertPlayerMovementSystem : SystemBase
{
    protected override void OnUpdate()
    {
        foreach (var clientInputData in
                 SystemAPI.Query<RefRW<ClientInputData>>().WithAll<GhostOwnerIsLocal>()) // GhostOwnerIsLocal => this is the local player
        {
            // ClientInputData used to avoid unnecessary bytes being sent. ClientInput is set in the PredictedSimulattionSystemGroup from current tick.
            // Outside of PredictedSimulationSystemGroup, ClientInputData has to be used to get and set this frames input data.
            var clientInput = UnsafeUtility.As<ClientInputData, ClientInput>(ref clientInputData.ValueRW);
            clientInput.movementDirection *= -1f;
			clientInputData.ValueRW = UnsafeUtility.As<ClientInput, ClientInputData>(ref clientInput);
        }
    }
}

[WorldSystemFilter(WorldSystemFilterFlags.ServerSimulation)]
[UpdateInGroup(typeof(SimulationSystemGroup))]
public partial class MoveCowsAwayFromPlayerSystem : PugSimulationSystemBase
{
    protected override void OnUpdate()
    {
        // No need to dispose World allocator
        var playerPositions = new NativeList<float3>(World.UpdateAllocator.ToAllocator);

        Entities.WithAll<PlayerGhost>().ForEach((in LocalTransform translation) =>
        {
            playerPositions.Add(translation.Position);
        }).Schedule();
        
        var deltaTime = SystemAPI.Time.DeltaTime;
        var distanceToMoveSq = 5 * 5;
        // Filter on cattle just to avoid looping through everything
        Entities.WithAll<CattleCD>().ForEach((ref LocalTransform translation, in ObjectDataCD objectData) =>
            {
                if (objectData.objectID != ObjectID.Cow)
                {
                    return;
                }
                
                foreach (var playerPos in playerPositions)
                {
                    if (math.distancesq(translation.Position, playerPos) < distanceToMoveSq)
                    {
                        var dir = math.normalizesafe(translation.Position - playerPos);
                        translation.Position += dir * deltaTime;
                    }
                }
            }).Schedule();
        
        base.OnUpdate();
    }
}
```


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://modding.corekeepergame.com/mod-code-examples/system-example.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
