LogoLogo
⚠️ Outdated documentationGo to LatestHomeAPI
SDK 0.9
SDK 0.9
  • Welcome
  • Overview
    • What is coherence?
    • How does coherence work?
    • Features and Roadmap
    • Rooms and Worlds
    • Requirements
    • Release Notes
  • Get Started
    • Install coherence
    • Scene setup
    • Prefab setup
    • Baking and code generation
    • Build and run
    • Create a free account
    • Deploy Replication Server
    • Share builds
    • Troubleshooting
  • Authority and communication
    • How authority works
    • Authority transfer
    • Commands
    • Simulation frame
    • Client connections
    • Server-authoritative setup
    • GGPO
    • Animations
    • Value sync callbacks
  • Persistence
    • Overview
    • Configuring persistence
    • Storage
    • Example – a global counter
  • Optimization
    • Overview
    • Simulation frequency
    • Areas of interest
    • Level of detail
    • Interpolation
  • Connected entities
    • Overview
    • Entity references
    • Parent-child relationships
    • CoherenceNode
  • Simulators
    • Overview
    • Client vs Simulator logic
    • Build and deploy
    • Simulator load balancing
    • Room Simulators
    • Multi-Room Simulators (advanced)
    • World Simulators
    • Simulator slugs
    • Testing Simulators locally
  • 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. Team-based
      • 8. Connected Entities
  • Game Services
    • Game account
    • Key-value store
    • Matchmaking
  • Developer Portal
    • Overview
    • Dashboard
    • Enabling game services
    • Configure Rooms
    • Manage Worlds
  • API reference
    • coherence SDK
      • CoherenceSync
      • MonoBridge
      • LiveQuery
      • Level of detail
      • Sample UI
      • Settings window
      • Custom bindings (advanced)
      • PlayResolver
      • Rooms
      • Worlds
    • Cloud API
      • API tokens and keys
      • Game account
      • Key-value store
      • Matchmaking
    • Replication Server
    • Simulation Server
  • Schema reference
    • Overview
    • Specification
    • Field settings
    • Archetypes in schemas
  • Resources
    • Downloads
    • SDK update guide
    • Video tutorials
    • Order of execution
    • Glossary
    • CLI utilities
    • Simulator CLI arguments
    • Helper scripts
    • Troubleshooting
    • Continuous Integration setup
  • Community
    • Community
  • Additional information
    • Pricing
    • SLA
    • Unreal Engine support
    • WebGL
    • Peer-to-Peer (P2P)
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 you may find useful:

A simple spawn script that Instantiates 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 can handle Network Commands and transfer Authority
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.HasStateAuthority)
        {
            return;
        }

        sync.RequestAuthority(AuthorityType.Full);
    }

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

        sync.AbandonAuthority();
    }

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

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

    public void Command(CoherenceSync sender)
    {
        sync.SendCommand<CoherenceCommandHandler>(nameof(CoherenceCommandHandler.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);
    }
}
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.Connection;
using Coherence.Toolkit;

public class CoherenceConnectionEvents : MonoBehaviour
{
    public UnityEvent onConnect;
    public UnityEvent onDisconnect;
    private CoherenceMonoBridge monoBridge;

    private void OnEnable()
    {
        monoBridge = FindObjectOfType<CoherenceMonoBridge>();
        monoBridge.onConnected.AddListener(HandleConnected);
        monoBridge.OnLiveQuerySynced += HandleLiveQuerySynced;
        monoBridge.onDisconnected.AddListener(HandleDisconnected);
    }

    private void OnDisable()
    {
        monoBridge.onConnected.RemoveListener(HandleConnected);
        monoBridge.onDisconnected.RemoveListener(HandleDisconnected);
    }

    private void HandleConnected(CoherenceMonoBridge _)
    {
        onConnect?.Invoke();
    }

    private void HandleLiveQuerySynced(CoherenceMonoBridge _)
    {
        onConnect?.Invoke();
    }

    private void HandleDisconnected(CoherenceMonoBridge _,  ConnectionCloseReason __)
    {
        onDisconnect?.Invoke();
    }
}

Last updated 2 years ago

Was this helpful?