# Helper Scripts

Some extra scripts that are part of the Network Playground that may be useful

{% tabs %}
{% tab title="Simple Spawn" %}

```
A simple spawn script that Instantiates the Networked Player on a Keypress
```

{% endtab %}

{% tab title="C#" %}

```csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpawnCharacter: MonoBehaviour
{
    public KeyCode KeyPress;
    public GameObject NetworkedCharacter;
    
    // Update is called once per frame
    void Update()
    {
        if(Input.GetKeyDown(KeyPress))
        {
            Instantiate(NetworkedCharacter);    
        }
    }
}
```

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="Simple Input" %}

```
Simple Input to get your Game Objects Moving 
```

{% endtab %}

{% tab title="C#" %}

```csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SimpleMovement : MonoBehaviour
{
    public float speed = 1f;

    void Update()
    {
        float h = Input.GetAxisRaw("Horizontal");
        float v = Input.GetAxisRaw("Vertical");

        transform.position += transform.forward * (v * speed * Time.deltaTime);
        transform.position += transform.right * (h * speed * Time.deltaTime);
    }
}

```

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="Coherence Command Handler" %}

```
An Event System you put on the Prefab with coherenceSync
that can handle Network Commands and transfer Authority
```

{% endtab %}

{% tab title="C#" %}

```csharp
using Coherence;
using UnityEngine;
using UnityEngine.Events;
using Coherence.Toolkit;

public class CoherenceCommandHandler : MonoBehaviour
{
    private CoherenceSync sync;

    public UnityEvent onCommandReceived;

    private GameObject fxInstance;

    private void Awake()
    {
        sync = GetComponent<CoherenceSync>();
    }

    public void Adopt()
    {
        if (!sync || sync.isSimulated)
        {
            return;
        }

        sync.RequestAuthority();
    }

    public void Orphan()
    {
        if (!sync || !sync.isSimulated)
        {
            return;
        }

        sync.AbandonAuthority();
    }

    public void ToggleAuthority()
    {
        if (!sync)
        {
            return;
        }

        if (sync.isSimulated)
        {
            Orphan();
        }
        else
        {
            Adopt();
        }
    }

    public void Command(CoherenceSync sender)
    {
        sync.SendCommand<SampleCommandHandler>("ReceiveCommand", MessageTarget.All);
    }

    public void ReceiveCommand()
    {
        onCommandReceived.Invoke();
    }

    public void InstantiateFX(GameObject fx)
    {
        if (fxInstance)
        {
            Destroy(fxInstance);
        }

        var t = fx.transform;
        fxInstance = Instantiate(fx, transform.position + t.position, transform.rotation);
    }
}

```

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="Coherence Connection Events" %}

```
An Event System you put on an empty game object in your 
scene that handles Global Connection/Disconnection
```

{% endtab %}

{% tab title="C#" %}

```csharp
using UnityEngine;
using UnityEngine.Events;
using Coherence;

public class CoherenceConnectionEvents : MonoBehaviour
{
    public UnityEvent onConnect;
    public UnityEvent onDisconnect;

    private void OnEnable()
    {
        Coherence.Network.OnConnected += OnConnect;
        Coherence.Network.OnDisconnected += OnDisconnect;
    }

    private void OnDisable()
    {
        Coherence.Network.OnConnected -= OnConnect;
        Coherence.Network.OnDisconnected -= OnDisconnect;
    }

    private void OnConnect()
    {
        onConnect.Invoke();
    }

    private void OnDisconnect()
    {
        onDisconnect.Invoke();
    }
}

```

{% endtab %}
{% endtabs %}


---

# Agent Instructions: 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:

```
GET https://docs.coherence.io/0.5.2/resources/helper-scripts.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
