Sign In Register

How can we help you today?

Start a new topic
Answered

Crafting and Achievements

In this article linked you mention that Achievements can be used to implement 'Basic Crafting' but do not give an example.


https://docs.gamesparks.net/developer-portal/achievements


I understand how an achievement system can be used for detecting XP thresholds for determining when a player might qualify for a specific levelup reward. And I understand how you might be able to detect if a player has 3 apples + 1 Cinnamon + 1 Pie Crust to detect if they have 'achieved' the ingredients necessary to craft and apple pie. But aside from awarding an apple pie for having the ingredients, how do you deduct the ingredients for the pie recipe upon cashing in on that achievement? Also, are achievement rewards automatically given or does the user have to 'accept' the reward?




Best Answer

You could deduct the ingredients (assuming they are stored as virtual goods) for the recipe in user message for AchievementEarnedMessage. Here's how you could do it:


1. Create a meta collection called "recipes" to store the recipes in the following format:

{
	"id": "apple_pie",
	"ingredients": [
		{
			"id": "cinnamon",
			"amount": 1
		},
		{
			"id": "pie_crust",
			"amount": 1
		},
		{
			"id": "apple",
			"amount": 3
		}
    ]
}

 

2. Create achievement with short code "apple_pie", set its virtual good award to Apple pie, make it repeatable
3. Create an event to allow players craft the desired item (i.e. earn the achievement):

var itemId = Spark.getData().item_id; // provided by client (e.g. "apple_pie")
// Look for the item's recipe
var res = Spark.metaCollection( "recipes" ).find( { "id": itemId } );

if( res.hasNext() ) {
    res.next();
    var recipe = res.curr();
    var ingredients = recipe.ingredients;
    // Check if the player has all the ingredients (virtual goods)
    var player = Spark.getPlayer();
    var hasAllIngredients = true;
    for( var i = 0; i < ingredients.length; i++ ) {
    	if( player.hasVGood( ingredients[i].id ) < ingredients[i].amount ) {
    		// Player does not have enough ingredients
    		hasAllIngredients = false;
    		break;
    	}
    }
    if( hasAllIngredients ) {
    	// Award the achievement
    	player.addAchievement( "apple_pie" );
    }
}

 

4. Once the achievement is earned, deduct the ingredients in user message CloudCode for AchievementEarnedMessage:

// Short code of the achievement - assumes that it matches the id for the recipe ("apple_pie")
var shortCode = Spark.getData().achievementShortCode;

// Now, similar to the craft event, get the recipe from the collection and deduct the virtual goods (ingredients) using Spark.getPlayer().useVGood(...)

 

The rewards for the achievement are given automatically.

Marcel

2 people have this question
1 Comment

Answer

You could deduct the ingredients (assuming they are stored as virtual goods) for the recipe in user message for AchievementEarnedMessage. Here's how you could do it:


1. Create a meta collection called "recipes" to store the recipes in the following format:

{
	"id": "apple_pie",
	"ingredients": [
		{
			"id": "cinnamon",
			"amount": 1
		},
		{
			"id": "pie_crust",
			"amount": 1
		},
		{
			"id": "apple",
			"amount": 3
		}
    ]
}

 

2. Create achievement with short code "apple_pie", set its virtual good award to Apple pie, make it repeatable
3. Create an event to allow players craft the desired item (i.e. earn the achievement):

var itemId = Spark.getData().item_id; // provided by client (e.g. "apple_pie")
// Look for the item's recipe
var res = Spark.metaCollection( "recipes" ).find( { "id": itemId } );

if( res.hasNext() ) {
    res.next();
    var recipe = res.curr();
    var ingredients = recipe.ingredients;
    // Check if the player has all the ingredients (virtual goods)
    var player = Spark.getPlayer();
    var hasAllIngredients = true;
    for( var i = 0; i < ingredients.length; i++ ) {
    	if( player.hasVGood( ingredients[i].id ) < ingredients[i].amount ) {
    		// Player does not have enough ingredients
    		hasAllIngredients = false;
    		break;
    	}
    }
    if( hasAllIngredients ) {
    	// Award the achievement
    	player.addAchievement( "apple_pie" );
    }
}

 

4. Once the achievement is earned, deduct the ingredients in user message CloudCode for AchievementEarnedMessage:

// Short code of the achievement - assumes that it matches the id for the recipe ("apple_pie")
var shortCode = Spark.getData().achievementShortCode;

// Now, similar to the craft event, get the recipe from the collection and deduct the virtual goods (ingredients) using Spark.getPlayer().useVGood(...)

 

The rewards for the achievement are given automatically.

Marcel
Login to post a comment