API Object
Now to create your game API object:
Pick up username and token from where you’ve saved them in the globalDict.
You have to enter your game ID and private key here. I guess someone could hack your blend and find the private key, but it’s not a big problem. You can always generate a new key if you think you’ve been hacked.
You should check if the user name and token are correct by using:
valid_user = jolt_object.authenticateUser()
This can take a second or two to process, so leave some text on screen saying “searching” or whatever. You can print a message to say that the username is valid, or you can skip straight to the game, your choice.
Don’t forget to put your game jolt object in to the global dict so you can pick it up later.
bge.logic.globalDict['jolt_object'] = jolt_object
***Scores.
Adding high scores is simple, you just need to call
score = own['score']
bge.logic.globalDict['jolt_object'].addScores(score,score)
To upload a score.
Again it can take a moment or two to upload so it’s good to do this at a time when there isn’t anything happening on screen. Instead of when your player dies, it can be best to send a request when you press space to restart, or when you exit to the main menu or whatever.
You can set the high score table manually if you have more than one by including the table_id. You can also allow guest scoring (people who didn’t log in) by using guest=True, guestname=‘guest’
In fact if you can’t get anything else to work or you don’t want the fuss of people having to set thier user name and token, you can still use your game ID and private key with a blank ID and game token to log in to game jolt and add scores for guest users.
Here’s the format you should follow:
addScores(score, sort, table_id=None, extra_data='', guest=False, guestname='')
You can fetch scores for the game too, so they can be displayed in your game. In that case use:
fetchScores(<i>limit=10</i>, <i>table_id=None</i>, <i>user_info_only=False</i>)
which will return a dictionary with two items:
success and scores.
Success tells you if it found the scores, and scores is just a list of scoring info.
if scores['success']:
scoreboard = scores['scores']
else:
scoreboard = []
I’m sure you can think of ways to display this info in a scoreboard in your game.
***Trophies.
There are also the trophies.
You need to set up the trophies in Gamejolt first, as that will give you the trophy ID. You need it to set a trophy as achieved. You can go back and edit them later, adding a trophy image and other info as you like.
Again adding trophies is as simple as making a call you your game jolt object:
Remember, making a call to the gamejolt website takes quite a bit of time. So don’t do the check often.
In my case I make the call when you start a new level, but you could make it happen when you pick up a certain object, or kill a special enemy or whatever. The best time though is when starting or leaving a scene, as the normal lag of loading a new scene hides the lag of sending a message to game jolt.
As with score you can get a list of a player’s trophies to display in game.
fetchTrophy(<i>achieved=None</i>, <i>trophy=None</i>)
***game data:
EDIT:
After testing it seems you can’t send game data with any spaces. So we have to convert the string to have a dummy character which we convert back when loading.
If you store the gamelogic dictionary as json first, then use replace on the json.dumps(data) object, you can save it just fine.
Then fetch it, replace the filler with spaces and then use json.loads(data) to get a dictionary with properly formatted items.
import json
#####################
### save
data_file = {"something":(12,"x",-12),"other":["this is
a test"]}
data_item = json.dumps(data_file)
converted = data_item.replace(" ","%20")
set_data = jolt_object.storeData("saved",converted)
######################
### load
recovered = jolt_object.fetchData("saved")["data"]
reconverted = recovered.replace("%20"," ")
loaded_data = json.loads(reconverted)
print (loaded_data)
print (loaded_data["something"][2] + 5)
This will preserve all your data. Though it’s best not to try and save classes or other complex objects. Strings, ints, floats, lists, dicts, tuples etc… should all be fine.
***Taking it to the next level:
I think this achievements system is great. It opens doors for some interesting meta games, such as a treasure hunt, where people have to play a few different games to pick up trophies to unlock a final game which can only be played if you have all the trophies. Think of the novel “Ready Player One” which sets up a easter egg hunt for the players of a MMORPG with a prize of millions of dollars. It’s a shame there’s not really that many games on game jolt that use the system.
Battery (the host of BGMC16) did a good tutorial on using phyon’s built in webbrowser module to access a webpage from within Blender. If you wanted you could use that as a starting point for writing your own achievements and scores API to add high scores toyour own website.