Main forum > GLBasic - en

Steam API for GLBasic -> achievements, leaderboards for Your game ;-)

(1/4) > >>

dreamerman:
Current version: 09.03.2022

This is my attempt to use Steam API or rather some of its parts in GLbasic. Yeah, it isn't clean code, not bullet-proof, and have some issues, it's still under constant development to fit my needs. Most important thing is that, user stats and achievements are working, tested on live app, leaderboards are under testing.

Download consist of two files:
steam_sdk.gbas -> imports for proper Steam API functions from dll, only part of functions were tested.
stats_manager.gbas -> Stat Manager type/object, main thing simplify using Steam stuff in game, supports user stats (int, float), achievements, leaderboards/highscore, and some help functions.

Take note, that if You will run project from Steam version GLBasic editor it will inherit its app_id, so You want be able to test it properly, not sure if putting 'steam_appid.txt' into project exe path should fix that, but You may need to either grab STDOUT to file or use alternative editor/older version.
Also read this: https://partner.steamgames.com/doc/features/achievements


How to use.
First create userstats, achievements, leaderboards on steamworks app admin panel.
Setup in game code:

1. set some object/udt for stats manager

--- Code: (glbasic) ---GLOBAL mySteam AS stats_manager_object, i1%
mySteam.Init(1)
--- End code ---

2. set all
userstats, achievements - max value for stats to unlock achievements

--- Code: (glbasic) ---mySteam.addAchievement("points_1000")
mySteam.addAchievement("points_3000")
mySteam.addAchievement("10games_played")
mySteam.addUserStat("games_played", 10, 0, 0, 2) // 2 because '10games_played' achievement has such index in our in-game array
mySteam.addAchievement("collect_100_elixirs")
mySteam.addUserStat("elixirs_collected", 100, 0, 1, 3) // this is rapidly changing userstat

--- End code ---
leaderboards - info, names

--- Code: (glbasic) ---mySteam.addLeaderboard("new_levels")
mySteam.addLeaderboard("classic_levels")
// if you want to use avatars create sprite for storing them - should be large to contain hundreds small avatars
mySteam.setLeaderboarOptions(1, 1)
CREATESCREEN 10, 10, 1024, 1024
mySteam.setAvatarsBufferInfo(9, 10)
--- End code ---

3. fetch leaderboards main handles - provided by Steam (stats for current user are called in Init() internally, and processed with first RunFrame())

--- Code: (glbasic) ---mySteam.leaderboardPrepare()
--- End code ---

4. additional setup:
to receive info when leaderboard is ready add this function:

--- Code: (glbasic) ---FUNCTION StatManager_LeaderboardReady%: handle AS int64, tab_id%, top_or_near%
    LOCAL i1%, pid%, tnick$
   
IF (game_menu% = CONST_HIGHSCORES)
IF (tab_id% = 0 AND top_or_near% = 0)
FOR i1% = 0 TO mySteam.leaderboards[tab_id%].top_count% - 1
pid% = mySteam.leaderboards[tab_id%].top_entry[i1%].id_onlist%
tnick$ = mySteam.usersinfo_list[pid%].nickname$
STDOUT "score: " + i1% + ": " + mySteam.leaderboards[tab_id%].top_entry[i1%].score% + " - " + tnick$ + "\n"
// avatars are stored on sprite defined with setAvatarsBufferInfo(), rectangle position left-top: mySteam.usersinfo_list[pid%].image_pos.x%, mySteam.usersinfo_list[pid%].image_pos.y%, mySteam.settings.avatars_size_px%, mySteam.settings.avatars_size_px%
NEXT
ENDIF
ENDIF
ENDFUNCTION
--- End code ---

additional functions that may be usefull:

--- Code: (glbasic) ---StatManager_LeaderboardScoreUploaded() // called when score was uploaded

FUNCTION GLB_READ_ARGS%: caller_type% // to get cmd line arguments from Steam - when app is launched from browser
    LOCAL game_mode% = -1
    IF (caller_type% > 0)       // new query parameter
        game_mode% = Steam_API_Apps_GetLaunchQueryParam$("gamemode")
    ENDIF
    IF (game_mode% > -1)
// navigate to that game mode
    ENDIF
ENDFUNCTION
--- End code ---

Usage after succesfull initialization:
Stats/achievements are ready to use after few RunFrame() calls, leaderboards after fetching handles - may take few extra ms.

for rapidly incremented userstats - destroyed enemies, items picked, doors unlocked...

--- Code: (glbasic) ---mySteam.updateUserStat(1, "elixirs_collected", 1, 0) // thats it, if this stat will reach its max/unlock value, 'mySteam.storeUserStats(0)' will be called automatically and proper achievement popup will be shown
--- End code ---

for other achievements - beat some score to unlock use this:

--- Code: (glbasic) ---IF (match_points% >= 1000 AND mySteam.achievements_list[0].unlocked% = 0) THEN mySteam.unlockAchievement(0, "points_1000", 0)
IF (match_points% >= 3000 AND mySteam.achievements_list[1].unlocked% = 0) THEN mySteam.unlockAchievement(1, "points_3000", 0)
mySteam.storeUserStats(0) // this will show Steam achievement popup

--- End code ---

to refresh leaderboards, for example when user clicks to 'Highscore' button in menu:

--- Code: (glbasic) ---mySteam.leaderboardRefresh(-1) // can be called in some longer intervals to get current/updated highscore
--- End code ---

upload new score with

--- Code: (glbasic) ---mySteam.uploadHighscore(leaderboard_id%, user_score%, "")
--- End code ---


If you will have any questions or issues let me know.

spacefractal:
first time was due the compiler glbasic was used was simply just...... too old... but howover yes you are right as archivements, mostly in Genius Greedy Mouse (which have bounch of internal once) should do have them.

The compilers is updated and should been quite easy to integrate the steam SDK directly into the source code im should do this a day.

dreamerman:
Yeah, I wasn't sure but I though that Genius Greedy Mouse had some kind of 'achievements'.. I know that game should defend itself by quality, but now even small games have achievements, and some users just skip games without them with filters or just thinking that game isn't finished or due other reasons.
That's really nice to hear that such integration (into GLB source code) should be now easier to implement. As ability to call Steam API in such simple way (at least those basic things like stats, achievements, maybe leaderboards) would be awesome and that could be another great feature to list on Steam product page.

MrPlow:
Achievements would be great to have for Steam - is this planned soon for a future update?

bigsofty:
Yup, access to the Steam API in general would be very handy.

Nice bit of inlining there BTW Dreamerman!

Navigation

[0] Message Index

[#] Next page

Go to full version