Dev Log#17: Learning about Match States to make adjustments to time
- camjhill737
- Aug 5, 2024
- 3 min read
A difference between GameMode class and GameModeBase class:
Game mode has a match state functionality and provides custom match state functionality, they even state in the documented code that the game mode class is meant to serve multiplayer match based behaviour. So if you are creating a multiplayer game you're probably better off going with the game mode class to inherit from for your own game mode class. For a simpler game however then it is recommended to go with the game mode base class.
Match States include, EnteringMap, WaitingToStart, InProgress, WaitingPostMatch, LeavingMap and Aborted. Match State has a getter and setter
To implement custom match states it has to be between WaitingToStart and WaitingPostMatch. The reason for this I imagine is because before waiting to start you're in the stage of entering the map and after waiting post match you're leaving the map.
If a game needs to add additional states, you may need to override HasMatchStarted and HasMatchEnded to deal with the new states. As these functions handle the transitions for waiting to start to in progress and from in progress to waiting post match.
In my game there are 3 stages the waiting to start period where players are in spectator mode and they can fly around the map. I've labelled this as warm up time. Then the game starts and functions how it's it supposed to hopefully. Then once the game has reached its end, there's a cool down period where the winner is declared. The game is then restarted and it goes back to the warm up stage.
To display the time correctly to the players I created a getter function for retrieving the current server time. To display the correct time you need to get the total amount of time played in your game and subtract the current time on the server plus the time the level started. It's important to factor in the level start time because that starts ticking before your game starts.
So if the match is in progress you need to add the warm up time and the match time because that is how much time has passed in game so far. If you're in the cooldown stage you also need to add cooldown time to this.
Initially this didn't work because I messed up the parameters for my function to allow clients to join anytime after the server which handles the setting of match state and time to respond appropriately for them. But another couple of reasons where I discovered something interesting was that firstly, the client machines times all started at -1 I'm not sure of the exact reason for this but by adding safety check of is less than 0 and then setting the clients text value for its time solved the problem. One thing I did find from my research is that the player's state time is initialised as -1.
The third bug I encountered was that on the server the time was displayed in correctly because the controller is created before the game mode so we need to retrieve the time variables straight away on the controller from the game mode class and assign them to the controller's time variables. This is done in an has authority check as its only the server player that needs this functionality.
Comments