Tutorial - How to Build FlowWords
Outline
The FlowWords tutorial example project is the complete FlowWords game, with the FlowSDK and any SDK related code removed.
In this tutorial, you will learn how to;
- Configure the FlowSDK and local emulator for use with Unity
- Deploy contracts, create Accounts and run Transactions on the Flow emulator from within Unity.
- Incorporate Flow Accounts, Transactions and Scripts into Unity projects using Code
Requirements
FlowWords Tutorial has been tested on Unity version 2021.3.6f1
This tutorial assumes you have created a blank project, and installed the FlowSDK package from the Unity Asset Store, and FlowWords Tutorial Sample.
Step 1 – Configure the FlowSDK and local emulator
-
Open the scene at Assets\Samples\Flow SDK\<version>\Flow Words Tutorial Assets\Scenes\Game.unity
-
Open the Flow Control Manager window, at Window->Flow->Flow Control.
This will create a Flow Control GameObject in the Game scene, and a FlowControlData asset in the Resources folder if they do not already exist. -
Go to the Emulator Settings tab. If the manager states the Flow Executable cannot be found, use the Install button provided and follow the instructions to install the Flow CLI.
Note: If Unity still does not detect the emulator after installing the Flow CLI, you may need to restart your computer for the install to take effect. -
Set a directory for our Emulator data to reside in. This is where the emulator stores the state of the emulated Flow blockchain.
For the purposes of this tutorial, we will create a new folder called FlowEmulator inside our project folder. -
Click Start Emulator to start the emulator and create the emulator_service_account, and ensure “Run emulator in play mode” is checked.
NOTE: If this is the first time you have run the emulator on Windows, you may be presented with a dialog to allow access through your firewall. This is safe to allow.
-
Go to the Accounts tab and verify that the service account has been created.
This is the account we will use to create more accounts, and deploy our game contract on to the emulator. -
Back on the Emulator Settings tab, you can click Show Emulator Log, to view the output.
Step 2 – Deploy Contracts, create Accounts and run Transactions
We have provided you with the FlowWords game contract, but before we can interact with the contract, we have to deploy it to the blockchain.
We also have to set up some text replacements as, once deployed, our scripts will require hardcoded references to the contract name and deployed address.
Set up Text Replacements
Text replacements allow us to set and update references across all of our local Cadence scripts, without having to update each file individually.
-
Open the Flow Control Manager window, and navigate to the Text Replacements tab.
-
Set up the text replacements as follows. You can add text replacements by clicking the ‘+’ button at the top of the panel.
The anatomy of a text replacement is as follows;
Description: A friendly description, has no bearing on functionality.
Original Text: The original text, in your script files, which you want to replace.
Replacement Text: The text that you wish to replace the original text with. This is what gets submitted to the chain.
Active: This checkbox enables or disables the text replacement.
Apply to Accounts: Select which, if any, accounts this text replacement should apply to.
Apply to Gateways: Select which, if any, gateways (such as Emulator, or TestNet) this replacement should apply to.
Create User Accounts
While it would be perfectly possible to play our game with the emulator service account, we will often want to test our contracts with multiple different user accounts.
To create a new user account;
-
Open the Flow Control Manager window, and navigate to the Tools tab.
-
In the Create New Account section;
- Select the paying account. This will usually be the emulator_service_account
- Enter a friendly name for the new account. This is just for your own reference and does not get written to the chain.
- Click Create
-
If successful, the new account will appear under the Flow Control Manager Accounts tab.
Deploy the Contract
Before anyone can interact with a contract, it must be deployed to the blockchain.
We are going to deploy our game contract to the emulator, for local testing. But for deploying to Testnet, or Mainnet follow the same process.
-
Go to the Emulator Settings tab, in the Flow Control Manager, and Start the Emulator if it is not already running.
-
Go to the Tools tab. In the Manage Contracts section, enter the contract name of ‘FlowWords’ – this should match our CONTRACT_NAME text replacement precisely.
-
Populate the Contract field with game-contract.cdc, which can be found in Resources/contracts
-
Finally, ensure that the Account field contains emulator_service_account, and click the Deploy Contract button.
-
To check if the contract has deployed successfully, open the Flow Emulator Output window. Successful deployment will look as follows;
Submit Transactions
For administration purposes, it is sometimes useful to be able to directly submit transactions to the chain.
We can use a transaction to check out text replacements are set up correctly, and our game contract has successfully deployed.
-
In Flow Control Manager, navigate to tools.
-
In the Transactions section;
-
Populate the Transaction field with check-contract-deployed.cdc, located in Resources/transactions
-
Set the Signer field to the new Account we just created earlier. ‘Player1’
-
Click Execute Transaction
-
-
If you have successfully configured the SDK and deployed the game contract, you will see the following message;
Step 3 – Incorporate Flow Accounts, Transactions and Scripts into Code
We have our SDK and emulator configured, and our game contract deployed.
Now we are going to create the code which will allow our game to send and receive data from the blockchain.
Our FlowWords tutorial project contains a script called FlowInterface.cs, which can be found in the Scripts folder. This script contains all of our blockchain interfacing functions, which are called from GameManager.cs and UIManager.cs.
Our game FlowInterface has 5 main functions:
- Login
- Logout
- GetGameDataFromChain
- SubmitGuess
- LoadHighScoresFromChain
Our functions are going to need to access a number of cadence scripts and transactions.
These have been provided for you, and can be found in the Resources\scripts folder and Resources\transactions folder.
FlowInterface.cs has a number of Serialized TextAsset fields, which can be populated via the Unity inspector. Select the GameFlowInterface gameobject in the Game.unity scene, and populate the fields as follows, using the scripts and transactions in the aforementioned folders; (you may find these have already been populated for you)
Login
The Login function’s role is to take the credentials entered by the user, create a FlowControl.Account object with which we can submit transactions to the chain, and run the login.cdc transaction.
We first need to declare our FlowControl.Account object.
At the top of the file, add the following using statements to grant us easy access to the Flow SDK structures.
_10using DapperLabs.Flow.Sdk;_10using DapperLabs.Flow.Sdk.Cadence;_10using DapperLabs.Flow.Sdk.DataObjects;_10using DapperLabs.Flow.Sdk.DevWallet;_10using DapperLabs.Flow.Sdk.Unity;
Then declare our Flow account object on line 19. This will hold the credentials of the currently logged in player for use by all of our FlowInterface functons.
_10public FlowControl.Account FLOW_ACCOUNT = null;
Your file should now look like this:
We now have to register a wallet provider with the Flow SDK. We are going to use DevWallet, which comes with the Flow SDK, and is only intended for development purposes on emulator and testnet. Add the following to the Start
function:
_10// Register DevWallet_10FlowSDK.RegisterWalletProvider(ScriptableObject.CreateInstance<DevWalletProvider>());
Your Start function should now look like this:
NOTE: Do not use DevWallet in production builds. It is only intended for development purposes and is NOT secure.
Next, we will fill out the body of the Login function.
First, we have to invoke the wallet provider to authenticate the user and get their flow address. Add the following code to the Login function;
_10// Authenticate an account with DevWallet_10FlowSDK.GetWalletProvider().Authenticate(_10 "", // blank string will show list of accounts from Accounts tab of Flow Control Window_10 (string flowAddress) => StartCoroutine(OnAuthSuccess(username, flowAddress, onSuccessCallback, onFailureCallback)), _10 onFailureCallback);
The Authenticate function takes parameters as follows;
- The first parameter is a username which corresponds to the name of an account in the Accounts tab. If you leave this string blank (as above), a dialog will be shown to the user to select an account from the Accounts tab.
- The second parameter is a success callback for
Authenticate()
. We pass in a lambda function which starts a coroutine to run our async functionOnAuthSuccess
. This takes the flow address that we got fromAuthenticate()
, as well as a few other parameters. - The third parameter is a callback for if
Authenticate()
fails. We pass through the fail callback that was passed to Login.
Your completed Login function should look as follows;
Now we need to implement the OnAuthSuccess
function for when we successfully authenticate our user and get their flow address.
To sign transactions we need to assign a FlowControl.Account object, which we get from FlowControl using the flow address that the wallet provider's Authenticate
gave us.
With this information, we can now easily sign any transaction we like for the account in question.
_10// get FLOW account from address_10FLOW_ACCOUNT = FlowControl.Data.Accounts.FirstOrDefault(x => x.AccountConfig["Address"] == flowAddress);
WARNING: Having the Address and Private Keys to a blockchain account gives your application full access to all of that account’s funds and storage. They should be treated with extreme care.
Now that we have an Account object, we can use it to Submit our login.cdc transaction.
Add the following code beneath the Flow Account object creation code;
_10// execute log in transaction on chain_10Task<FlowTransactionResult> task = FLOW_ACCOUNT.SubmitAndWaitUntilExecuted(loginTxn.text, new CadenceString(username));_10while (!task.IsCompleted)_10{_10 int dots = ((int)(Time.time * 2.0f) % 4);_10 UIManager.Instance.SetStatus("Connecting" + new string('.', dots));_10 yield return null;_10}
Because transactions can take quite some time on chain, we create an asynchronous Task by calling SubmitAndWaitUntilExecuted on our newly created Flow Account object, to prevent blocking the main game thread.
Into SubmitAndWaitUntilExecuted, we pass the script that we want to execute, and any parameters.
For our script, we refer to the serialized TextAsset field, loginTxn, to which we will assign login.cdc in the inspector.
For parameters, the login.cdc script is expecting a single String parameter with the player’s display name in it. We pass in a new CadenceString object, which we create inline from the encapsulating function’s username string parameter.
Next, we simply wait until our asynchronous task.IsCompleted.
While we wait, we update the UI with a simple animated ‘Connecting…’ text status, and yield to the Unity engine to prevent blocking the thread.
Once our transaction has completed, we want to check if it was successful on chain. Add the following code beneath the transaction submission code;
_10// check for error. if there was an error, break._10if (task.Result.Error != null || task.Result.ErrorMessage != string.Empty || task.Result.Status == FlowTransactionStatus.EXPIRED)_10{_10 onFailureCallback();_10 yield break;_10}
Here we must check the transaction Result for three conditions:
- Error: Was there an error submitting the transaction to the blockchain?
- ErrorMessage: Was there an error during processing on the blockchain?
- Status: A status of EXPIRED means that the transaction did not execute on time and was discarded.
Any error here, and we are simply going to fail the login, and call our onFailureCallback.
Next, we process the result of our transaction.
In this case, if there was no error, then we have achieved our goals.
We have verified that the user owns the account, by virtue of them successfully signing a transaction, and we have, during the course of that transaction, created game related resources where required.
Add the following code to the bottom of the login function to call the onSuccess callback.
_10// login successful!_10onSuccessCallback(username, flowAddress);
You may also remove the final yield return null
if you wish.
Your entire OnAuthSuccess function should now look as follows;
Logout
The Logout function’s role is to clear the FlowControl.Account object, to prevent any more transactions from being executed with those account credentials.
The Logout function is very simple. Simply add the following line;
_10FLOW_ACCOUNT = null;_10FlowSDK.GetWalletProvider().Unauthenticate();
This clears the FlowAccount object, preventing any more transactions from being submitted with it, and unauthenticates the user from the wallet provider.
Your completed Logout function should now look like this;
GetGameDataFromChain
This function executes the get-current-gamestate.cdc transaction on the chain, and then processes the emitted events to get the CurrentGameState for the logged in account, and current GameStartTime for the game of the day, which we use to show time remaining.
As per the Login function, we are going to submit our transaction to chain using SubmitAndWaitUntilExecuted, and wait until our async Task is complete.
Add the following code to the GetGameDataFromChain function;
_15// execute getCurrentGameState transaction on chain_15Task<FlowTransactionResult> getStateTask = FLOW_ACCOUNT.SubmitAndWaitUntilExecuted(getCurrentGameStateTxn.text);_15while (!getStateTask.IsCompleted)_15{_15 int dots = ((int)(Time.time * 2.0f) % 4);_15 UIManager.Instance.SetStatus("Loading" + new string('.', dots));_15 yield return null;_15}_15_15// check for error. if so, break._15if (getStateTask.Result.Error != null || getStateTask.Result.ErrorMessage != string.Empty || getStateTask.Result.Status == FlowTransactionStatus.EXPIRED)_15{_15 onFailureCallback();_15 yield break;_15}
This time, we are passing in the transaction referenced in the serialized TextAsset field getCurrentGameStateTxn, which we populated in the inspector with get-current-gamestate.cdc.
This transaction requires no parameters, so we can simply exclude them.
This transaction is designed to return the game state for the user, and the time remaining on the word of the day, via emitted events.
We can access these emitted events via the .Result.Events property on our task.
To do so, add the following code below our submission logic;
_10// transaction success, get data from emitted events_10List<FlowEvent> events = getStateTask.Result.Events;_10FlowEvent currentStateEvent = events.Find(x => x.Type.EndsWith(".CurrentState"));_10FlowEvent startTimeEvent = events.Find(x => x.Type.EndsWith(".LastGameStart"));_10_10if (currentStateEvent == null || startTimeEvent == null)_10{_10 onFailureCallback();_10 yield break;_10}
This chunk accesses the returned Events list, and attempts to find events ending with “.CurrentState” and “.LastGameStart”. We use .EndsWith, as the transaction returns fully qualified event names, and the name of the deployed contract may change during development.
Finally, we check that we do indeed have both of our required events, and if not, call the onFailure callback and break.
Next, we will parse the contents of each event. Add the following code to the function;
_51// process current game state event_51// access event payload_51CadenceComposite statePayload = currentStateEvent.Payload as CadenceComposite;_51CadenceBase[] priorGuessResults = statePayload.CompositeFieldAs<CadenceArray>("currentState").Value;_51_51// iterate over prior guess results in payload, add to results list, and populate letter statuses_51List<GuessResult> results = new List<GuessResult>();_51Dictionary<string, string> letterStatuses = new Dictionary<string, string>();_51foreach (CadenceBase result in priorGuessResults)_51{_51 // add guess result to output_51 GuessResult newResult = new GuessResult_51 {_51 word = (result as CadenceComposite).CompositeFieldAs<CadenceString>("Guess").Value.ToUpper(),_51 colorMap = (result as CadenceComposite).CompositeFieldAs<CadenceString>("Result").Value_51 };_51 results.Add(newResult);_51_51 // add letters to lettermap_51 for (int i = 0; i < 5; i++)_51 {_51 bool letterAlreadyExists = letterStatuses.ContainsKey(newResult.word[i].ToString());_51 string currentStatus = letterAlreadyExists ? letterStatuses[newResult.word[i].ToString()] : "";_51 switch (currentStatus)_51 {_51 case "":_51 letterStatuses[newResult.word[i].ToString()] = newResult.colorMap[i].ToString();_51 break;_51 case "p":_51 break;_51 case "w":_51 if (newResult.colorMap[i] == 'p')_51 {_51 letterStatuses[newResult.word[i].ToString()] = newResult.colorMap[i].ToString();_51 }_51 break;_51 case "n":_51 if (newResult.colorMap[i] == 'p' || newResult.colorMap[i] == 'w')_51 {_51 letterStatuses[newResult.word[i].ToString()] = newResult.colorMap[i].ToString();_51 }_51 break;_51 }_51 }_51}_51_51// get game start time event_51double gameStartTime = 0;_51CadenceComposite timePayload = startTimeEvent.Payload as CadenceComposite;_51CadenceNumber startTime = timePayload.CompositeFieldAs<CadenceNumber>("startTime");_51gameStartTime = double.Parse(startTime.Value);
For the currentGameState event, we know that we are going to receive an Event object, containing an array of UserGuesses, which each contain two String objects.
Since Events are of the CadenceComposite type, we get the currentStateEvent.Payload, as CadenceComposite.
Now that we have the event structure, we can get the UserGuesses array, by calling CompositeFieldAs<CadenceArray>(“currentState”) on our event structure object.
This function retrieves the field “currentState”, which we can see in the event declaration above, and casts it to the CadenceArray type for us.
We access the Value property of this, and assign it to CadenceBase[] priorGuessResults for easy iteration.
Next, we iterate over out priorGuessResults array.
Currently, the array elements are of type CadenceBase, but we know from our game contract that they are a struct, which is a CadenceComposite type, so for each iteration, we cast the element to CadenceComposite, and then once again use CompositeFieldAs<CadenceString> to access both the “Guess” and “Result” member strings from the struct, and store them in our own local result list.
The GameStartTime event is simpler.
This event contains a single variable of type UFix64, an unsigned 64 bit fixed point number. This is the standard data format on the Flow blockchain to store timestamps.
As with the guess results, we first cast our event payload to the CadenceComposite type, and then we simply get the startTime parameter using CompositeFieldAs<CadenceNumber>(“startTime”).
CadenceNumber encompasses all numerical data types on chain. The value is stored as a string, and so we use double.Parse to convert this back to a number for our own use. Double being the closest data type to UFix64 supported in C# natively, and close enough for our use case.
Finally, we call our onSuccess callback to return our results to our caller.
Add the following lines to the bottom of the function;
_10// call GameManager to set game state_10onSuccessCallback(gameStartTime, results, letterStatuses);
Your completed function should now look like this;
SubmitGuess
This function has two phases. First, it checks that the entered word is valid by submitting the check-word.cdc script to chain, and processing the returned value.
If the word is deemed valid, it then submits the word guess to the game contract using the currently logged in user’s credentials, by executing the submit-guess.cdc transaction script on chain, and then processing the emitted events.
For phase one, enter the following code at the top of the SubmitGuess function;
_23// submit word via checkWord script to FLOW chain to check if word is valid_23Task<FlowScriptResponse> checkWordTask = FLOW_ACCOUNT.ExecuteScript(checkWordScript.text, new CadenceString(word.ToLower()));_23_23while (!checkWordTask.IsCompleted)_23{_23 int dots = ((int)(Time.time * 2.0f) % 4);_23 UIManager.Instance.SetStatus("Waiting for server" + new string('.', dots));_23 yield return null;_23}_23_23if (checkWordTask.Result.Error != null)_23{_23 onFailureCallback();_23 UIManager.Instance.SetStatus("Error checking word validity.");_23 yield break;_23}_23_23bool wordValid = ((checkWordTask.Result.Value as CadenceString).Value == "OK");_23if (wordValid == false)_23{_23 onFailureCallback();_23 yield break;_23}
This code starts by calling ExecuteScript, passing in the checkWordScript and our guess word as a CadenceString object, to create an async Task using our Flow Account object.
Scripts on Cadence can be thought of as read-only transactions, which are performed very quickly.
Since scripts are read only, they do not require signing, and are best to use when you need to quickly get publicly available data from chain.
As with our previous transactions, we then wait until our task.IsCompleted, and then check for any errors in the result. With scripts we only have to check the Result.Error, as this catches all possible failure modes.
We then process the return value of the script, which can be found in the Result.Value property on our completed task object. Scripts do not emit events like transactions, but have return values like a regular function.
The return value is of the generic base type CadenceBase, which we cast to CadenceString, as we are expecting a string type return value.
If the word guess is deemed to be invalid we call the onFailure callback and break, otherwise we proceed onto the guess submission phase.
For the second phase of the function, add the following code below phase one;
_39// word is valid, submit guess via transaction to FLOW chain_39Task<FlowTransactionResult> submitGuessTask = FLOW_ACCOUNT.SubmitAndWaitUntilExecuted(submitGuessTxn.text, new CadenceString(word.ToLower()));_39_39while (!submitGuessTask.IsCompleted)_39{_39 int dots = ((int)(Time.time * 2.0f) % 4);_39 UIManager.Instance.SetStatus("Waiting for server" + new string('.', dots));_39 yield return null;_39}_39_39if (submitGuessTask.Result.Error != null || submitGuessTask.Result.ErrorMessage != string.Empty || submitGuessTask.Result.Status == FlowTransactionStatus.EXPIRED)_39{_39 onFailureCallback();_39 yield break;_39}_39_39// get wordscore_39string wordScore = "";_39FlowEvent ourEvent = submitGuessTask.Result.Events.Find(x => x.Type.EndsWith(".GuessResult"));_39if (ourEvent != null)_39{_39 CadenceComposite payload = ourEvent.Payload as CadenceComposite;_39 wordScore = payload.CompositeFieldAs<CadenceString>("result").Value;_39_39 // check if we are out of guesses_39 if (wordScore == "OutOfGuesses")_39 {_39 onFailureCallback();_39 UIManager.Instance.SetStatus("Out Of Guesses. Try again tomorrow.");_39 yield break;_39 }_39_39 // process result_39 onSuccessCallback(word, wordScore);_39}_39else_39{_39 onFailureCallback();_39}
This phase begins by submitting the submit-guess.cdc transaction, passing in our guess word as a new CadenceString parameter. We then wait for the task to complete as usual, and check for any errors.
As this is a transaction, we once again check the three possible failure modes, and call the onFailure callback if the transaction failed.
Next we parse our transaction’s emitted events.
We are expecting an event called GuessResult, with a single string parameter called result.
We first find our event in the Result.Events list on our task object.
If our event is found, we then cast the event.Payload to our CadenceComposite type, as Events are composite types, and then call CompositeFieldAs<CadenceString>(“result”) on our cast payload, to retrieve our string ‘result’ field. We then pass the guess word, and the result back to our caller via the onSuccess callback.
If the GuessResult event cannot be found in the Result.Events list, we call the onFailure callback.
Once complete, your SubmitGuess function should look like this;
LoadHighScoresFromChain
This function fires off a number of small scripts simultaneously, which pull publicly available high score data from the game contract on chain using;
- get-highscores.cdc
- get-player-cumulativescore.cdc
- get-player-guess-distribution.cdc
- get-player-maxstreak.cdc
- get-player-scores.cdc
- get-player-streak.cdc.
It then processes their returned values, and passes them out to the onSuccess call for the high scores UI to render.
For this function, we are going to first fire off a number of simultaneous scripts on the blockchain. This is something you want to avoid with transactions, as transaction order of execution cannot be guaranteed due to the distributed nature of blockchain, however as scripts are read-only, and do not mutate the chain, order of execution is far less likely to matter.
To execute the scripts, add the following code to the top of the function;
_29// execute scripts to get highscore data_29Dictionary<string, Task<FlowScriptResponse>> tasks = new Dictionary<string, Task<FlowScriptResponse>>();_29tasks.Add("GetHighScores", FLOW_ACCOUNT.ExecuteScript(GetHighScores.text));_29tasks.Add("GetPlayerCumulativeScore", FLOW_ACCOUNT.ExecuteScript(GetPlayerCumulativeScore.text, new CadenceAddress(FLOW_ACCOUNT.AccountConfig["Address"])));_29tasks.Add("GetPlayerWinningStreak", FLOW_ACCOUNT.ExecuteScript(GetPlayerWinningStreak.text, new CadenceAddress(FLOW_ACCOUNT.AccountConfig["Address"])));_29tasks.Add("GetPlayerMaxWinningStreak", FLOW_ACCOUNT.ExecuteScript(GetPlayerMaxWinningStreak.text, new CadenceAddress(FLOW_ACCOUNT.AccountConfig["Address"])));_29tasks.Add("GetGuessDistribution", FLOW_ACCOUNT.ExecuteScript(GetGuessDistribution.text, new CadenceAddress(FLOW_ACCOUNT.AccountConfig["Address"])));_29_29// wait for completion_29bool complete = false;_29while (!complete)_29{_29 complete = true;_29 foreach (KeyValuePair<string, Task<FlowScriptResponse>> task in tasks)_29 {_29 complete = complete && task.Value.IsCompleted;_29 }_29 yield return null;_29}_29_29// check for errors_29foreach (KeyValuePair<string, Task<FlowScriptResponse>> task in tasks)_29{_29 if (task.Value.Result.Error != null)_29 {_29 onFailureCallback();_29 yield break;_29 }_29}
This block of code creates a Dictionary<string, Task> to store our concurrent script tasks.
We then sequentially create async Tasks for each script that we want to execute, using ExecuteScript, and add them to the Task dictionary.
In our use case, we want all of the tasks to complete before we display any results, so our wait for completion code block iterates over every Task in the dictionary, and only moves on once every task.IsComplete.
Checking for errors is similarly done using a foreach loop, where every task is checked to ensure the Error field is null. If even one task has an Error, we call the onFailure callback and break.
Next we need to process the returned values. Add the following code beneath the previous;
_31// load global highscores_31CadenceBase[] highscores = (tasks["GetHighScores"].Result.Value as CadenceArray).Value;_31List<ScoreStruct> GlobalScores = new List<ScoreStruct>();_31foreach (CadenceComposite score in highscores)_31{_31 ScoreStruct parsedScore = new ScoreStruct("", 0);_31 foreach (CadenceCompositeField field in score.Value.Fields)_31 {_31 switch (field.Name)_31 {_31 case "Name":_31 parsedScore.Name = (field.Value as CadenceString).Value;_31 break;_31 case "Score":_31 parsedScore.Score = int.Parse((field.Value as CadenceNumber).Value);_31 break;_31 }_31 }_31_31 GlobalScores.Add(parsedScore);_31}_31GlobalScores = GlobalScores.OrderByDescending(score => score.Score).Take(10).ToList();_31_31// load player scores_31uint PlayerCumulativeScore = uint.Parse((tasks["GetPlayerCumulativeScore"].Result.Value as CadenceNumber).Value);_31uint PlayerWinningStreak = uint.Parse((tasks["GetPlayerWinningStreak"].Result.Value as CadenceNumber).Value);_31uint PlayerMaximumWinningStreak = uint.Parse((tasks["GetPlayerMaxWinningStreak"].Result.Value as CadenceNumber).Value);_31uint[] PlayerGuessDistribution = (tasks["GetGuessDistribution"].Result.Value as CadenceArray).Value.Select(value => uint.Parse((value as CadenceNumber).Value)).ToArray();_31_31// callback_31onSuccessCallback(GlobalScores, PlayerCumulativeScore, PlayerWinningStreak, PlayerMaximumWinningStreak, PlayerGuessDistribution);
Our global highscores are an array of Scores objects.
As such, we cast the Result.Value (return value) of our GetHighScores task to a CadenceArray object, and store the .Value of this in CadenceBase[] highscores.
We iterate over this highscores array, and cast each Scores struct element to a CadenceComposite object. We then iterate over each field in the CadenceComposite object, and assign the .Value of each to our own local ScoreStruct object, which we then add to a GlobalScores list for later display.
Next, we parse the detailed statistics for the current player.
Each of these results are a simple cast of the Result.Value to a CadenceNumber, which we then parse to an unsigned integer. The final statistic follows the same idea, but uses some LINQ trickery to process all of the elements of a CadenceArray in a single line.
Finally, we call the onSuccess callback, passing in all of our parsed results.
You may also remove the final yield return null
statement at this point, if you wish.
Once complete, your function should look as follows;
Step 5 – Play FlowWords!
If you have correctly followed all of the steps above, you will now have a working project.
- Return to the Unity editor, and press the Play button.
- Enter a friendly name - this will appear on the leaderboard.
- Click Log In.
- Select an account from the dialog that appears to authenticate with.
You should see the login screen, say Connecting…, and then Loading…
Followed shortly thereafter by the game screen;
And the High Scores screen (if you click the button);
Step 6 – Further Experimentation
For an extra challenge, try some of the following;
- Add more accounts and play with some friends, hot seat style
- Modify the game-contract.cdc to make a new game every 5 minutes instead of every 24 hours.
- Try to remove and redeploy the contract
(hint: once removed, a contract’s name can never be used again on the same account)
(extra hint: delete-game-resources.cdc) - Poke about in the game contracts, scripts and transactions to see what they do!
If you ever get the emulator into a messy state, you can always hit the Clear Persistent Data button, which will wipe the emulator back to its blank state. This will of course lose all deployed contracts and high score and game history.