Kongregate API

I looked around the forums and couldn’t find much information on integrating Kongregate API into an HTML5 build.

Is this possible to do? I looked at the following thread: Javascript HTML5 Extension

Can the Kongregate API be integrated via an extension? They have a Javascript API on their site here: https://docs.kongregate.com/docs/javascript-api

Yes, this looks absolutely possible to do! I’m currently in the process of creating a wrapper from another JavaScript API to a Defold Lua API via a native extension and it’s a pretty straight forward process, although fairly time consuming. My recommendation is to narrow down the bare minimum of their API that you need to support and start from there.

5 Likes

Great! I will look into it. Thanks :slight_smile:

1 Like

Yup, it’s quite easy. You don’t really need a native extension, at least not for the basic stuff, just a tiny bit of javascript to set it up and then use html5.run() to call the Kongregate functions. I set up some of the high score and username stuff a while ago, but unfortunately I scrapped it, so I don’t have much to share.

You can follow the Kongregate documentation that you linked. You just need to add the Kongregate API in your index.html file:

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

Then load it with kongregateAPI.loadAPI, like in the Kong docs. I did this with my own little Javascript file, but you could just add it to the script block in your index.html, or maybe even do it from Lua with html5.run(). Then I believe you can just do things like:

html5.run("kongregate.stats.submit('Score', 1000);")

From any of your Lua scripts.


I am curious to see how Kongregate works for you. I wanted to put my Asteroids game on Kongregate, but when I uploaded a test build, I had HUGE performance issues. The web build I put on Itch runs fine, but the same build on Kongregate ran very slow (inconsistent, and often well under 30 fps). I checked the profiler, tried to optimize my code, tried uploading an empty template project—still slow. I never figured it out. :frowning: But . . . it’s quite possible that I was doing something wrong.

10 Likes

An update with some more specifics, since I am messing around with Kongregate games again.

Now I am using JSToDef, which is not really necessary for what I’m doing, but it makes life easy. :slight_smile: So the first step is to add that as a dependency in your game.project file.

Next I added this little snippet to my index.html:

<script src='https://cdn1.kongregate.com/javascripts/kongregate_api.js'></script>
<script id='kongwrapper' type='text/javascript'>
	var isKongLoaded = false;

	function loadKongregate() {
		kongregateAPI.loadAPI(
			function() {
				kongregate = kongregateAPI.getAPI();
				isKongLoaded = true;
				console.log("Kongregate API Loaded");
				JsToDef.send("kongIsLoaded")
			}
		);
	}

	function kongIsLoaded() {
		return isKongLoaded;
	}
</script>

(The second function I haven’t actually used yet, but I thought I might want it in the future.)

I put it near the end, but definitely before this:

<script id='engine-start' type='text/javascript'>
	load_engine();
</script>

Then, here is my little test script that loads the Kongregate API, listens for the callback when it is done loading, and gets the current Kongregate username and userID (and sets some labels with them).


html5 = html5 or { run = function()  return  end }

local function getKongUser()
	local userName = html5.run("kongregate.services.getUsername()")
	local userID = html5.run("kongregate.services.getUserId()")
	return userName, userID
end

local function jsListener(self, message_id, message)
	if message_id == "kongIsLoaded" then
		local userName, userID = getKongUser()
		label.set_text("#username", "Hello, " .. tostring(userName) .. ".")
		label.set_text("#userid", tostring(userID))
	end
end

function init(self)
	if jstodef then
		jstodef.add_listener(jsListener)
		html5.run("loadKongregate()")
	end
end

Hopefully that’s enough to get someone started. The rest is basically just looking at the Kongregate documentation and calling their functions with html5.run().


Regarding the performance issues I had before: they seem to have disappeared. Maybe because of updates to Defold or Kongregate or because I was doing something stupid before, I don’t know. I haven’t done any fancy graphics or heavy stuff with my latest prototype, but so far it’s running a solid 60 fps on Kongregate. No problems.

7 Likes