How to use the kongregate API with Playcanvas?

Hello Playcanvas community!

I’øve been working on my game for some time now and want to implement highscores. I have used Kongregates API with other engines and it works great. However I can’t seem to figure out how to add the

<script src='https://cdn1.kongregate.com/javascripts/kongregate_api.js'></script>

line, which I’m supposed to do.

I’ve been looking at tutorials but they all seem to be of the old scripting system so something always goes wrong, and some things seem unable to translate.

What I’m currently using is:

var KongregateApiadder = pc.createScript('kongregateApiadder');

// initialize code called once per entity
KongregateApiadder.prototype.initialize = function() {
    
    console.log(pc.AssetRegistry);
    var asset = this.app.assets.findByTag("apii");
    
    
    console.log(asset.resource);

    var div = document.createElement('kongregatediv');
    div.innerHTML = asset.resource ||'';
    document.body.appendChild(div);
    
    asset.on('load', function() 
    {  
        div.innerHTML = asset.resource;
    });
    
    this.app.assets.load(asset);

};

// update code called every frame
KongregateApiadder.prototype.update = function(dt) {
    
};

Where “apii” is a html file containing the previously mentioned line. For this code I get > asset.on is not a function

Any help with implementing the Kongregate API is much appriciated!

I’ve just created small script that loads API:

var app = pc.Application.getApplication();

var asset = new pc.Asset('kongregate_api.js', 'script', {
    file: 'kongregate_api.js',
    url: 'https://cdn1.kongregate.com/javascripts/kongregate_api.js'
});

app.assets.load(asset);

asset.once('load', function() {
    kongregateAPI.loadAPI(function() {
        window.kongregate = kongregateAPI.getAPI();
        app.fire('kongregate:load', window.kongregate);
    });
});

So in your scripts you would need to do this:

// somewhere is script initialize, or other places:
if (kongregate) {
    // kongregate API already loaded
} else {
    // not yet loaded, subscribe to it's load event
    app.once('kongregate:load', function() {
        // kongregate API loaded
    });
}

Just tested, and seems that it only will be testable when is running though Kongregate iframe, so you might try to figure out how to run launch link within kongregate for testing purposes, that will make your development process much easier.

1 Like

This is wonderful! Thank you so much.

Do you export/publish your game then upload it to kongregate to test it?

I don’t really know how kongregate provides testing environment. If they support just URL’s, then you can use launcher URL and that will not require building/uploading.