GPF Docs

Coin Flipper Walkthrough

Coin Flipper Walkthrough

Coin Flipper is a single-player demonstration where the player's longest streak of flipping 'heads' is sent up to the leaderboard. This article documents the engineering behind Coin Flipper.


Run the Tests locally

  1. Change the project to Simulated mode in Unity Editor -> GPF -> Settings -> Backend Type
  2. Open Test Runner Unity Editor -> Window -> General -> Test Runner
  3. Run HellContentTest, see it turn Green.
  4. Run CoinTest, see it turn Green.
  5. Right Click on CoinTest and open the source code.
Notice that:
  1. The CoinTest class extends ServerObjectTest
  2. There is a function to simulate player actions PlayerActions.
  3. The main entry point Run starts multiple PlayerActions in parallel to simulate players.
  4. Each player creates a Syncer using ServerObjectTest.CreateSyncer
  5. Players use Syncer.SendWait to set the username, this will return when the server has processed the message
  6. Players send a coin Flip messages with Syncer.Send (which returns immediately)
  7. Players wait for the coin flip to resolve with Syncer.WaitFor which will return once the server has determined the result

Server Objects

Now skim through the code for every ServerObject (CoinPlayerSO, CoinLeaderboardSO). Our ServerObject Guide Video provides a detailed breakdown of what these ServerObjects do.

Below we will diagram what the server objects do. This is where the stateful part of the game is defined. These SO's are all located here: Assets\ServerObjects

SO NameDescription
CoinPlayerSORepresents the player, handles flipping coin
CoinLeaderboardSOStores everyone's top score. Sends the top few scores to players upon request

Sequence diagrams are ideal to visualize how SO's work. Try to match the code up with the sequence diagram.

ClientCoinPlayerSOCoinLeaderboardSOFlipFlipResultSendScoreTopScoresparClientCoinPlayerSOCoinLeaderboardSO

Run the Game

Now let's actually play the game.

  1. Open the Coin scene in the Assets/ folder.
  2. Click the play button, and flip the coin.
  3. Open the Data Explorer GFP -> Data Explorer so you can watch the state of the game change as you play through. You will have to select the Data Explorer to make sure it's up to date.
  4. The goal is to get the yellow side as many times as possible.

UI

At this point we should understand how state is determined and how the game looks, but now lets look at how the SO's are hooked up to UI.

Open the Heirarchy for the Coin scene in the Assets/ folder and notice the Controller GameObject has a script attached to it called CoinController.cs (/Assets/Scripts). Open that.

CoinController.cs does the following:

  1. Creates a Syncer, which provides
    • The ability to sync UI to SOs
    • The ability to send messages to SOs.
  2. Adds an alias to the DataStore so the CoinPlayerSO state can be easily referenced by the UI (See Hooking Up UI)
  3. Event functions for the Button and Input Field to connect to in the scene (Flip, SetUsername)

Let's look at ViewBindings on the the Hierarchy

If you turn on the inspector panel, you will notice some GameObjects have ViewBindings:

UIViewBindingbound to variable
Canvas.UsernameInputFieldTextInputVBplayer.username
Canvas.LongestStreak.ValueTextVBplayer.longestStreak
Canvas.CurrentStreak.ValueTextVBplayer.currentStreak
Canvas.LeaderboardLeaderboardVBplayer.leaderboard

You will also notice components have events that reference CoinController functions for example:

UIFunction (defined in CoinController.cs)
Canvas.UsernameInputFieldSetUsername
Canvas.FlipFlip

© 2023 Launch It Labs INC