GameSparks example

Hi guys!

Can anyone help me by providing an example on how to set up GameSparks in Defold? I already loaded the dependencies and everything, but there aren’t any examples on how to set up the environment to be able to use GameSpark’s API

Thank you!

Heres a small (and ugly/raw) script you could hook up to a gameobject and send a message to when you want to start up Gamesparks. It puts gs in global scope which I wouldnt recommend but for simplicity sake it is there for now. (so you could directly create a request anywhere by typing: local req = gs.getRequestBuilder().createMatchmakingRequest() )
Also this script requires you to have gamesparks api_key, api_secret and api_credentials in game.project under a section named [gamesparks]
Hope you will find it enough to get started.

EDIT: Also i would recommend you doing some kind of message_proxy where other scripts can register to certain messages coming from gamesparks and that could be handled from the MessageHandlerCallback passing the message further to the scripts needing it.

EDIT2: Removed an internal library (log) that didnt make sense for you. If you got any questions about Gamesparks + Defold just give me a shout as we are fully utilising it.

local GS = require "gamesparks.GS"
local GSRequest = require "gamesparks.GSRequest"
require "gs.gs_errors"
gs = createGS()

local function init_gs(self)
	local api_key = sys.get_config("gamesparks.api_key")
	local api_secret = sys.get_config("gamesparks.api_secret")
	local api_credential = sys.get_config("gamesparks.api_credential")
	assert(api_key and api_secret and api_credential, "Must define api_key and api_secret in game.project [gamesparks]")
	gs.setLogger(function(str) print(str) end)
	gs.setApiKey(api_key)
	gs.setApiSecret(api_secret)
	gs.setApiCredential(api_credential)
	gs.setAvailabilityCallback(function(is_available) 
		if is_available then
			print("Gamesparks ready")
		end
	end)
	gs.setAuthenticatedCallback(function(val)
		print("AuthenticatedCallback:",val)
	end)
	gs.setMessageHandlerCallback(function(message)
		print("MessageHandlerCallback:")
		pprint(message)
	end)
	gs.connect()
end

function on_message(self, message_id, message, sender)
	if message_id == hash("init_gs") then
		init_gs(self)
	end
end
6 Likes

Thanks a lot! Will try it out and I’ll let you know if I have any other questions