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 Can I create or modify virtual purchases?

Discussion in 'Cloud Code' started by Vitmine, May 16, 2024.

  1. Vitmine

    Vitmine

    Joined:
    Mar 24, 2021
    Posts:
    6
    I need to generate a shop every day from the cloud code. Can I create, delete, and modify virtual purchases or do I need to edit the Cloud Config and in game decrement coins and give inventory items?
     
  2. samg-unity

    samg-unity

    Unity Technologies

    Joined:
    Mar 23, 2021
    Posts:
    44
    Hi @Vitmine

    > Can I create, delete, and modify virtual purchases

    We don't currently offer an Economy Admin SDK as part of Cloud Code but this post demonstrates how you could build your own for csharp modules https://forum.unity.com/threads/ser...naging-player-economies.1545188/#post-9644168 or alternatively you would be able to manage your virtual purchases by making requests to the Economy Admin Rest API https://services.docs.unity.com/economy-admin/v2 using a rest api client (see examples for csharp or javascript)

    > do I need to edit the Cloud Config and in game decrement coins and give inventory items?

    I don't know if the above helps answer your question, I believe Cloud Code is capable of being scheduled to change your virtual purchase items but if I've misunderstood then I would need some more insight into how you plan on managing the daily shop and items and how players will be interacting with it
     
  3. Vitmine

    Vitmine

    Joined:
    Mar 24, 2021
    Posts:
    6
    I have a script that generates random prices and stores them in the game data, and in the game, I fetch that data and if the player buys something, it decrements the coins and gives the item. But I'm getting an error with saving to the game data.
    Code (JavaScript):
    1. module.exports = async ({ params, logger }) => {
    2.  
    3.   const { DataApi } = require("@unity-services/cloud-save-1.4");
    4.  
    5.   const shopConfig = [{
    6.     "title": "Sell Material",
    7.     "type": "material",
    8.     "items": [{
    9.       "id": "iron",
    10.       "name": "Iron",
    11.       "price": 0 // Placeholder for the generated price
    12.     }, {
    13.       "id": "gold",
    14.       "name": "Gold",
    15.       "price": 0 // Placeholder for the generated price
    16.     }]
    17.   }, {
    18.     "title": "Buy Fuel",
    19.     "type": "fuel",
    20.     "items": [{
    21.       "id": "petrol",
    22.       "name": "Petrol",
    23.       "price": 0 // Placeholder for the generated price
    24.     }]
    25.   }, {
    26.     "title": "Drills",
    27.     "type": "drill",
    28.     "items": [{
    29.       "id": "PRECISION_DRILL",
    30.       "price": {
    31.         "buy": 0, // Placeholder for the generated buy price
    32.         "sell": 0 // Placeholder for the generated sell price
    33.       }
    34.     }]
    35.   }];
    36.  
    37.   // Function to generate a random price
    38.   function generateRandomPrice() {
    39.     return Math.floor(Math.random() * 100) + 1; // Generates a random price between 1 and 100
    40.   }
    41.  
    42.   // Function to update the shop configuration with random prices
    43.   function updateShopConfig() {
    44.     shopConfig.forEach(category => {
    45.       category.items.forEach(item => {
    46.         if (item.price.hasOwnProperty('buy')) {
    47.           item.price.buy = generateRandomPrice();
    48.           item.price.sell = generateRandomPrice(); // Generate sell price for drills
    49.         } else {
    50.           item.price = generateRandomPrice();
    51.         }
    52.       });
    53.     });
    54.     return shopConfig;
    55.   }
    56.  
    57.   async function setGameData(updatedConfig) {
    58.     // Create an instance of DataApi
    59.     const dataApi = new DataApi();
    60.  
    61.     const setItemBody = {
    62.       key: "shop",
    63.       value: JSON.stringify(updatedConfig)
    64.     };
    65.  
    66.     try {
    67.       const response = await dataApi.setCustomItem("2e791a75-897b-40b4-afd0-99dadc97e12f", "shop", setItemBody);
    68.       logger.info('Game data updated successfully:', response.data);
    69.     } catch (error) {
    70.       logger.error('Error updating game data:', error);
    71.     }
    72.   }
    73.  
    74.   async function main() {
    75.     updatedConfig = updateShopConfig();
    76.     logger.info(updatedConfig);
    77.     await setGameData(updatedConfig);
    78.   }
    79.  
    80.   // Call the main function to execute the script
    81.   await main();
    82. };
    logs:

    [
    {
    "attributes": {
    "code.function": "main",
    "faas.name": "shop",
    "log.record.uid": "b74c873e-1656-41cf-b51f-2914e5ee8237",
    "unity.environmentId": "54db0aa4-9d1c-4872-913d-68be038850b5",
    "unity.projectId": "2e791a75-897b-40b4-afd0-99dadc97e12f",
    "unity.userId": "WmNyPdFUaSdnsWNyYv4UA5jwDpfO"
    },
    "body": "[\n {\n title: 'Sell Material',\n type: 'material',\n items: [\n { id: 'iron', name: 'Iron', price: 33 },\n { id: 'gold', name: 'Gold', price: 43 },\n '0': { id: 'iron', name: 'Iron', price: 33 },\n '1': { id: 'gold', name: 'Gold', price: 43 }\n ]\n },\n {\n title: 'Buy Fuel',\n type: 'fuel',\n items: [\n { id: 'petrol', name: 'Petrol', price: 18 },\n '0': { id: 'petrol', name: 'Petrol', price: 18 }\n ]\n },\n {\n title: 'Drills',\n type: 'drill',\n items: [\n { id: 'PRECISION_DRILL', price: { buy: 69, sell: 13 } },\n '0': { id: 'PRECISION_DRILL', price: { buy: 69, sell: 13 } }\n ]\n }\n]",
    "severityNumber": 9,
    "severityText": "INFO",
    "timestamp": 1715959844230
    },
    {
    "attributes": {
    "code.function": "setGameData",
    "faas.name": "shop",
    "log.record.uid": "197b5428-12be-4964-84e1-86010dddb143",
    "unity.environmentId": "54db0aa4-9d1c-4872-913d-68be038850b5",
    "unity.projectId": "2e791a75-897b-40b4-afd0-99dadc97e12f",
    "unity.userId": "WmNyPdFUaSdnsWNyYv4UA5jwDpfO"
    },
    "body": "Error updating game data:",
    "severityNumber": 17,
    "severityText": "ERROR",
    "timestamp": 1715959844240
    }
    ]
     
  4. samg-unity

    samg-unity

    Unity Technologies

    Joined:
    Mar 23, 2021
    Posts:
    44
    Hi @Vitmine,

    Thanks for sharing your code - I can see that the `new DataApi();` instance is not being passed the `context` argument which contains the authentication details you need to make requests to other UGS services.

    Also your error logging should log error.message rather than the error object in order to view the error output.

    We have an cloud save integration example in the docs here https://docs.unity.com/ugs/en-us/ma...es-integration#Using_the_Unity_Cloud_Save_SDK which should clarify what you'll need.

    Hope that helps solves your issue!
     
  5. Vitmine

    Vitmine

    Joined:
    Mar 24, 2021
    Posts:
    6
    Thanks for the reply, but due to the large world files I'm taking the game offline
     
  6. GabKBelmonte

    GabKBelmonte

    Unity Technologies

    Joined:
    Dec 14, 2021
    Posts:
    149
    Hi!

    For what is worth, CCD is capable of dealing with dynamic downloads, but also I'd love to hear the reason why a hybrid game is not an option.
    Cheers!