Integrate Steamworks features
Call Steamworks features from your C# scripts and get their results.
After you initialize the Steamworks API, call a feature, such as achievements, leaderboards, or friends lists, through the static class that matches its Steamworks interface, from either a client application or a game server. The methods keep the names and signatures from the Steamworks API reference. For example, SteamUser corresponds to ISteamUser.
Prerequisites
- Initialize the Steamworks API. For more information, refer to Initialize and shut down the Steamworks API.
- Configure the feature on the Steamworks partner website if the feature requires it. For example, you must define achievements, stats, leaderboards, and inventory before you call them in your code. For more information, refer to the interface page for the features in Steamworks API reference.
Call a Steamworks feature from a client application
To call a Steamworks feature:
- In the Steamworks API reference, find the interface for the feature you want to call, for example,
ISteamUserStats. - Call a C# method on the corresponding class with the same name,
SteamUserStats. - Receive the data from the Steamworks API, depending on the method:
- If the method returns a
SteamAPICall_thandle, call its Async version instead, and await the result. For example,SteamUserStats.GetNumberOfCurrentPlayersreturns aSteamAPICall_thandle, so you callSteamUserStats.GetNumberOfCurrentPlayersAsyncand await the result. For methods with no Async version, useSteamCallbacks.CallResultAsync. - If the method triggers a callback, subscribe to it with
SteamCallbacks.Subscribe. For example,SteamUserStats.StoreStatstriggers theUserStatsStored_tcallback. It also triggers theUserAchievementStored_tcallback once for each newly unlocked achievement, so you subscribe to both.
- If the method returns a
You also subscribe to events that your code didn't cause, such as PersonaStateChange_t when a friend's status changes. For more information, refer to Handle callbacks and call results.
The following code example calls two Steamworks features through the SteamFriends and SteamUserStats classes. SteamFriends.GetPersonaName returns the signed-in user's name. SteamUserStats.GetNumberOfCurrentPlayersAsync() starts the request and returns a Task, so the script awaits it.
using UnityEngine;
using Unity.Steamworks.Interop;
public class CurrentPlayers : MonoBehaviour
{
async void Start()
{
if (!SteamAPI.Init())
{
Debug.LogError("Steam is not running.");
return;
}
Debug.Log($"Signed in as {SteamFriends.GetPersonaName()}");
// Asynchronous call: await its call result.
var result = await SteamUserStats.GetNumberOfCurrentPlayersAsync();
if (result.m_bSuccess != 0)
Debug.Log($"{result.m_cPlayers} players are in this game right now.");
else
Debug.LogWarning("Steam couldn't get the number of players.");
}
void OnDestroy()
{
SteamAPI.Shutdown();
}
}
Synchronous methods return the same values as the Steamworks API, such as a bool, an EResult, or an invalid handle. The package doesn't add error codes or exceptions. For failures when you await a call result, refer to Await call results.
Call a Steamworks feature from a game server
To call a feature from a game server, follow the same steps as for a client application with the following differences:
- Initialize the Steamworks API with
GameServer.InitExinstead ofSteamAPI.Init. - Call the
SteamGameServerclasses, such asSteamGameServerStatsandSteamGameServerUtils, instead of the client classes. - Pass
gameServer: truetoSteamCallbacks.SubscribeandSteamCallbacks.CallResultAsync. Without this argument both methods receive only client messages, not server messages.
For more information, refer to Initialize and shut down the Steamworks API and Handle callbacks and call results.