CREATE YOUR WORLD
Economy

Product API Usage Examples

18min

The following are examples of using the Product API to manage World Currency and World Products.

This example allows you to get a basic understanding of the Product API and implement sending and receiving messages between the server and the client, so that you can easily test the granting and deducting world currencies and products.

This example includes descriptions of both the World Currency and World Product client scripts, followed by the integrated server script that handles all server-side processing, and includes the following functions:

  • Granting World Currency
  • Deducting World Currency
  • Granting World Products without Currency Deduction
  • Deducting World Products
Document image




Granting & Deducting World Currency

Below is the complete client code for managing world currency granting and deduction, and how to implement it.

Currency Client Script

CurrencyManagerSample




Currency Client Script Description

  • When a character is loaded, a Room Message is sent to the server to load the existing balance of currency. The UI is then updated based on the balance information received from the server.
TypeScript

  • To facilitate message exchange, the CurrencyMessage interface is defined.
TypeScript

  • When a user increases or decreases their currency, they send a request to the server as a Room Message. It includes data about which currency to increase or decrease and by how much.
TypeScript

  • The server then processes the increase or decrease in currency and sends the final balance information to the client. The client receives this information and updates the UI.
TypeScript




Granting & Deducting World Products

Below is the complete client code for managing world product granting and deduction, and how to implement it.

Product Client Script

ProductManagerSample




Product Client Script Description

  • When a character loads, use the InventoryService from ZEPETO.Inventory to load the initial inventory of products. Then update the UI.
TypeScript

  • To send and receive messages, define the ProductMessage interface. Define a ProductAction Enum for the example implementation.
TypeScript

  • When you increase or decrease a product, you send that data to the server as a Room Message. It includes data about which product to increase or decrease and by how much.
TypeScript

  • After processing the product changes, the server sends the final inventory information to the client, which updates the UI accordingly.
TypeScript




Server Script

Below is the complete server code that manages the world currency and products, and how to implement it.

Full Server Script

TypeScript




Server Script Description

  • Manage the world currency via ZEPETO.Multiplay.Currency.
  • Use currency.credit() and currency.debit() to increase or decrease the balance of the desired currency. Afterward, call currency.getBalances() to get the current balance for each currency.
TypeScript

  • Manage world products via ZEPETO.Multiplay.Inventory.
  • Use inventory.add() and inventory.use() to increase or decrease the balance of the desired currency.
TypeScript

  • Handles requests from clients for increasing or decreasing currencies and products.
  • Use loadBalance() to get the currency information registered in ZEPETO Studio.
  • If there are multiple currencies, you can condition on a specific currency ID value to get the balance of only that currency.
  • Pass the final balance value to the client as a Room Message.