Guest User

GameSparks Custom Data Import Script

a guest
Oct 31st, 2019
214
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var API = Spark.getGameDataService();
  2.  
  3. //Config
  4. var collectionName = "mycustomCollectionName";
  5.  
  6. function handleData(collectionJson)
  7. {
  8. var collectionArray = JSON.parse(collectionJson);
  9. collectionArray.forEach(function(item) {
  10. handleItem(item);
  11. });
  12. }
  13.  
  14. function handleItem(item)
  15. {
  16. createItem(item.id, item.data);
  17. }
  18.  
  19. function createItem(playerId, dataObject) {
  20. var entry = API.createItem(collectionName, playerId);
  21. var data = entry.getData();
  22.  
  23. Object.keys(dataObject).forEach(function(key) {
  24. var val = data[key];
  25. data.key = val;
  26. })
  27.  
  28. saveObject(entry);
  29. }
  30.  
  31. function saveObject(entry) {
  32. var status = entry.persistor().persist().error();
  33. if(status){
  34. Spark.setScriptError("ERROR", status);
  35. Spark.exit();
  36. }
  37. }
  38.  
  39.  
  40. var myMinifiedJson = '[{"data":{"mycustomdatafield1":"mycustomdatafield1value","mycustomdatafield2":"mycustomdatafield2value"},"id":"id1234","ttl":0,"version":1}]';
  41. handleData(myMinifiedJson);
RAW Paste Data