Description
A regenerating currency system limits how much a player can play in a given time window, similar to the lives or hearts mechanic in many mobile games. In this example, Lives (LV) deplete during combat and regenerate at a rate of one life every five minutes, up to a maximum of five. Players who run out of lives must either wait or spend Gems (GM) to purchase additional lives immediately. All game logic—including the outcome of battles, life deductions, and Gem grants—runs in CloudScript. This prevents clients from faking results or bypassing the life check.How it works
Authenticate
The client calls an authentication method (for example,
LoginWithCustomID) to obtain a valid session ticket.Trigger a battle
The client calls
ExecuteCloudScript with FunctionName: "Battle" to simulate gameplay.Battle checks for available lives
The
Battle CloudScript handler calls server.GetUserInventory to retrieve the player’s current Virtual Currency balances and recharge times. If the player has zero Lives, the function throws an error that includes the number of seconds until the next life regenerates.Calculate battle results
If the player has at least one Life, the script:
- Generates a random Gem reward between
GEM_MIN(10) andGEM_MAX(20) and credits it viaserver.AddUserVirtualCurrency. - Rolls a random number to determine if the player loses a Life (
CHANCE_TO_DIE = 0.3333). If so, one Life is deducted viaserver.SubtractUserVirtualCurrency.
Return results to the client
The script returns a result object containing
gemsFound and lostALife so the client can update the player’s display.PlayFab building blocks
- Accounts — player authentication and session management
- Virtual Currency (LV) — Lives, the play-limiting resource; configured with a recharge rate and cap
- Virtual Currency (GM) — Gems, the premium currency awarded during battles and used to buy lives
- Catalog — purchasable life items priced in Gems
- CloudScript —
Battlehandler enforcing life checks and calculating outcomes
Setup
Create the Lives virtual currency
In Game Manager, go to Economy > Currencies and select New Currency. Enter:
Select Save Currency.
| Property | Value | Detail |
|---|---|---|
| Code | LV | Abbreviation for the Lives currency |
| Name | Lives | Display name |
| Initial Deposit | 1 | Players start with one life |
| Recharge Rate | 288 | Recharges once every 5 minutes (24 hr × 60 min ÷ 5 min = 288 units/day) |
| Recharge Max | 5 | Maximum lives a player can hold |
Create the Gems virtual currency
Select New Currency again and enter:
Select Save Currency.
| Property | Value | Detail |
|---|---|---|
| Code | GM | Abbreviation for the Gems currency |
| Name | Gems | Display name |
| Initial Deposit | 5 | Starting balance for every new player |
Upload the catalog
Go to Economy > Catalogs and select Upload JSON. Select
Catalog.json from the PlayFab-JSON folder of this recipe. The catalog contains the purchasable life items priced in Gems.The CloudScript
TheBattle handler contains all gameplay logic and relies on two helper functions for Virtual Currency operations.
Helper functions
Helper functions
Running the example
- Unity 3D
- JavaScript
Install the PlayFab Unity SDK
Download the PlayFab Unity 3D SDK and import it into a new or existing Unity project.
Import the recipe package
Import
RegeneratingCurrencyRecipe.unitypackage from the Example-Unity3d folder (or download it from the PlayFab-Samples GitHub repo).Open the scene
In the Project window, open Assets > PlayFab Recipes > RegeneratingCurrency > Scenes and add the scene to your Hierarchy.
Set your title ID
Select the Main Camera. In the Inspector, set Play Fab Title Id to your PlayFab title ID.
