LogoLogo
HomeOnline DashboardAPIDiscordForums
SDK 0.4.14
SDK 0.4.14
  • Welcome
  • Overview
    • What is coherence?
    • How does coherence work?
    • Features and Roadmap
    • Requirements
  • Get Started
    • Install coherence
    • Scene setup
    • Prefab setup
    • Build and run
    • Baking and code generation
    • Create a free account
    • Deploy and share
  • Authority and communication
    • How authority works
    • Authority transfer
    • Commands
    • Server-side and input queues
    • Animations
  • Persistence
    • Overview
    • Configuring persistence
    • Storage
    • Example – A global counter
  • Optimization
    • Overview
    • Simulation frequency
    • Areas of interest
    • World size
    • Level of detail
    • Interpolation
    • Extrapolation
  • Connected entities
    • Overview
    • Entity references
    • ConnectedEntity component
    • Parent-child relationships
  • Simulators
    • Overview
    • Client vs. simulator logic
    • Build and deploy
    • Simulator load balancing
  • Tutorial project
    • Get the Tutorial Project
    • Start Tutorial
      • 1. Transforms
      • 2. Physics
      • 3. Persistence
      • 4. Animation and Variables
      • 5. AI Navigation
      • 6. Network Commands
      • 7. Network Events
  • Game Services
    • Game account
    • Key-value store
    • Matchmaking
  • API reference
    • Network SDK
      • CoherenceSync
      • MonoBridge
      • LiveQuery
      • Archetype
      • Sample UI
      • Settings Window
    • Cloud API
      • API tokens and keys
      • Server discovery
      • Game account
      • Key-value store
      • Matchmaking
    • Replication Server
    • Simulation Server
    • Entity Component System
      • Network Entities and Components
  • Schema reference
    • Overview
    • Specification
    • Field Settings
    • Archetypes and LOD-ing
  • Resources
    • Downloads
    • Video Tutorials
    • Glossary
    • CLI Utilities
    • Helper Scripts
    • Troubleshooting
  • Community
    • Discord
  • Additional information
    • Pricing
    • SLA
    • Unreal Engine support
    • Peer-to-Peer (P2P)
    • Known Issues
    • Version History
Powered by GitBook
On this page

Was this helpful?

Export as PDF
  1. Resources

Helper Scripts

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

A simple spawn script that Instatiates the Networked Player on a Keypress
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);    
        }
    }
}
Simple Input to get your Game Objects Moving 
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);
    }
}
An Event System you put on the Prefab with 
coherenceSync
that handles Network Events and Commands
using UnityEngine;
using UnityEngine.Events;
using Coherence.Toolkit;

public class CoherenceHandler : MonoBehaviour
{
    private CoherenceSync sync;

    public UnityEvent onAuthorityGained;
    public UnityEvent onAuthorityLost;
    public UnityEvent onCommandReceived;
    public UnityEvent onEventReceived;

    private GameObject fxInstance;

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

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

        sync.RequestAuthority();
    }

    public void Command(CoherenceSync sender)
    {
        sync.SendCommand(sender, "CoherenceHandler.ReceiveCommand");

        ReceiveCommand(); // reproduce locally
    }

    public void SendEvent()
    {
        foreach (var s in CoherenceSync.instances.Values)
        {
            if (!s || s == sync)
            {
                continue;
            }

            s.SendCommand(sync, "CoherenceHandler.ReceiveEvent");
            s.SendMessage("ReceiveEvent", SendMessageOptions.DontRequireReceiver); // reproduce locally
        }
    }

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

    public void ReceiveEvent()
    {
        onEventReceived.Invoke();
    }

    public void OnAuthorityTransfer(bool gained)
    {
        if (gained)
        {
            onAuthorityGained.Invoke();
        }
        else
        {
            onAuthorityLost.Invoke();
        }
    }

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

        var t = fx.transform;
        fxInstance = Instantiate(fx, transform.position + t.position, transform.rotation);
    }
}
An Event System you put on an empty game object in your 
scene that handles Global Connection/Disconnection
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();
    }
}

Last updated 4 years ago

Was this helpful?