Hello,
I'm using Shared Group Data to store int arrays that change frequently. (I'm aware that SGD should be restricted to small groups of players, but as a temporary solution we use it for all our players, and we intend to find a better solution if/when our title scales up.)
Whenever a player completes an activity and gets a score in our game, it gets added to a list in SGD via a CloudScript function, reproduced here:
var MAX_SCORES = 200; handlers.submitScore = function(args) // Adds a new score to the specified activity { var environment = args.environment; // string var activityId = args.activityId; // string var score = args.score; // int: the new score var sharedGroupId = "Scores_" + environment; var result = null; var scores = null; try { result = server.GetSharedGroupData({ SharedGroupId: sharedGroupId, Keys: [ activityId ] }); } catch (e) { log.error("GetSharedGroupData: Error: " + e.message); return; } if (result == null) { log.info("GetSharedGroupData: No result"); scores = [score]; } else if (!result.Data.hasOwnProperty(activityId)) { log.info("GetSharedGroupData: Activity '" + activityId + "' not found"); scores = [score]; } else { var scoresAsJson = result.Data[activityId].Value; scores = JSON.parse(scoresAsJson); scores.push(score); scores = scores.slice(-MAX_SCORES); // only keep the last MAX_SCORES values } var scoresAsJson = JSON.stringify(scores); log.info("scores: " + scoresAsJson + " (" + scores.length + " elements)"); var data = {}; data[activityId] = scoresAsJson; server.UpdateSharedGroupData({ SharedGroupId: sharedGroupId, Data: data }); }
There seems to be a recurring problem where sometimes a comma is missing between two values in these lists, eg it contains '[40,46,17,4533,99]' instead of '[40,46,17,45,33,99]' (notice the comma between 45 and 33). I suspect the problem is with JSON.parse()/JSON/stringify(), perhaps because the function is called too frequently, but I haven't been able to confirm.
Has anyone else encountered this problem? Thank you.