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. 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.