Before we type any code, we need to inform our game about each achievement (including the name and it’s ID). Remember that Steam offers the Spacewar game as a sample project in order to integrate any platform specific features to our game that hasn’t yet been Greenlit. Having said that, we’re going to use Spacewar’s achievements for our game.
To inform our game about each achievement, open up the DefaultEngine.ini located inside the Config folder and in [OnlineSubsystemSteam] category, add the following lines:
C++
1
2
3
Achievement_0_Id="ACH_WIN_ONE_GAME"
Achievement_1_Id="ACH_WIN_100_GAMES"
Achievement_3_Id="ACH_TRAVEL_FAR_SINGLE"
The id and name of each achievement was retrieved from the following Steamworks screenshot:
If your game gets Greenlit, you can add your own achievements.
Progressing Achievements
In order to progress any achievement, we’re going to use the following logic:
We’re going to cache each achievement when the game starts (BeginPlay).
If the player completes an achievement, we’re going to search the cached achievements and if the achievement is valid we’re going to complete it.
Open up the header file of your Character class and add the following header file:
#include "OnlineStats.h"
Then, add the following code:
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
virtualvoidBeginPlay()override;
/** Get all the available achievements and cache them */
The reason we wrap each achievement inside a macro is because it’s less prone to errors since we can avoid typing wrong strings later in our code. Let’s implement the QueryAchievements functions and BeginPlay:
//Get a thread-safe pointer (for more info check out this link: https://docs.unrealengine.com/latest/INT/API/Runtime/Core/Templates/TSharedPtr/index.html )
//Get a thread-safe pointer (for more info check out this link: https://docs.unrealengine.com/latest/INT/API/Runtime/Core/Templates/TSharedPtr/index.html )
The last thing we need is to implement the achievement functions. These function are going to call the UpdateAchievementProgress function with the desired Achievement they wish to complete. I’ve added the BlueprintCallable specifier in order to test them using key binds in the Blueprint graph. Here is the logic for each function:
At this point, launch your game on Standalone mode (this assumes that you have enabled the Online Subsystem Steam plugin which resides in the Online Platform category) and test your achievements!
In this post I'm going to show you how to create Functional Tests with the…
This Post Has 9 Comments
Hi orfeasel,
I cant find those headers such as “#include “OnlineStats.h”” ,#include “OnlineAchievementsInterface.h”,etc.
My engine version is 4.14.1,Can you help please?
If anyone is trying to create a plugin or Subsystem that does this and is having include errors, you will need to add your plugin/subsystem to the list of included modules for your project. This can be done by adding the name in the .uproject file and the “yourprojectname”.build.cs file.
Marin was right, this tutorial no longer works. Any help with making a new one? Even better if it’s just creating general function achievement nodes instead of tying them to the character.
Is there a way to do this just using blueprints? I am attempting to unlock achievements using the Cache Achievements and Write Achievements Progress nodes, but my Cache Achievements node always fails.
The Steam overlay works correctly. Any idea what would be going on?
Hi orfeasel,
I cant find those headers such as “#include “OnlineStats.h”” ,#include “OnlineAchievementsInterface.h”,etc.
My engine version is 4.14.1,Can you help please?
Hello,
That’s weird, have you tried running your project with another version?
-Orfeas
You don’t need it for this tutorial but if you plan on doing anything else with the steam api you need to include these in your “MyProject.h”
PrivateDependencyModuleNames.Add(“Steamworks”);
AddThirdPartyPrivateStaticDependencies(Target, “Steamworks”);
and then include this inside of the header you plan on doing stuff in.
#include
This will allow you to call functions like this
//get the player iID
uint64 id = *((uint64*)UniqueNetId.UniqueNetId->GetBytes());
int Picture = 0;
// get the Avatar ID using the player ID
Picture = SteamFriends()->GetMediumFriendAvatar(id);
Hopefully that will save someone the few hours it took me to figure it out.
The #include didnt show up it was
#include
steam / steam api .h but with chevrons
If anyone is trying to create a plugin or Subsystem that does this and is having include errors, you will need to add your plugin/subsystem to the list of included modules for your project. This can be done by adding the name in the .uproject file and the “yourprojectname”.build.cs file.
Marin was right, this tutorial no longer works. Any help with making a new one? Even better if it’s just creating general function achievement nodes instead of tying them to the character.
Hi Orfeas,
Is there a way to do this just using blueprints? I am attempting to unlock achievements using the Cache Achievements and Write Achievements Progress nodes, but my Cache Achievements node always fails.
The Steam overlay works correctly. Any idea what would be going on?
Thanks,
Jackson
I managed to get this to work in Unreal Engine 5.3.1. The includes are slightly different and you also need module imports. Here they are.
#include “OnlineSubsystem.h”
#include “OnlineStats.h”
#include “Interfaces/OnlineIdentityInterface.h”
#include “OnlineSubsystemUtils.h”
#include “Interfaces/OnlineAchievementsInterface.h”
modules:
PrivateDependencyModuleNames.AddRange(new string[] { “OnlineSubsystem”, “OnlineSubsystemUtils” });
DynamicallyLoadedModuleNames.Add(“OnlineSubsystemSteam”);