Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Client > Cloud code > Game Server

Discussion in 'Cloud Code' started by FabrizioRagaglia, Apr 5, 2024.

  1. FabrizioRagaglia


    Apr 19, 2021
    Hello, I am working on a multiplayer project with mirror networking, I am integrating the store with the ECONOMY service.
    At the moment I managed to get the client to ask the cloud code for the bundle he wants to buy, but I need the reward not to be sent to my client but to be listened by the server to process the items and deliver them to the player.
    Is this possible?

    currently my cloud code is developed in JS.

    Cloud code JS

    Code (JavaScript):
    1. const { PurchasesApi } = require("@unity-services/economy-2.4");
    2. module.exports = async ({ params, context, logger }) => {
    3.   const { projectId, accessToken } = context;
    4.   const { playerId, bundleId } = params;
    5.   //PurchaseApi
    6.   const purchaseApi = new PurchasesApi(context);
    7.   var playerPurchaseVirtualRequest = {id: bundleId};
    8.   var productPurchase = {projectId, playerPurchaseVirtualRequest, playerId};
    9.   const purchaseResult = await purchaseApi.makeVirtualPurchase(productPurchase);
    10.   return {
    11.     playerId: playerId,
    12.     purchaseResult: JSON.stringify(
    13.     //configItem : itemSelected
    14.   };
    15. };
  2. samg-unity


    Unity Technologies

    Mar 23, 2021
    Hi @FabrizioRagaglia,

    > I need the reward not to be sent to my client but to be listened by the server to process the items and deliver them to the player.

    If I understand the question correctly, currently the client makes a Cloud Code request to a function that allows the virtual purchase of the given bundle on behalf of another player and what you would like to do is notify the recipient player that they have been rewarded the bundle so that their client is able to sync the changes.

    If that is the case, I believe your current options are limited as unfortunately JS does not offer the same feature set as C# modules so all I can suggest is checking out the push client to see if that would fit your use case, but that would mean that you'd need to migrate over to C#.

    Otherwise, you could have the script make a request to a third party API (e.g. one that can handle push messages/ notifications), or the only other thing I can think to try (but would not be very efficient) is have the other player client periodically make a request to a service that could supply some information e.g. Cloud Save which could then be updated by your script.
    GabKBelmonte likes this.