Here is a short tutorial on how I implemented Steam Achievements with the help of luasteam. I used the documentation from luasteam, as well as code from a327ex, which has tons of useful code (with an MIT license), as well as some video tutorials from AuroDev and the SteamWorks documentation.
I would like to note in advance that my writing style is a bit too verbose, so I apologize in advance for the huge wall of text. I am not too good at writing tutorials. Also, this tutorial is a bit later than I had planned to release it
What I used to add Steam Achievements were four things:
1. A SteamWorks SDK .dll file (from the official SteamWorks SDK)
2. A luasteam .dll file
3. Added the achievements in Steam.
(I followed this tutorial from AuroDev's YouTube video)
4. Functions that start, run and exit the Steam SDK, and functions that unlock the achievements.
I released my first game in 2022, so the SteamWorks SDK version I used is the one from 2022. This is important, because some functions might have changed in the meantime for newer versions of luasteam and SteamWorks SDK, so please be aware of this. For the version of SteamWorks SDK I used, this was the luasteam I downloaded (Version 2.0.0),
https://github.com/uspgamedev/luasteam/ ... .0.0%2Bfix
From the above link I downloaded the win64_luasteam.dll file and renamed it to luasteam.dll (like how it says in the official documentation).
Next you have to download the SteamWorks SDK file for your system.
Now you place these two files you just downloaded in your game's folder, where you have your main.lua file.
Upon starting the game, like all libraries you might be using, you have to require and initialize the luasteam library. I followed the method from here, and added the following code,
Code: Select all
Steam = require 'luasteam'
if type(Steam) == 'boolean' then Steam = nil end
Code: Select all
ffi = require('ffi')
Next, in the love.load() function I initialized Steam with luasteam like this,
Code: Select all
Steam.init()
Now, for the actual function that will unlock the Steam achievements I used the following function from a327ex as starting point, but I also had to add the Steam.userStats.requestCurrentStats() before actually setting the achievement,
Code: Select all
function unlockAchievement(achievement_name)
if achievements[achievement_name] then return end
achievements[achievement_name] = true
if Steam.init() then
Steam.userStats.requestCurrentStats()
Steam.userStats.setAchievement(achievement_name)
timer:after(0.5, function() Steam.userStats.storeStats() end) -- the timer I used was from vrld's excellent HUMP library
end
end
Now we should add the achievements in Steam. I followed this tutorial by AuroDev,
https://www.youtube.com/watch?v=s554p28MTxY
We should add these achievements in our game as well. I did this like so,
Code: Select all
achievements = {"score1k", "score10k", "50skeletons", etc.}
in the logic we have for checking if we can remove a skeleton, we can implement something like this,
Code: Select all
if numberSkeletonsEliminated >= 50 then
unlockAchievement("50skeletons")
end
Bonus: for loading what achievements a player has unlocked, you can use the following function (from a327ex):
Code: Select all
function loadAchievementsFromSteam()
if Steam.init() then
Steam.userStats.requestCurrentStats()
for _, achievement_name in ipairs(achievement_names) do
local b = ffi.new('bool[1]')
Steam.userStats.getAchievement(achievement_name, b)
achievements[achievement_name] = b[0]
end
end
end
Hope this helps! Let me know if you have any questions.
EDIT: One thing I forgot to mention in my original post is that I also added a Steam.shutdown() when the player exits the game. So, when you exit the game you can do something like,
Code: Select all
Steam.shutdown()
love.event.quit()