Part 8 : Gamestate and PlayerState

Back to main post

According to documentation: The GameState exists on the server and the clients, so the server can use replicated variables on the GameState to keep all clients up-to-date on data about the game. Information that is of interest to all players and spectators, but isn’t associated with any one specific player, is ideal for GameState replication. As an example, a baseball game could replicate each team’s score and the current inning via the GameState.

According to documentation: A PlayerState will exist for every player connected to the game on both the server and the clients. This class can be used for replicated properties that all clients, not just the owning client, are interested in, such as the individual player’s current score in a free-for-all game. Like the PlayerController, they are associated with individual Pawns, and are not destroyed and respawned when the Pawn is.

For this part I will make a little game : Collecting coins.
At the start there are for example 1000 coins in the bank. Each player can collected coins. When there are no coins left in the bank, the game is finished. Each client gets a message showing the coins collected by each player.

Although it looks a bit overdone for such a small game, I used gamemode, gamestate and playerstate:

  • Gamemode : Setting the max coins in the bank (1000)
  • Gamestate : Calculating collected coins by players and check if coins in bank are finished.
  • Playerstate : Tracking number of coins collected by player.
  • Playercontroller : used for connecting menu widget with gamestate and playerstate.

In the next screenshots I show the final blueprints, when building you most likely need to go back and forth.

Note: I only used the interface of gamestate and playerstate to get a reference.
I should have used more functions going through the interface.

Step 1 Create Gamestate, create interface, create reference

Step 2 Create PlayerState, create interface, create reference

Step 3 Add Gamestate and Playerstate to you gamemode!!
At first I had forgotten to do this 🙁

Step 4 Changes to the mainmenu

Added : PS Get List, PS Print, Add Coins, Print Coins, Start Collection Coins
PS Get List
Ps Print
Add Coins
Print coins
Start Collecting coins
Add change player name in playerstate add the end of continuebutton

Step 5 GameMode

Event SetNameInPlayerState. This is the way to change the playername of the playerstate.
Function GetCoinCount
Variable Maxcoins

Step 6 GameState

Variable TotalCoinsCollected
Blueprints Event Graph
On Rep Coins Left
On Rep Game Finished
On Rep Start Game

Step 7 : PlayerState

Event Print Name Player State and variable CoinsCollected
Increase coins
Several blueprints

Step 8 : Playercontroller

Most of the events in the playercontroller are necessary to connect the menu widget to the gamestate and playerstate. So they are called from the menu, to get something done in the gamestate or playerstate

Blueprints Playercontroller

Step 9 : BP_Pickup

Changes made to the existing BP_Pickup. In the first event you should add an has authority switch, to make sure coins is only set on the remote. The dedicatie server has no coins (has no graphical interface). At first I got a warning in the dedicated server log.

Blueprints Even graph and variable NumberOfCoins
Added textrenderer and RotatingMovement

Step 10 : New Blueprint with cube (only one) called BP_Cube

Blueprints Event graph

Result when running in multiplayer

Share Button

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.