Build items and a shop

Add potions to adventure rewards, then create buttons to use and buy them.

Let players earn one potion along with coins for completing Forest exploration. Using a potion restores 5 stamina, and the shop sells one for 20 coins.

1. Register the potion

Store items in a Simple Inventory, which tracks the quantity of each item type. The potion’s ID is potion.

  1. Add Items from Add features.
  2. Under Tables → Item, add a row with ID potion. Quantity belongs to player data, so do not enter it in this table.
  3. Deploy from Publish → Calculate server differences, then run Update the Unity project.
  4. Download the potion use and purchase template.

Download the potion storage, use, and purchase template. It contains these settings.

SettingValue
Inventory namespaceadventure-inventory
Simple InventoryInventory
Itempotion
Exchange namespace for use and purchaseadventure-item-actions

Deploy the downloaded template through the management console.

  1. In the Tokyo region, open Deploy → Stack list → Create stack.
  2. Name the stack adventure-items and select the downloaded JSON file under Template data.
  3. Click Create and wait for Create complete.

This creates two exchange recipes in adventure-item-actions.

Exchange nameConsumesGrants
use_potion1 potion5 stamina
buy_potion20 free coins1 potion

2. Add a potion to Forest exploration rewards

Open forest under Tables → Quests → Quest. Under Completion rewards → Add item → Not set, add the following action. Keep the coin deposit and the mission counter action added on the previous page.

FieldValue
ActionAcquireSimpleItemsByUserId (inventory)
Target resourceNone
namespaceNameInventory
inventoryNameInventory
userId#{userId}

Add one entry to acquireCounts, with itemName = potion and count = 1. Save, deploy to the server, and update the Unity project.

Open the template used for adventure-game. Append the following entry to forest → contents → completeAcquireActions. Keep the coin deposit and mission counter entries.

- action: Gs2Inventory:AcquireSimpleItemsByUserId
  request: |-
    {"namespaceName":"adventure-inventory","inventoryName":"Inventory","userId":"#{userId}","acquireCounts":[{"itemName":"potion","count":1}]}

Open Deploy → adventure-game → Update stack, select the edited file under Template data, and update. Wait for Update complete.

3. Add quantity, use, and purchase controls in Unity

Stop Play Mode. Adding the following file changes the right panel to show potion quantity and action buttons. Keep the adventure panel on the left.

Create an empty GameObject named Extensions. Keep the login and mission reward buttons on the adventure screen.

Place AdventureItems.cs in Assets/AdventureExtensions, then add Adventure Items to Extensions. It uses the signed-in Gs2ClientHolder and Gs2GameSessionHolder.

Uncheck Adventure Daily on Extensions.

Place AdventureItems.cs in Assets/AdventureExtensions, then add Adventure Items to Extensions. Assign the scene’s AdventureLogin to Login.

Widen the Game view so the adventure panel on the left and potion panel on the right are both visible. Enter Play Mode, log in, and check that the potion quantity appears.

Subscribe to SimpleInventory("Inventory").SimpleItem("potion") to display the quantity. The same callback receives the initial value and subsequent changes.

var item = Client.Inventory.Namespace(InventoryNamespace).Me(Session)
    .SimpleInventory("Inventory").SimpleItem("potion");
var id = await item.SubscribeWithInitialCallAsync(value => potion = value);

Start and complete Forest exploration on the left. If the potion count on the right increases by one and coins increase on the left, the new reward is working. After deployment, reward changes can take up to five minutes to take effect. If only coins increase, wait a little, then start and complete another forest quest. Continue checking coins and stamina in the left panel.

4. Use a potion

With stamina at 15 or below, click Use 1 potion to restore 5 stamina. The potion count decreases by one and stamina increases by five. Any natural recovery during the operation is added too.

The use button calls the following exchange through RunWrite(id => Exchange("use_potion", id)). requestId is the key used to retry the same operation after a connection error.

private async UniTask Exchange(string rate, string requestId)
{
    var transaction = await Client.Exchange.Namespace("adventure-item-actions").Me(Session).Exchange()
        .ExchangeAsync(rate, 1, speculativeExecute: false, duplicationAvoider: requestId);
    if (transaction != null) await transaction.WaitAsync(all: true);
}

The template uses Gs2Inventory:ConsumeSimpleItemsByUserId to consume the item and Gs2Stamina:RecoverStaminaByUserId to grant stamina. Also try the use button with zero potions. An insufficient-items error appears and stamina does not increase.

5. Buy a potion with coins

With at least 20 coins, click Buy 1 potion for 20 coins. Coins decrease by 20 and potions increase by one.

The purchase button uses the same exchange API with the exchange name changed to buy_potion.

RunWrite(id => Exchange("buy_potion", id)).Forget();

The purchase consumes coins with Gs2Money2:WithdrawByUserId, using slot = 0, withdrawCount = 20, and paidOnly = false. It grants one potion with Gs2Inventory:AcquireSimpleItemsByUserId.

Stop and restart Play Mode, then log in as the same player. Check that the purchased potion is still there before moving on to characters.

Next: Collect and train characters