Add error catch
The first step for now is creating a error catch event in case something's going wrong. Add a new event from the Photon object and choose On error. In this event add a new Function action. Go ahead and choose Call function. Type in "Debug" and add a parameter. This parameter is the text that is displayed in the TextBox. In the parameter type in or choose from the expressions list Photon.ErrorMessage.
Connecting to the lobby
First thing we need to do is connecting to the name and master server of ExitGames. To do this we create a new event in our Event sheet. Add the On start of layout event from System. Create a new sub-event. Copy the "Debug" action from the On error event. Change the parameter to "Connecting to main server". Now create another action from the Photon object and select Connect. That's it for connecting to the main server.
When connection is successful the player should join into the lobby. Add another Photon event called On join lobby. Copy the "Debug" action again into this one and type as parameter "Connected to lobby".
Connecting to a random room or create it if it doesn't exist
To join into a room we make it easier for us by adding a new Keyboard project to the project. Add a new event for the Keyboard and choose On key released. Pick whatever key you like. I chose key "1". Add another condition to the event from the Photon object called Is connected to a lobby and another one from the Photon called Is joined to a room. The player can only be connected to either a room or the lobby, not both at the same time. With this done add a new action into this event and call the Join random room action from the Photon object. Leave everything as it is and press done.
Next we add the "exception" event for the random room event. Add a new event from the Photon object called On joinRandomRoom no match found. Add a new action to this event from the Photon object called Create room. Leave everything as it is and press done.
Finally we will add the On join room event from the Photon object to make sure we are connected to a game. In this event copy the "Debug" log again and type as parameter "You connected to a game" because this event is triggered only for the player who's joining the room.
That's it. We should be connected to the server and being joined in a room.
Leaving a room by pressing 2 on the keyboard (for testcase)
First create a global variable called meIsExiting with default 0 (zero). This variable will be set to 1 whenever we decide to leave a game and rejoin to the lobby.
We simply add another Keyboard event like before but this time we pick key number 2 to trigger the event. This time it's necessary to check if we are connected to a room instead of the lobby to only trigger the event when we are in a game. We set the meIsExiting variable to 1, trigger the Leave room event and display a message for the player. The meIsExiting variable will be explained in the next steps.