Photon Implementation - Capi-Metaverse/Template GitHub Wiki

Table of Contents

Technologies

For this project we have used Unity as the development engine. This engine is based on the use of scenes and game objects, elements that will be explained later.

Photon Networking 2 (PUN2) has been used to implement a multiplayer system. PUN2 is a Unity package that allows to manage small multiplayer rooms with a limit of 2-16 players. We have used the free basic plan because the only major difference with the paid version is the number of simultaneous players you can have on one server.

Free Unity assets have been used to create the temporary scenes. These assets consist of models such as lamps or tables, textures such as wood or marble and structures such as walls and floors.

Unity Structure

This project has been structured through a series of scenes. These scenes are the stage on which the entire game flow takes place.

They consist of a scenario to which various elements known as game objects are added. Game objects are to Unity as the Object class is to Java. However, GameObjects do nothing by themselves. They need special properties before they can become a character, an environment, or a special effect.

The following image shows the flowchart of the application:

Diagram
Figure 1. Flowchart of the application

The following scenes can be seen in the diagram:

  • Login Scene: This scene encompasses the entire registration and login process. Once the user is logged in, he will be redirected to the Lobby scene. In case of validation error, the user will have to re-enter his data.

  • Lobby Scene: The Lobby scene allows users to join different rooms that have been created by users or to create one themselves. When you join a room, you will be asked to choose the avatar you wish to use.

  • Room Scene: Currently the application has two different maps, with the first map having the most functionality at the moment.

Possible activities include moving around the map, talking via text and voice chat and interacting with objects such as lamps and a lectern for Powerpoint presentations.

Photon

As mentioned above, Photon Network 2 has been used to make the connection between users. There are newer versions of Photon such as Photon Realtime, Fusion or Quantum, but they currently have no way to be implemented in WebGL. It might be interesting to make a switch to Photon Fusion once it is upgraded for WebGL or even implement our own multiplayer system if we are thinking on a larger scale.

Photon Connection

To connect to Photon, an App ID is required for both game servers and chat servers. These IDs can be obtained by registering the project from the Photon website and they should be included in the Photon Server Settings file in the Photon Resources folder. This file also allows us to include some settings such as the region or if we want an error file.

From here, the connection is simple, you must use the PhotonNetwork.ConnectUsingSettings function, which will use the file we have created to manage all the configuration.

InitScreen
Figure 2. Start Screen of the Application

Before moving on, it is important to mention that Photon uses a system of Callbacks to manage everything that happens in the application before or after a event. Some examples could be the OnConnectedToMaster function that is called once the user connects to the application or the OnJoinedRoom function called once the user enters a room.

Another important aspect is how the flow of connections between servers works, as can be seen in the following image:

Server States
Figure 3. Diagram with all server state changes.

As explained above, the user connects to the Master Server at login. Once connected, the user will be transferred to the Lobby, where he will enter the Game Server. From the Game Server any room can be accessed.

Photon Rooms

Photon Rooms allow 1 to 16 players to interact with each other in one of the two scenarios created. The user can join a room from the list of rooms in the Lobby or create one himself. The Photon methods used to create and join rooms are PhotonNetwork.CreateRoom and PhotonNetwork.JoinRoom respectively.

Lobby
Figure 4. Lobby with a new room.

These rooms have a number of properties that we use for the connection between players:

  • RoomName : This name will be displayed in the Lobby for users to join.

  • IsOpen : Indicates if a user can join to the room.

  • Players : Dictionary that includes all players in the room.

  • CustomProperties : Hashtable with custom properties that we created like "Map" or "Init".

These CustomProperties allow us to indicate properties such as the map to be used in that room or if the room has already been initialised.

A user can leave a room from the character selection view or from the pause menu once the game has started. This will call the PhotonNetwork.LeaveRoom function which will remove the user from the room and move him back to the MasterServer.

Important CallBacks OnJoinedRoom, OnLeftRoom, OnPlayerEnteredRoom, OnLeftEnteredRoom

Photon Events

Photon allows a series of events to be triggered so that players can communicate with each other via the RaiseEvent function.

This function receives an event number along with a series of parameters such as the sending options, the users who should receive it or extra information.

The different events that have been implemented include:

  • Light Devices : Such us Lamps that can be switched on or off.

  • Presentation : Users can upload a PPTX File to the scene and do a presentation. Users can move the slides forward or backward.

  • Animation Synchronization : This event shares information about other characters animations like walking, running or jumping.

This area is under construction and more events will be added as the project progresses.

⚠️ **GitHub.com Fallback** ⚠️