question

maria-1 avatar image
maria-1 asked

Convert to economy v2

Hello, I have started adding playfab to my exsisiting game only to realize that I have been reading the v1 economy documentation. How can I buy/sell items like this but for v2 economy? this is my ItemToBuy.cs class


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PlayFab;
using PlayFab.ClientModels;
public class ItemToBuy : MonoBehaviour { public int glimmersPrice; public string itemName; public void BuyItem() { var request = new SubtractUserVirtualCurrencyRequest { VirtualCurrency = "GL", Amount = glimmersPrice }; PlayFabClientAPI.SubtractUserVirtualCurrency(request, OnSubtractGlimmersSuccess, OnError); } void OnSubtractGlimmersSuccess(ModifyUserVirtualCurrencyResult result) { Debug.Log("Bought item! " + itemName); UserProfile.instance.GetVirtualCurrencies(); //Grant items to user here } // Other void OnError(PlayFabError error) { Debug.Log("Error: " + error.ErrorMessage); } }
In-Game Economy
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Xiao Zha avatar image
Xiao Zha answered

Since your buy items workflow is first subtract the virtual currency then grant the Items, you can call Subtract Inventory Items API to subtract the virtual currency then call Add Inventory Items to grant the items. You can refer to Quickstart - PlayFab | Microsoft Learn to know how to create the items and Currency. And for more information about Economy V2 you can refer to PlayFab Economy documentation - PlayFab | Microsoft Learn. In addition, for security reasons, we recommend that you use the Cloud Function to implement the workflow of your item purchasing instead of achieving it on client side.

2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

maria-1 avatar image maria-1 commented ·

How would I implement Cloud Function to the workflow for item purchasing?

0 Likes 0 ·
Xiao Zha avatar image Xiao Zha maria-1 commented ·

You can move the currency reduction and item grant operations into your Azure Function. For more information about Azure Function, you can refer to PlayFab CloudScript using Azure Functions - PlayFab | Microsoft Learn.

0 Likes 0 ·
kylemc@microsoft.com avatar image
kylemc@microsoft.com answered

Also, take a look at using the purchase API directly (instead of subtract/add) or using the batch API to make the sequential operations transactional.

Inventory - Purchase Inventory Items - REST API (PlayFab Economy) | Microsoft Learn
Inventory - Execute Inventory Operations - REST API (PlayFab Economy) | Microsoft Learn

4 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

drew avatar image drew commented ·

@kylemc@microsoft.com, it's not clear from the docs how you can SELL an item using Purchase Inventory Items. Is this possible?

0 Likes 0 ·
kylemc@microsoft.com avatar image kylemc@microsoft.com drew commented ·

@drew I'm not positive what you're asking. Is "you" in this question the game developer? In that case, when you (the developer) put a price on something in the catalog, players will be able to acquire it via a Purchase operation (they pay the price to get the thing).

0 Likes 0 ·
drew avatar image drew kylemc@microsoft.com commented ·

@kylemc@microsoft.com as a developer, how can I allow players to sell back an item, meaning trade that item for in game currency instead of vice versa?

0 Likes 0 ·
Show more comments
kylemc@microsoft.com avatar image
kylemc@microsoft.com answered
There are two ways to make an item purchasable.

  1. Define a price directly on the item (CatalogItem) and it can be purchased without a Store
    Catalog - Create Draft Item - REST API (PlayFab Economy) | Microsoft Learn
  2. Define a price in a Store (via CatalogItemReference) and it can be purchased at that price through that Store
    Catalog - Create Draft Item - REST API (PlayFab Economy) | Microsoft Learn
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.