Jump to content

Leaderboards


Josh
 Share

Recommended Posts

I am adding Steam leaderboard functionality to help make your games for the Halloween game tournament more fun. It will let you display the top ten scores of your game among all players, with the user's name, score, and avatar displayed onscreen.

 

Here's a quick rundown of the API:

 

Get the leaderboard object. Use the name parameter to create multiple leaderboards for different parameters. Type can be 1 for numeric, 2 for seconds:

Leaderboard* Steamworks::GetLeaderboard(std::string name="Hiscore", const int type = 1)

 

This gives you everything you need to display the leaderboard entries. Entries are numbered 0 to CountEntries()-1:

int Leaderboard::CountEntries()
uint64_t Leaderboard::GetEntryUserID(const int index)
int Leaderboard::GetEntryScore(const int index)

 

Use this to set the player's hiscore. (It will automatically keep the highest score they achieve.)

bool Leaderboard::SetScore(const int score)

 

You can get the user name and avatar from the Steam ID:

std::string Steamworks::GetUserName(const uint64_t userid)
Texture* Steamworks::GetUserAvatar(const uint64_t userid)

 

This will be available later today on the beta branch and in game launcher.

  • Upvote 9

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Here it is in action:

post-1-0-41561600-1444941323_thumb.jpg

 

Add this at the top of main.lua:

import("Scripts/HiScores.lua")

 

And save this file as "HiScores.lua":

storedavatars={}

function SetScore(score)
if leaderboard==nil then leaderboard = Steamworks:GetLeaderboard() end
   if leaderboard==nil then return false end 
result=leaderboard:SetScore(score)
leaderboard:Release()
leaderboard = nil
return result
end

function ShowHiScores(context)

if leaderboard==nil then
	leaderboard = Steamworks:GetLeaderboard()
end
if leaderboard==nil then
	return false
end
local n
local x=300
local y=300
local w=600
local fh = context:GetFont():GetHeight()
local rowheight=68
local rowspacing=2
local h = (rowheight+rowspacing)*leaderboard:CountEntries()+20

x = (context:GetWidth()-w)/2
y = (context:GetHeight()-h)/2

context:SetBlendMode(Blend.Alpha)

context:SetColor(0,0,0,0.5)
context:DrawRect(x,y-20,w,20)
context:SetColor(1,1,1,1)

context:DrawText("Name",x+80,y-20+2)
context:DrawText("Score",x+300,y-20+2)	

for n=0,leaderboard:CountEntries()-1 do
	local score = leaderboard:GetEntryScore(n)
	local steamid = leaderboard:GetEntryUserID(n)
	local s = type(steamid)
	local name = Steamworks:GetUserName(steamid)
	if storedavatars[steamid]==nil then storedavatars[steamid] = Steamworks:GetUserAvatar(steamid) end
	context:SetColor(0,0,0,0.5)
	context:DrawRect(x,y,w,rowheight)
	context:SetColor(1,1,1,1)
	if storedavatars[steamid] then
		context:DrawImage(storedavatars[steamid],x+2,y+2,64,64)
	else
		context:DrawRect(x+2,y+2,64,64)
	end
	context:DrawText(name,x+80,y+(rowheight-fh)/2)
	context:DrawText(score,x+300,y+(rowheight-fh)/2)
	y=y+rowheight+rowspacing
end
return true
end

 

To set the hiscore:

SetScore(101)

 

To display the hiscores table:

	--Show hiscores table
ShowHiScores(context)

--Refresh the screen
context:Sync(true)

 

Upload will be up shortly, Windows only at the moment.

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

How many scoreboards do we get? This looks like it could possibly be a way to save some game data for our game player games too wink.png Saying this because we need to be able to save data somewhere for gameplayer games.

 

 

1 for numeric, 2 for seconds

 

2 for seconds? What does that mean? How is a second not numeric?

Link to comment
Share on other sites

Seconds = time. AFAIK this only affects how it is displayed in the community hub.

 

You can set a name for your leaderboard and create as many as you want. I would not use this for settings because it probably doesn't work in offline mode.

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Two quick questions:

 

1) when i first did a 'SetScore' and viewed, it showed 'anonymous and some weird long number instead of 999. I restarted the game and it showed my id and the correct score.

 

2) Can it also store stats, not just a single score?

 

Thanks.

Link to comment
Share on other sites

You should be able to store whatever you want, but you'd have to "bastardize" the system to do that. In other words if you wanted to store how many enemies the player killed you have to make a separate leaderboard for that.

 

I was thinking of something like that where my leaderboard names would be game.stat like:

 

"MyGame.Score"

"MyGame.EnemyKills"

"MyGame.CoinsCollected"

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...