-
Notifications
You must be signed in to change notification settings - Fork 437
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Single player hosting in WebGL using RPC #3035
base: develop
Are you sure you want to change the base?
feat: Single player hosting in WebGL using RPC #3035
Conversation
…ly make a single player mode while still using netcode usually as a host.
@BenHamrick Where is the |
My bad! It's supposed to be IPCNetworkInterface |
@BenHamrick |
@NoelStephensUnity I tried using the mock transport but it seems like |
Ahhh... I meant to look at it as an example/reference. In reality, you really should only need to do something like this: /// <summary>
/// Add this component to the same GameObject as your NetworkManager
/// </summary>
public class GameModeHandler : MonoBehaviour
{
private NetworkManager m_NetworkManager;
private UnityTransport m_UnityTransport;
private UnityTransport.ConnectionAddressData m_Multiplayer;
private UnityTransport.ConnectionAddressData m_SinglePlayer;
private void Start()
{
m_NetworkManager = GetComponent<NetworkManager>();
if (m_NetworkManager)
{
m_UnityTransport = m_NetworkManager.NetworkConfig.NetworkTransport as UnityTransport;
if (m_UnityTransport)
{
m_Multiplayer = m_UnityTransport.ConnectionData;
}
// Single player would use the device's local/private loopback address
// (i.e. no external client on another device could connect to the session)
m_SinglePlayer.Port = 7777;
m_SinglePlayer.Address = "127.0.0.1";
m_SinglePlayer.ServerListenAddress = "127.0.0.1";
}
}
/// <summary>
/// Invoke this prior to starting NetworkManager
/// </summary>
public void ApplyGameMode(bool isSinglePlayer)
{
if (!m_UnityTransport)
{
Debug.LogError("Transport is not set.");
}
// Defines the transport for single or multiplayer use
m_UnityTransport.UseWebSockets = !isSinglePlayer;
m_UnityTransport.UseEncryption = !isSinglePlayer;
m_UnityTransport.ConnectionData = isSinglePlayer ? m_SinglePlayer : m_Multiplayer;
}
} Where you just make the single player use the device's local loopback address, use the default port (7777), disable web sockets and encryption, and start the NetworkManager as a host. This would allow you to start a single player game using standard UDP. The real issue is what builds are available. A WebGL build vs a device specific build... which I will need to look into that side of the equation... because a WebGL build will require the device to host the webservice to deliver the content via browser instance where a device specific build will allow you to launch the game from the device in question (could get tricky depending upon the device and could impact performance depending upon the device). It might be the simplest answer is to make two builds...but I don't know the specifics of your game/project and if you are trying to provide a way to build upon something in single player and then use progress made in single player within a multiplayer WebGL instance. |
@NoelStephensUnity Reason why I am even here is because a player of mine had something running on 7777 and so could not start a server. Probably a second instances of the game 🤷 , but that does not matter here. I would rather run it using IPC that is used for testing anyways so we know it works. I never want to run a server on a port locally, even just by accident, if someone wants to play singe player. |
Expose the ability to use RPC especially on WebGL to allow us to easily make a single player mode while still using netcode usually as a host.
Changelog
Testing and Documentation