Kongregate Developers

Saving content

You can use the save function to submit shared content on the Kongregate back-end.

save(type:String,content:String,callback:Function,
thumb:DisplayObject=null,label:String=null):void
  • type:String — Type of content the user wishes to save, 12 characters max
  • content:String — Value of content to be saved. We strongly recommend keeping these values under 100K since the game will hang until the content is sent, which can lead to a poor user experience if the content is too large.
  • callback:Function — Function to call when save has finished
  • thumb:DisplayObject — Optional but highly recommended! Send us a DisplayObject that we will snapshotted and used as a thumbnail for the content. If this is not provided we will take a picture of the entire stage and use that as the thumbnail.
  • label:String — Optional, label for sub-classing the shared content.

The callback parameter should be a function that accepts a single Object as a parameter, which will have the following properties:

  • success:Boolean - True if successful. If false, no other attributes will exist
  • id:Number - Database ID of the shared content
  • name:String - Name of the shared content
  • permalink:String - Link to the shared content
  • content:String - The content itself
  • label:String - The label for the content

Example: Save some shared content to as a "Contraption" with the contents "x1y3z10", calling back onContraptionSaved using myContraptionEditor for the thumbnail and with the label "Level 3 Solution"

kongregate.sharedContent.save('Contraption', 'x1y3z10', onSaved, myContraptionEditor, 
  'Level 3 Solution');

function onSaved(params:Object) {
  if (params.success) {
    // The shared content was saved successfully.
    trace("Content saved, id:" + params.id + ", name:" + params.name);
  } else {
    // The shared content was not saved.
    // The most likely cause of this is that the User dismissed the save dialog  
  }
}

Saving shared content on sites other than Kongregate

You may submit shared content created in versions of your game hosted on sites other than Kongregate. The API simply consists of a set of URL parameters you pass along with a GET request to your game's URL on Kongregate.

The relevent query parameters are type, content, and optionally, label. Note that this method is more limited in terms of the size of the content due to browser limitations with passing through a URL - they need to be limited to under 2,000 characters after encoding in order to ensure that it will work.

Additionally, a trailing &z must be appended to the URL to indicate the content was not truncated.

Example

This snippet is a simple function that accepts the relevant arguments and forwards them to Kongregate:

var saveRemote:Function=function(kongregateGameUrl:String,type:String,
  content:String,label:String=null):void{
  var toQueryString:Function = function(params:Object):String {
    var q:Array = [];
    for (var p:String in params) q.push(encodeURIComponent(p) + '=' + 
      encodeURIComponent(params[p]));
    return q.join('&');
  }
  
  var loc:String = kongregateGameUrl + "?" + toQueryString({
    'type': contentType,
    'content': content,
    'label': label
  }) + '&z';

  // This &z sigil indicates that no content was truncated from the URL by a nasty
  // browser and is required for your level to be saved when a user visits the game
  
  // Make sure your URL isn't going to be too long to send through a browser
  if (loc.length < 2000)
    navigateToURL(new URLRequest(loc), '_blank');
  
  // else...
  // You will need to provide some error handling in cases where the 
  // content is too large
}

Comments