6. Restore stamina with coins
Let’s spend the coins earned from quests. Create a product named refill that restores 10 stamina for 20 coins. Players can buy it with the free coins received as quest rewards.
Process payment and recovery together
Combine the coin payment and stamina recovery into one transaction. If the player has too few coins, the purchase must fail without restoring stamina.
Steps
Create the product table
Add Stamina Shop under Add features, then create the following row in Tables → Stamina product (EnergyProduct).
| Field | Value |
|---|---|
| ID | refill |
| Recovery target (energy) | The stamina row |
| Recovery amount (recoveryValue) | 10 |
| Purchase cost (consumeActions) | One consume action, configured below |
Double-click Purchase cost and open Add item → Not set. Select WithdrawByUserId (withdraw from wallet), leave Target resource at None, and enter these values.
| Field | Value |
|---|---|
| namespaceName | Currency |
| userId | #{userId} |
| slot | 0 |
| withdrawCount | 20 |
| paidOnly | false |
Setting paidOnly to false allows payment with the free coins earned in this tutorial.
Close the editor on the right. Check that refill targets stamina, restores 10, and has one purchase-cost action, then save.
Deploy to GS2 and update Unity. Replace AdventureStudioView.cs with this chapter’s version to add a purchase button. Check the existing Quests, Progress, Wallet, and Energy assignments.
The stamina shop package creates an Exchange namespace named EnergyProduct. The purchase button calls Buy() below. Layout and current-value display are kept in separate methods.
private async Task Buy()
{
var request = Gs2ClientHolder.Instance.Gs2.Exchange.Namespace("EnergyProduct")
.Me(Gs2GameSessionHolder.Instance.GameSession).Exchange();
var transaction = await request.ExchangeAsync("refill", 1, speculativeExecute: false);
if (transaction != null) await transaction.WaitAsync(all: true);
}Update the settings
Download the chapter 6 template. Open Deploy → adventure-game → Update stack and select it under Template data. This complete template includes the previous resources. Review and apply the changes, then wait for Update complete.
The template’s refill combines Gs2Money2:WithdrawByUserId with withdrawCount = 20 and paidOnly = false, and Gs2Stamina:RecoverStaminaByUserId with recoverValue = 10. The exchange uses timingType: immediate.
Update the Unity code
Download AdventurePlay.cs and replace the file in Assets. Keep chapter 5’s AdventurePlayer.cs.
Keep the existing Inspector settings.
The relevant part of the script is shown below. You do not need to add this excerpt as another class.
private async UniTask BuyRefill()
{
var transaction = await player.Gs2.Exchange.Namespace("adventure-shop").Me(player.Session)
.Exchange().ExchangeAsync("refill", 1, speculativeExecute: false);
awaitingResult = true;
if (transaction != null) await transaction.WaitAsync(all: true);
}What you do
Start the agent in your working folder and give it the following request. When the agent finishes, try the checks under “What to check” below yourself.
Request for the agent
In this folder, I am building a small adventure game with GS2 and Unity.
The GS2 project is adventure, the region is ap-northeast-1,
and I have already signed in.
This folder contains the GS2 Studio project Adventure
and the Unity project AdventureUnity.
So far, I have built login, the quest list (Forest exploration),
starting and clearing quests with a coin reward (10 free coins per clear),
and stamina (maximum 20, recovers by 1 every 5 minutes, 5 used to start a quest).
This time, I want to build a shop where coins restore stamina.
- The product identifier is refill.
Paying 20 coins (paid or free, either is fine) restores 10 stamina.
- Put a purchase button on the screen, such as "Restore 10 stamina for 20 coins".
- The coin payment and the stamina recovery must never happen one without the other.
- After a purchase, update the coin balance and stamina shown on the screen.
- While a purchase is processing, do not let the purchase button be clicked.
Keep everything built so far working.
Deploy to GS2 and update Unity, then enter Play Mode in Unity to check that it works.
When you are done, write the steps for me to check the result and a list of the numbers
you chose, without technical terms. If there is anything you did not try, list that too.What to check
Check by playing: In addition to “Try buying the product” below, try the following.
- With exactly 20 coins, you can buy it, and you have 0 coins afterward
- With 19 coins or fewer, you cannot buy it, and neither coins nor stamina change
- Buying at 15 stamina stops at 20 (it does not go over the maximum)
Check that the numbers are what you intended
- The product list in the agent’s report (price, which coins can be used, and amount restored) matches the request
- The price on the button matches the number of coins actually spent
Check by asking the agent
- “Why can the payment and the recovery never happen one without the other?”
- “If I buy when stamina is close to the maximum, what happens to the amount that goes over?”
- “If the connection drops during a purchase, could it be bought twice?” (Chapter 8 covers this.)
- “What did you not try?”
Practice
Ask the agent, “Change the product’s price to 25 coins, and update the button text to match.” Once the change is applied, check that both the price on screen and the coins spent when you buy are 25. If the display says 25 but only 20 coins are spent, wait about five minutes and check with the next purchase. When you are done, ask the agent to change it back to 20 coins.
If something goes wrong
- Check Unity’s Console for red errors. If there are any, paste the message to the agent and ask it to fix the problem.
- Check what the agent’s report lists as not tried.
- If you have just changed the settings, wait five minutes before checking.
Disable the button during the purchase, then display the updated coins and stamina when it completes.
Try buying the product
Use the coins you earned to restore stamina for the same player.
- Make sure you have at least 20 coins and at most 10 stamina. If you need coins or have too much stamina, start and clear Forest exploration to adjust them.
- Note the coins and stamina before clicking the purchase button.
- Check that coins decrease by 20 and stamina increases by 10. For example, 30 coins and 5 stamina become 10 coins and 15 stamina.
- Stop and restart Play Mode. Studio logs in automatically; with the SDK, click Log in. Check that the new balance and restored stamina remain.
Stamina recovers up to 20. Include any natural recovery that occurs during these actions or the restart when comparing values.
With fewer than 20 coins, the purchase fails and an error appears. A failed purchase neither spends coins nor restores stamina.
Practice: change the settings
Change the price to 25 coins. First, clear Forest exploration until you have at least 25 coins and at most 10 stamina. Does the screen show 25, and does buying spend 25 coins? After deployment, cost changes can take up to five minutes to take effect. If the display says 25 but only 20 are spent, wait a little and check the next purchase.
Hint
In Studio, set withdrawCount in the product’s purchase cost to 25; with the SDK, change it in the template. Deploy the change. The sample button uses fixed text, so also change Restore 10 stamina for 20 coins to Restore 10 stamina for 25 coins.
Before moving on
Restore the price to 20 coins and update both the display and the cost settings. Keep recovery at 10. Wait five minutes after deployment, earn coins in Forest exploration, then buy and check that 20 coins are spent before moving on.

