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 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 import { button, text } from 'unityengine ui'; import { zepetoscriptbehaviour } from 'zepeto script' import { localplayer, zepetoplayers } from 'zepeto character controller'; import { room, roomdata } from 'zepeto multiplay'; import { zepetoworldmultiplay } from 'zepeto world'; export default class currencymanagersample extends zepetoscriptbehaviour { public currencyid string; public increasequantity number; public decreasequantity number; public increasecurrencybtn button; public decreasecurrencybtn button; public balancetext text; public multiplay zepetoworldmultiplay; private localplayer localplayer; start() { zepetoplayers instance onaddedlocalplayer addlistener(() => { this localplayer = zepetoplayers instance localplayer; this loadcurrencybalance(this currencyid); }); // handles room joined events for currency ui updates this multiplay roomjoined += (room room) => { room addmessagehandler("synccurrencyinfo", (message currencymessage) => { const currentcurrencyid = message currencyid; const currentquantity = message quantity; this balanceuiupdate(currentcurrencyid, currentquantity); }); }; // button event listener for increasing currency request credit this increasecurrencybtn onclick addlistener(() => { const data = new roomdata(); data add("currencyid", this currencyid); data add("quantity", this increasequantity); this multiplay room? send("creditcurrency", data getobject()); }) // button event listener for decreasing currency request debit this decreasecurrencybtn onclick addlistener(() => { const data = new roomdata(); data add("currencyid", this currencyid); data add("quantity", this decreasequantity); this multiplay room? send("debitcurrency", data getobject()); }) } // requests the current amount of specified currency private loadcurrencybalance(currentcurrencyid string) { this multiplay room? send("loadbalance", currentcurrencyid); } // updates the ui to reflect the current quantity of the currency private balanceuiupdate(currentcurrencyid string, currentquantity number) { this balancetext text = currentquantity tostring(); } } // interface for currency sync messages interface currencymessage { currencyid string, quantity number, } 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 this multiplay room? send("loadbalance", currentcurrencyid); to facilitate message exchange, the currencymessage interface is defined interface currencymessage { currencyid string, quantity number, } 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 this multiplay room? send("creditcurrency", data getobject()); this multiplay room? send("debitcurrency", data getobject()); 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 product api usage examples docid\ dkjpzfglbxm8cpau5ehlz this multiplay roomjoined += (room room) => { room addmessagehandler("synccurrencyinfo", (message currencymessage) => { const currentcurrencyid = message currencyid; const currentquantity = message quantity; this updatebalanceui(currentcurrencyid, currentquantity); }); }; 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 import { button, text } from 'unityengine ui'; import { zepetoscriptbehaviour } from 'zepeto script' import { localplayer, zepetoplayers } from 'zepeto character controller'; import { room, roomdata } from 'zepeto multiplay'; import { zepetoworldmultiplay } from 'zepeto world'; import { inventoryrecord, inventoryservice } from 'zepeto inventory'; import { waituntil } from 'unityengine'; export default class productmanagersample extends zepetoscriptbehaviour { public productid string; public productaddquantity number; public acquireitembtn button; public useitembtn button; public itemcounttext text; public multiplay zepetoworldmultiplay; private localplayer localplayer; start() { zepetoplayers instance onaddedlocalplayer addlistener(() => { this localplayer = zepetoplayers instance localplayer; this startcoroutine(this refreshproductui()); }); // handles room joined events for product ui updates this multiplay roomjoined += (room room) => { room addmessagehandler("syncproductinfo", (message productmessage) => { this startcoroutine(this refreshproductui()); }); }; // button event listener for the acquire item add product this acquireitembtn onclick addlistener(() => { const data = new roomdata(); data add("productid", this productid); data add("quantity", this productaddquantity); this multiplay room? send("addproduct", data getobject()); }) // button event listener for the use item use product this useitembtn onclick addlistener(() => { const data = new roomdata(); data add("productid", this productid); data add("quantity", 1); this multiplay room? send("useproduct", data getobject()); }) } private refreshproductui() { const request = inventoryservice getasync(this productid); yield new waituntil(() => request keepwaiting == false); // if the request is successful, updates the product ui if (request responsedata issuccess) { this updateitemcounttext(request responsedata product); } else { console log("failed to load product item "); } } private updateitemcounttext(item inventoryrecord) { if (item != null) { this itemcounttext text = item quantity tostring(); } else { this itemcounttext text = "0"; } } } // interface for product messages interface productmessage { productid string, productaction productaction, } // enum for defining product action types export enum productaction { use, add, } product client script description when a character loads, use the inventoryservice from zepeto inventory https //developer zepeto me/docs/product/namespaces/zepeto inventory/ to load the initial inventory of products then update the ui private refreshproductui() { const request = inventoryservice getasync(this productid); yield new waituntil(() => request keepwaiting == false); if (request responsedata issuccess) { this updateitemcounttext(request responsedata product); } else { console log("failed to load product item "); } } to send and receive messages, define the productmessage interface define a productaction enum for the example implementation interface productmessage { productid string, productaction productaction, } export enum productaction { use, add, } 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 this multiplay room? send("addproduct", data getobject()); this multiplay room? send("useproduct", data getobject()); after processing the product changes, the server sends the final inventory information to the client, which updates the ui accordingly product api usage examples docid\ dkjpzfglbxm8cpau5ehlz this multiplay roomjoined += (room room) => { room addmessagehandler("syncproductinfo", (message productmessage) => { this startcoroutine(this refreshproductui()); }); }; generally, world products should cost currency to purchase in this case, you can easily sell them with a product purchase button by referring to the guide monetize your world! setting up products and currencies docid\ rekja6ud7qz6c6ltst1en server script below is the complete server code that manages the world currency and products, and how to implement it full server script import { sandbox, sandboxoptions, sandboxplayer } from "zepeto multiplay"; import { loadcurrency } from "zepeto multiplay currency"; import { loadinventory } from "zepeto multiplay inventory"; export default class extends sandbox { async oncreate(options sandboxoptions) { // currency //request loading currency this onmessage("loadbalance", (client, message string) => { this loadbalance(client, message); }); //request adding credit this onmessage("creditcurrency", (client, message currencymessage ) => { const currencyid = message currencyid; const quantity = message quantity; this addcredit(client, currencyid, quantity); }); //request debiting credit this onmessage("debitcurrency", (client, message currencymessage ) => { const currencyid = message currencyid; const quantity = message quantity; this ondebit(client, currencyid, quantity); }); // product //request adding item this onmessage("addproduct", (client, message any) => { const productid = message productid; const quantity = message quantity; this addproduct(client, productid, quantity); }); //request using item this onmessage("useproduct", (client, message any) => { const productid = message productid; const quantity = message quantity; this useproduct(client, productid, quantity); }); } async addcredit(client sandboxplayer, currencyid string, quantity number) { try { const currency = await loadcurrency(client userid); await currency credit(currencyid, quantity); this loadbalance(client, currencyid); } catch (e) { console error(`addcredit error ${e}`); } } async ondebit(client sandboxplayer, currencyid string, quantity number) { try { const currency = await loadcurrency(client userid); if (await currency debit(currencyid, quantity) === true) { this loadbalance(client, currencyid); } else { console error("debitcredit error currency not enough"); } } catch (e) { console error(`debitcredit error ${e}`); } } async loadbalance(client sandboxplayer, currencyid string) { try { const currency = await loadcurrency(client userid); let balancesobject = await currency getbalances(); let balancesmap map\<string, number> = new map(object entries(balancesobject)); const specificcurrencyid = currencyid; const specificbalance = balancesmap get(specificcurrencyid) ?? 0; const currencysync currencymessage = { currencyid specificcurrencyid, quantity specificbalance } client send("synccurrencyinfo", currencysync); } catch (e) { console error(`loadbalance error ${e}`); } } async addproduct(client sandboxplayer, productid string, quantity number) { try { const inventory = await loadinventory(client userid); await inventory add(productid, quantity); const productmessage productmessage = { productid productid, productaction productaction add } client send("syncproductinfo", productmessage); } catch (e) { console error(`addproduct error ${e}`); } } async useproduct(client sandboxplayer, productid string, quantity number) { try { const inventory = await loadinventory(client userid); await inventory use(productid, quantity); const productmessage productmessage = { productid productid, productaction productaction use } client send("syncproductinfo", productmessage); } catch (e) { console error(`useproduct error ${e}`); } } onjoin(client sandboxplayer) { // onjoin logic here } ontick(deltatime number) void { // tick logic here } onleave(client sandboxplayer, consented? boolean) { // handle leave logic here } } // interface for currency messages interface currencymessage { currencyid string, quantity number, } // interface for product messages interface productmessage { productid string, productaction productaction, } // enum for defining product action types export enum productaction { use, add, } server script description manage the world currency via zepeto multiplay currency https //developer zepeto me/docs/multiplay server/namespaces/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 currency credit(currencyid, quantity); currency debit(currencyid, quantity); currency getbalances(); manage world products via zepeto multiplay inventory https //developer zepeto me/docs/multiplay server/namespaces/zepeto multiplay inventory use inventory add() and inventory use() to increase or decrease the balance of the desired currency inventory add(productid, quantity); inventory use(productid, quantity); 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