8. Handle network errors and repeated clicks

Test repeated clicks, insufficient coins, and network errors in your shop.

Try clicking the purchase button repeatedly and disconnecting the network. Continue with the same player you have been using.

Click again while a purchase is processing

  1. Prepare at least 20 coins. Clear quests to earn more if needed.
  2. Click Restore 10 stamina for 20 coins, then immediately click again.
  3. Check that the button is disabled during processing and cannot start another purchase.

The code sets busy to true when an operation starts and resets it to false in finally. In addition to disabling buttons, Studio’s Run() and the SDK’s RunWrite() reject calls while busy.

Clicking after the purchase completes and the button is enabled starts a new purchase.

Check the insufficient-coins message

  1. Buy repeatedly until your balance is below 20 coins.
  2. Click the purchase button again and check for the insufficient-coins message.
  3. Check that the failed purchase neither spends coins nor restores stamina, excluding any natural recovery.
  4. Start Forest exploration to check that quest controls still work, then clear it.

For insufficient coins or stamina, show the reason and allow the next operation.

Allow retries of the same purchase

When a connection drops, the server may have completed the purchase but its response may not have arrived. Sending a new purchase would spend coins again.

Assign one key to each purchase. Resend the same content with the same duplicationAvoider key, and GS2 returns the completed purchase’s result without repeating it.

Stop Play Mode and replace the matching file for your course (in the Agent course, use the request in the Agent tab below). Keep the Inspector references.

Download AdventureStudioView.cs.

The purchase method Buy() now looks like this. purchaseId is a class field.

private string purchaseId;

private async Task Buy()
{
    purchaseId ??= Guid.NewGuid().ToString();
    var request = Gs2ClientHolder.Instance.Gs2.Exchange.Namespace("EnergyProduct")
        .Me(Gs2GameSessionHolder.Instance.GameSession).Exchange();
    var transaction = await request.ExchangeAsync(
        "refill", 1, speculativeExecute: false, duplicationAvoider: purchaseId);
    if (transaction != null) await transaction.WaitAsync(all: true);
    purchaseId = null;
}

Download AdventurePlay.cs. Keep AdventurePlayer.cs and AdventureLogin.cs as they are.

The purchase method BuyRefill() now looks like this. purchaseId is a class field.

private string purchaseId;

private async UniTask BuyRefill()
{
    purchaseId ??= Guid.NewGuid().ToString();
    var transaction = await player.Gs2.Exchange.Namespace("adventure-shop").Me(player.Session)
        .Exchange().ExchangeAsync(
            "refill", 1, speculativeExecute: false, duplicationAvoider: purchaseId);
    awaitingResult = true;
    if (transaction != null) await transaction.WaitAsync(all: true);
    purchaseId = null;
}

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. Also check the two sections at the top of this page (clicking again while a purchase is processing, and not enough coins) after this request is done. Disconnect the network yourself for the checks. Do not let the agent change your computer’s settings.

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 and Cave exploration),
starting and clearing quests with a coin reward, stamina, and a shop where coins restore
stamina (10 stamina for 20 coins).

Finally, I want to tidy up what happens when things go wrong.
- If the purchase button is clicked repeatedly, do not start another purchase
  while one is processing.
- When there are not enough coins, make the reason (not enough coins) clear on the screen.
  A failed purchase must not spend coins or restore stamina.
- When the connection drops and the result of a purchase is unknown,
  make sure that retrying the same purchase does not buy it twice.
  Once a retry gets the result, treat the next purchase as a new purchase.
- When the connection drops, show a message that explains the situation,
  such as "Check your connection".
  I want starting and clearing quests to make the situation clear in the same way.
- Do not change this computer's network settings (such as Wi-Fi).
  Either make it possible to reproduce a dropped connection inside the game,
  or write it as steps for me to try myself.
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

The agent decides the wording of the buttons and messages, so read them as the names in its report. For every check, note your coins and stamina before and after the action and compare them, leaving out any natural recovery.

Check by playing: In addition to “Try disconnecting the network” below, try the following.

  • Even if you click the purchase button many times quickly, only 20 coins are spent
  • With fewer than 20 coins, the screen makes it clear that you do not have enough coins, and neither coins nor stamina change
  • If you turn off Wi-Fi and buy, a message such as “Check your connection” appears. After you reconnect and retry, only 20 coins are spent and stamina goes up by only 10
  • After the retry completes the purchase, buying again counts as a new purchase and spends another 20 coins
  • If you turn off Wi-Fi and click Start or Complete for a quest, a message that explains the situation also appears
  • If the agent made it possible to reproduce, inside the game, the case where only the result fails to arrive, try that too. Retrying does not spend coins twice

Turning off Wi-Fi stops the purchase request itself from reaching GS2. Turning off Wi-Fi alone cannot create the case where GS2 completes the purchase but only the result fails to arrive, so check that case with the way the agent built to reproduce it.

Check by asking the agent

  • “Why does retrying after the connection drops not buy it twice?”
  • “How long is the information for retrying kept? What happens if I stop Play Mode?”
  • “Could starting or clearing a quest also happen twice?”
  • “What did you not try?”

GS2 remembers the same purchase for one hour. Retry within an hour after you reconnect.

Practice

Ask the agent to change messages such as Processing, Not enough coins, and Check your connection to wording you came up with yourself. Create each situation and check that your new wording appears.

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.
  • Check that the connection is back, then retry.

The Agent course ends with this chapter. The extensions are written for the Studio and SDK courses, and following them with an agent has not been tried.

??= assigns a value only when none exists. A network error leaves purchaseId intact, so a retry uses the same key. Completion clears it so the next purchase gets a new key. The error handler also clears it when the purchase is rejected for insufficient coins.

After a network error, the button changes to Retry the same purchase. Until the result arrives, you can retry this purchase, but cannot start another quest or a new purchase.

Try disconnecting the network

  1. Enter Play Mode and log in. Prepare at least 20 coins and at most 10 stamina, then note both values.
  2. Disconnect your computer from the network and click the purchase button.
  3. Wait for a network error and the Retry the same purchase button.
  4. Reconnect the network and, without stopping Play Mode, click Retry the same purchase.
  5. Check that 20 coins are spent and 10 stamina restored, excluding natural recovery. The button returns to its normal label when the purchase completes.

Next, click purchase while connected and disconnect immediately afterward. If the response arrived first and completed the purchase, the normal purchase button returns. Reconnect and resend only if the retry button appears.

This sample keeps the key in memory during Play Mode, so do not stop Play Mode during the test. GS2 retains duplicate-avoidance results for one hour; retry as soon as you reconnect in this exercise.

Practice: write helpful status messages

Try messages that explain the current state, such as Processing, Not enough coins, or Check your connection. Edit the text in Studio’s Run(), or the SDK’s RunWrite() and WriteError().

Next: Add more features