How to Deploy to a Server
Alright so this guide is actually for Linux servers, but it will be similar for Windows servers. The reason I’m running with Linux is because a) easy to find hosting options for Linux b) that hosting is way cheaper then Windows or other. At the time of writing this I have 2 Linode servers running, each costs me $5 a month and I can run a bunch of server instances each. I have one for here in Australia where I live and one in the US. If I was smarter and had one of my games pick up popularity, it would probably be worth working out how to do cloud instances that create and destroy themselves as needed, but these Linode ones are just running all day everyday and that works for me.
Anyway, before we deploy to a server, it might be worth noting what kind of setup we’re going for. For this example I want the Master Server on a machine and along side it maybe a few official game servers running 24/7. Lets start with the Master Server.
Master Server
The goal here, is at least for small/mid projects, is to have 1 Master Server hosted somewhere so Clients and Game Servers can connect to it. It keeps a list of Game Servers and helps Clients connect to it. So we need just one of these guys and we need to know the IP and port its using so the Clients and Servers can connect to it.
Where ever you plan to host it, its important to make sure the server is not going to block incoming connections and is port forwarded for whatever ports you want the server to be listening/sending on. Also the Master Server should not have a roaming IP, a static one is preferable so that you know what the IP will be at all times.
To make sure no other weird apps or other people building projects with CyborgNetcode can join your MasterServer, make sure to replace the connection_key to something likely unique to your project:
This can be done on the next step, but since we’re here. If you already have the IP of the server you will be hosting on and desired port number, then you can put it into here as the default for clients and game servers to join.
In fact you may not give the users a textbox to fill in at all and might want to hardcode the Master Servers IP and port
Building Master Server
Lets build for Master Server then. Here are the settings I use. Its important that the first Scene in the list is the Master Server one.
A Server Build means it doesn’t do any rendering to screen. It will just sit running as background task on a computer, which is great because it’ll consume less cpu/ram/gpu and keep your costs lower if you get billed on those things (AWS?). Anyway, it will ask you where you want to build it and what to call it. Its up to you, I call mine:
This creates a Linux executable, so it wont work here on Windows or Mac so don’t bother trying to run it.
Setting Master Server up to run on Server
Now you can upload it onto your Linux server. For this I use WinSCP (since I’m on Windows) but you can use any ftp/sftp tool or other. You’ll work it out. If you’re with Linode I setup a login kinda like this for one of my servers
where the IP can be found in your Linode account against any servers you are hosting(I used this guide to sort of get me started on this https://noobtuts.com/unity/unet-server-hosting). Anyway, once you connect you should be able to upload files from your computer onto the server. If using WinSCP the folders on the left are your computers and the ones on the right is the server. You can drag stuff into the server and it will upload it.
Upload all of the files and folders of where your build ended up to the server.
Once its on there, we will want to launch it but we can’t do that from here, we need to login into the server directly in some way. For this I used Windows PowerShell, but any SSH client will do the trick likely. I sign into my server with :
ssh root@194.195.122.247 but you will need your IP and root is my default user, yours might be different too. Whoever you have hosting with will have guides.
Once you sign in, you’ll have to navigate the server via bash commands. If you have never done that before, worth looking up Linux bash commands(here is a cheat sheet https://cheatography.com/davechild/cheat-sheets/linux-command-line/) but I’ll explain any I use on the go. To see what is in the current directory type ls
and press enter(I think it stands for list?) and use cd ‘name of directory'
to move into that directory if its in the same folder or specify full directory path to jump to a specific folder. If you went too deep with cd, you can back out of a folder with cd ..
Here’s an example
Once you’ve located the master server file you created, we’ll want to launch it. BUT, we also want to be able to launch other programs on here and if we just straight up launch it we wont be able to do that. So instead, what we want to do is create separate ‘screens’ to run each thing in. Since we don’t have a visual of the server and only text, you can imagine screens being similar to your tabs open in an internet browser. Each tab/screen is looking after a different thing.
To create a screen use screen -S nameOfScreen
so for this example, lets type:
screen -S MasterServer
It will create and load you into this screen (if that command doesn’t work, look at this for guidance https://linuxize.com/post/how-to-use-linux-screen/)
Ok, now we can launch our Master Server program with these 2 commands
export TERM=xterm
(I can’t remember what this is for)
./MasterServerLinuxBuild.x86_64 -logfile -
(that’s the name of my file, you may have called yours something else)
If it doesn’t report any errors, it should be running now. To leave the screen so you can make others, hold CTRL and press A and then D
To see all of your screens you’ve made use screen -ls
and to re-attach(use) an existing screen type screen -r nameOfScreen
. If you want to kill your Master server program while its running, make sure you are in its screen and then press CTRL + C
and that should stop it from running.
If that all worked, congrats! You have Master Server up and running to connect to. Lets try get a Game Server up next.
Build Game Server
You can run your game and choose to host now if you want and it should list the game on your Master Server for others to join, but perhaps you also want some dedicated game servers running that are always connected to the Master Server. Again we’ll build for Linux but this time make sure the GameServerAutoSetupScene is up top
I save mine like this but you can put yours wherever you want.
This one is a tiny bit more involved. Scene GameServerAutoSetupScene on launch looks for a file locally called gameDetails.txt
which it reads and then works out how to connect to the MasterServer and setup its game name and details. For example one of these text files for one of my Sydney servers looks like this:
AU_Sydney Game Server 1
7001
127.0.0.1
7000
You may want to change the code to read more details like password or max players or something, but the current setup follows this format:
- Name of this game server
- game servers port it will listen on
- Master Server’s IP address (127.0.0.1 if you are running game server on the same machine as MasterServer
- Master Servers port
This file makes it easy to bring to life a game server real quick, especially if it gets downed for whatever reason. So I make a gameDetails.txt for each one of my dedicated game servers.
Setting up Game Servers to run on your Server
We’re going to upload this to the server, refer to Master Server section on how if you need a refresher.
I recommend making a folder on the server for each game server you want running on it. For example my test server is running 1 Master Server and 2 Game Servers, so I have folders game1 and game2 on there.
Upload the game server build and its files PLUS a gameDetails.txt
for EACH folder to describe each game server so it can setup correctly, it must be in the same folder as each Linux game server build. So if you have 5 game server folders, you should have 5 gameDetails.txts, one in each.
Each one of those gameDetails.txt files should have the second line, the port of the server, different to the other game servers and master server ports. It needs a unique one otherwise it wont be able to attach to it.
Sign into the server using Windows Powershell or other SSH client like we did for Master Server.
I’m going to speed run the next few steps because they are similar to Master Server so if you get lost, look above.
Lets say you made a bunch of folders for each game server and named them game1, game2 etc. Well we’ll need screens/tabs for each one. So I’ll just run through the first one. Your folders may be named differently, that’s ok, I’ll just be using game1 as an example.
Make the new screen/tab
screen -S game1
Navigate into your game1 folder wherever that is, use ls to see what’s in the current directory
ls
cd game1
Run the game server (make sure the Master Server was setup first)
export TERM=xterm
./ServerLinuxBuild.x86_64 -logfile -
(or whatever you called your game server build)
It should be running now, check for errors
Exit this screen by holding down CTRL and pressing A and then D
Sweet, repeat for each of the other game servers you have in folders.
If that all worked, lets see if it worked.
Checking it all worked
Run the project with Join scene as the first scene in the project and can be running locally on your Windows or whatever machine. In the game, if you setup the default Master Server IP and port, then you can go straight to Join Game, otherwise go to Network Settings first
If everything went well you should join the Master Server and if you setup the Game Servers correctly, you should see their names listed here as something you can join 😀
Click to join a game and playtest
If all went well, congrats! You hosted your own servers and they worked correctly! And maybe you learned a few things along the way.
If it did not pan out so well, check the status message at the bottom, go back onto your server machine/s and reattach to all of the screens you made and check their outputs. There might have been an error. If the Master Server connects for the client but you can’t see games, double check the gameDetails.txt file for each
Hopefully you spot something and get it all worked out. If everything looks fine server side, maybe disable firewalls on either server or client temporarily or check Server’s forwarded ports. Hope that helps.
Everything is now live!
Congrats! Now have a rest, this was big work haha