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?
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 Server-side support for managing player economies or alternatively you would be able to manage your virtual purchases by making requests to the Economy Admin Rest API Unity Services Web API docs 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
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.
module.exports = async ({ params, logger }) => {
const { DataApi } = require("@unity-services/cloud-save-1.4");
const shopConfig = [{
"title": "Sell Material",
"type": "material",
"items": [{
"id": "iron",
"name": "Iron",
"price": 0 // Placeholder for the generated price
}, {
"id": "gold",
"name": "Gold",
"price": 0 // Placeholder for the generated price
}]
}, {
"title": "Buy Fuel",
"type": "fuel",
"items": [{
"id": "petrol",
"name": "Petrol",
"price": 0 // Placeholder for the generated price
}]
}, {
"title": "Drills",
"type": "drill",
"items": [{
"id": "PRECISION_DRILL",
"price": {
"buy": 0, // Placeholder for the generated buy price
"sell": 0 // Placeholder for the generated sell price
}
}]
}];
// Function to generate a random price
function generateRandomPrice() {
return Math.floor(Math.random() * 100) + 1; // Generates a random price between 1 and 100
}
// Function to update the shop configuration with random prices
function updateShopConfig() {
shopConfig.forEach(category => {
category.items.forEach(item => {
if (item.price.hasOwnProperty('buy')) {
item.price.buy = generateRandomPrice();
item.price.sell = generateRandomPrice(); // Generate sell price for drills
} else {
item.price = generateRandomPrice();
}
});
});
return shopConfig;
}
async function setGameData(updatedConfig) {
// Create an instance of DataApi
const dataApi = new DataApi();
const setItemBody = {
key: "shop",
value: JSON.stringify(updatedConfig)
};
try {
const response = await dataApi.setCustomItem("2e791a75-897b-40b4-afd0-99dadc97e12f", "shop", setItemBody);
logger.info('Game data updated successfully:', response.data);
} catch (error) {
logger.error('Error updating game data:', error);
}
}
async function main() {
updatedConfig = updateShopConfig();
logger.info(updatedConfig);
await setGameData(updatedConfig);
}
// Call the main function to execute the script
await main();
};
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
}
]
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 Integrate with other Unity services which should clarify what you’ll need.
Hope that helps solves your issue!
Thanks for the reply, but due to the large world files I’m taking the game offline
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!