GPF Docs

Matchmaker Walkthrough

Matchmaker Walkthrough

This article documents the code that runs the Matchmaker project demonstrating how to create a matchmaker and Rock-Paper-Scissors match using GPF.


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 MatchmakerTest, see it turn Green.
  4. Right Click on Test and open the source code.
Notice that:
  • The PlayerActions Task is used by 2 players, "Bob" and "Gina".
  • Each PlayerActions uses a unique syncer.
  • PlayerActions are essentially a script of what each player will do, read through the code and comments to gain an understanding.
  • The client only interacts with MatchPlayerSO. It does so by sending it these messages:
    • Match
    • Accept
    • Move
  • The last line of PlayerActions assures the test only passes if the Match resolved with the correct state for both players.
await syncer.WaitFor(player, nameof(MatchPlayerSO.state), FieldIs.Equal(outcome));
  1. Open MatchPlayerSO.cs

MatchPlayerSO

MatchPlayerSO is responsible for the player's state in the cloud.

Notice that:
  • It's [Syncable], thereby allowing the client to access it's state.

  • The messages that the client sends up have handlers with the [FromClient] attribute, while the other's do not.

  • It interacts with other SOs, specifically:

    • MatchmakerSO
    • MatchGameSO

Diagram

Here are the interactions from the unit test. Go through them and try to match them up with the code.

CLIENT/hRTbtmatch_player/hRTbtmatchmaker/F3zrGCLIENT/091K.match_player/091K.match_game/u8XmjSetMatchmakerMatchMatchSetMatchmakerMatchMatchCreateOffersOfferAcceptAcceptOfferAcceptAcceptStartStartGameMoveMoveStartGameMoveMoveEndGameEndGameCLIENT/hRTbtmatch_player/hRTbtmatchmaker/F3zrGCLIENT/091K.match_player/091K.match_game/u8Xmj

Run the Game

Now let's actually play the game.

  1. Open the Matchmaker scene in the Assets/ folder.

  2. Do a build and run to run a desktop instance.

  3. Click the play button to run the editor instance.

  4. 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.

  5. Play

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 Matchmaker scene in the Assets/ folder and notice the Controller GameObject has a script attached to it called MatchController.cs (/Assets/Scripts). Open it.

MatchController.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 (match_player) so the MatchPlayerSO state can be easily referenced by the UI (See Hooking Up UI)
  3. Supports button calllback code for:
  • StartMatchMaking
  • StopMatchMaking
  • AcceptMatch
  • RejectMatch
  • Move
  • Leave

Let's look at ViewBindings on the the Hierarchy

If you turn on the inspector panel, you will notice that the Canvas.GameStates GameObjects has the following ViewBinding.

UIViewBindingbound to variable
Canvas.GameStatesSelectChildVBmatch_player.state

This video demonstrates how SelectChildVB is used to display states and state transitions:

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

UIFunction (defined in CoinController.cs)
Canvas.GameStates.MATCHMAKING.CancelButtonStopMatchMaking
Canvas.GameStates.TABLE_OFFERED.AcceptButtonAcceptMatch
Canvas.GameStates.TABLE_OFFERED.RejectButtonRejectMatch
Canvas.GameStates.GAME_STARTED.RockMove
Canvas.GameStates.GAME_WON.LeaveButtonLeave
Canvas.GameStates.GAME_LOST.LeaveButtonLeave

© 2023 Launch It Labs INC