Connect and inform about connection status
This is a critical Fsm.
It has three very distinct functions ( organized in rows visually):
-
Connecting to the server
When we START, we make sure we connect if we are not. We simply ask the Photon system ( in the state "Are we connected?") and explicitly requests a connection if we are not ( stats "connect"). We created for this occasion a NO event. It is very important that the States and Events we create are as clear as possible. If a state is labelled as a question, we intuitively wait for a YES NO or TRUE FALSE set of transition. This kind of practice makes Fsm Clear and understandable.
-
Reloading when entering a room
The scene is set up in such a way that when there is a change in the connection status and player status, we simply reload the scene, it is a very important assumption that other Fsm relies on to work properly. In our case, this Fsm is responsible for reloading when the user creates or joins a room, in which case we reload ( as a result, when the scene starts again, the application will be in a room, and spawn the player, display the chat system if the player is not alone, etc etc).
So, We watch for the PHOTON / JOINED ROOM and PHOTON / CREATED ROOM built in events and enter the "Wait for room" state. There is a latency between receiving these events and the player actually being in a room, creating and joining a room is not actually "being" in a room. So we wait for the signal, by watching the room property of the Photon system, and created for the occasion an "IN ROOM" event we plug to the "Is in the Room Event" of the action "Photon Network Get Room Properties". We we indeed are in a room, we transit to the state "we are in, reload!" and simply reload the scene.
You'll notice, and this is very important, that we also turn off the message queuing, If we don't, the photon server will stack messages for when we turn it on again, this is not what we want, we only need to receive messages from the photon server when we are all setup ( as we reload the scene, all Fsm will set everything properly to actually be ready), turning back On message queuing is done in the Photon DemoWorker Game Manager Fsmgame manager Fsm
-
Inform about the connection status
The last function of this Fsm, is to simply display the connection status of the Photon system when we are not connected. This is essentially the mechanism to inform the user that the system is doing something. There is three built in events for this purpose, PHOTON / STATE : CONNECTING , PHOTON / STATE : DISCONNECTED , PHOTON / STATE : CONNECTED For each of these events, we have a related state, where we either show a label, for example "connecting", "not connected", and when we are indeed connected, we show nothing, because the interface and other features of the application will make it obvious that we are connected.
Photon Actions used:
- Photon Network Get Is Connected ( in state "are we connected?")
- Photon Network Connect Using Settings ( in state "connect")
- Photon Network Get Room Properties ( in state "wait for room")
- Photon Network Set Is Message Queue Running ( in state "we are in, reload!" )