Photon Fusion 1
Last updated
Was this helpful?
Last updated
Was this helpful?
This guide will help you create a headless server on Edgegap for a Unity project using as its networking solution.
We will use the sample project as an example.
Because Edgegap works best when it knows where all the players are (so it can pick a server in the optimal location) the Tanknarok game will first start in client-hosted mode and then switch to the Edgegap server once all the players have joined. This is because Tanknarok has a playable lobby that runs before the match starts where players are able to join and leave at will. So the flow of logic is as follow:
Players connect to the same room using AutoHostOrClient
mode. Which automatically picks the first player in the room as the host.
All players send their IP address to the host when they join.
Once the players ready-up, the host uses these IP addresses to start an Edgegap server version of the game with a randomized room code (using a guid).
When the host sees that the Edgegap instance has finished deploying, it tells the other players about the new room code.
All players, including the host, disconnect from their current room and reconnect in Client mode to the new room being hosted by the Edgegap server.
When the Edgegap server sees all expected players have joined, it starts the match.
When the Edgegap server sees that everyone has left the match, it shuts itself down.
To run this application, you will need to have both a Photon account and an account with Edgegap. First, you will need to make a Fusion app on the Photon dashboard and set the app id in the PhotonAppSettings
scriptable object file. Then, you will need to add an application and create an API token on the dashboard. You will use these to set the App Name, Version, and Api Token in the EdgegapConfig
scriptable object file.
There are also two plugins to help with development: Newtonsoft Json for better Json serialization and for easier testing with multiple unity editors.
The Photon Fusion Tanknarok project needed to be slightly altered to work as a server. This mostly involved automatically starting the match and setting the mode.
Modified GameLauncher.cs
to launch on start with GameMode.Server
.
Modified FusionLauncher.cs
to call _spawnWorldCallback
once game is running instead of relying on InstantiatePlayer
(since no players are instantiated for server).
Skipping the lobby scene if it’s a server and going straight to the match when all the players have joined.
The docker file is pretty basic. We’re just using an ubuntu base, copying over the compiled linux server build, setting the permissions on the file, and running it.
The initial client takes on a role of host and of managing the Edgegap server. It does this through the EdgegapManager.cs
file, which in turn uses the EdgegapAPIInterface.cs
file, which wraps up most of the functionality surrounding interacting with Edgegap.
In GameManager.cs
, the OnAllPlayersReady
function for the host will deploy an Edgegap server instead of starting the match. It does this based on the IP addresses of all the players. Unity coroutines allow these actions to happen asynchronously. The Deploy method takes a callback function, which is fed the name of the new room the server will use. A Photon RPC function is sent to signal to all the clients to switch over.
The switch over happens in an empty scene named TransferringScene
.
Besides switching over to the new Edgegap server when told to, joining players also need to tell the host player what their IP address is. Since Photon doesn’t provide this information, an RPC call in GameManager
called ShareIpAddress
is called from the Player.cs
class in the Spawned
method.
The host then stores these IP addresses in the player class for all the players based on who the source of the RPC message was:
Back in the server, the modified GameManager.cs
keeps track of the number of players in the match in the main Update function. It both checks for when the target number of players have joined, so it can start the match, but also determines when it’s time to shut itself down. If the game has been running for over a minute and there aren’t any players left, it calls the Edgegap API to stop its own deployment.
You now have a Photon Fusion project available to deploy on demand!
You can grab the modified Tanknarok project on . You will also need to import the into the project.
This has already been completed in the modified Tanknarok project available our .
Inside EdgegapManager, a randomized room code is created and passed, along with the player count as Environment variables to the newly created instance through the deploy API. This is combined with the status
api calls that start checking once a second to see when the instance is done deploying. You could also use the option.