Join GitHub today
GitHub is home to over 36 million developers working together to host and review code, manage projects, and build software together.Sign up
Something you may be curious about is how you can make your game both single player and multi-player. Your main problem is probably going to be trying to figure out how you can separate all your RPC calls and your offline direct calls. The thing that may surprise you is if we were to say that there is no need for the distinction between "Online" and "Offline" code.
The Solo Server
So if you think about it, single player could very well be a server that has the following attributes:
- Does not register with a master server
- Does not register with a nat traversal server
- Does not accept any connections (max players is 0)
- Blocks any other connection attempts
Well this makes sense so far, if a server doesn't accept any clients then technically it is nothing more than an offline game. However, what about the RPC calls and the various other networked things like the networkObject? Well, the answer is simple, Forge Networking was built to where the server isolation is offline play. If there are no connections to the server, it will call all RPCs directly without any network layer involved for sending (it doesn't send to itself, just runs the method locally). Below is an example of a server put into an offline like state:
// Zero connections allowed to this server NetWorker server = new UDPServer(0); ((UDPServer)server).Connect(); // Block all connections to skip the validation phase ((IServer)server).StopAcceptingConnections();
When you specify receivers that do not include the server, such as
Receivers.Others then the server will not call the method, even with no other connections.
Your code will still need to initialize the NetworkManager just as you would when doing online mode. The only difference between the two modes is that offline will not accept any connections, and your server will be your "singleplayer" instance.