Skip to content

Instantly share code, notes, and snippets.

@will-hart
Created April 13, 2017 08:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save will-hart/da11d309e214c4b8abf2aa9fc79e388e to your computer and use it in GitHub Desktop.
Save will-hart/da11d309e214c4b8abf2aa9fc79e388e to your computer and use it in GitHub Desktop.
Single Player Fallback (Forge Networking)
namespace Goliath.Networking
{
#region Dependencies
using BeardedManStudios.Forge.Networking;
using BeardedManStudios.Forge.Networking.Unity;
// other usings
using UnityEngine;
#endregion
public class SinglePlayerFallback : MonoBehaviour
{
[SerializeField]
public GameObject _networkManager = null;
private void Start()
{
if (NetworkManager.Instance == null)
{
Debug.LogWarning("No network instance detected - reverting to single player");
UDPServer server = new UDPServer(0);
server.Connect();
server.StopAcceptingConnections();
var mgr = Instantiate(_networkManager).GetComponent<NetworkManager>();
mgr.Initialize(server);
NetworkObject.Flush(server);
// spawn required systems and delete existing ones which aren't required
NetworkManager.Instance.InstantiatePlayerSlots();
// do other setup logic that is normally done by the lobby scene
}
// do stuff here that I want done in any scene, i.e. instantiate my "GameManager", "Player" or whatever
// tidy up
Destroy(gameObject);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment