Steam
Steam from Balaur Script: Steam behind platform.*, the steam modules in every desktop build, a game that runs without Steam, and balaur export --steam.
Every desktop build of the engine has Steam in it, and so does the editor.
Valve's Steamworks SDK is loaded at run time, from the steam_api library
beside the game, when the Steam client runs it.
- Without that library or the client, the game runs as before. Every
steam_*call answers nil, and a call that answers later answersunsupported. steam::is_loaded()says which case you are in.- Where Steam is loaded, it is the store behind
platform.*, below. Platform services says what answers each call elsewhere.
Steam behind platform.*
Where the Steam client runs the game, Steam answers the portable calls, on
Windows, Linux and a Mac alike. platform::backend() answers "steam", and
a script that names only platform.* runs on every store.
| Call | What Steam does |
|---|---|
sign_in, player |
the Steam id as id and the persona name as alias |
identity |
a Web API ticket, hex, as ticket, for ISteamUserAuth/AuthenticateUserTicket on the server |
unlock |
sets the achievement and stores the stats, answering done once Steam stored them |
progress |
under 100, the progress note Steam's overlay shows; at 100, an unlock |
submit_score, scores |
a leaderboard, found or made by name, keeping the player's best |
cloud_read, cloud_write |
a file of that name in the game's Steam Cloud, read and written whole |
set_presence |
the rich presence text the friends list shows |
web_sign_in and the purchase calls |
unsupported |
// scripts/steam_platform.bs
pub class SteamPlatform {
node: Node,
}
impl SteamPlatform {
pub async fn init() {
if platform::backend() != "steam" {
return;
}
let me = await task::wait(platform::sign_in());
log::info("playing as {me["alias"].as_str()}");
platform::unlock("ACH_FIRST_RUN");
platform::submit_score("best_time", 4210);
platform::set_presence("In the first dungeon");
let ticket = await task::wait(platform::identity());
if ticket["kind"].as_str() == platform::EVENT_IDENTITY {
// Hex, for ISteamUserAuth/AuthenticateUserTicket on the server.
log::info("ticket of {ticket["ticket"].as_str().len()} hex digits");
}
}
}
progresskeeps nothing: Steam shows the note and forgets it. Progress that should last is a stat, set throughsteam_user_stats, which Steam turns into the achievement's progress bar.- Achievement calls wait for the player's stats, which Steam fetches as the game starts.
- Steam keeps one table a leaderboard, so
scores'periodchanges nothing. - A cloud key with no file reads nil. The last write wins, and Steam syncs as the game starts and stops, with its own dialog for a conflict.
- Achievements, leaderboards and Steam Cloud are switched on, and the achievements made, in the app's Steamworks settings.
- Notifications, push and
device.*stay the OS's on a Mac Steam started.
The modules
Each Steamworks interface is a module: Valve's method names in snake case.
SteamUser()->GetSteamID() is steam_user::get_steam_id(), and
SteamUserStats()->GetAchievement is steam_user_stats::get_achievement.
The script modules reference lists every one.
steamholdsis_loaded, Valve's enums and constants under their own names, and theEVENT_*kinds.- A call Valve answers later returns an id.
task::waiton it answers a map whosekindis anEVENT_*constant, with the callback's fields andfailed. - Callbacks nobody asked for arrive as the
steam_eventevent, on a node that calledevents::listen(node, "steam_event").
// scripts/steam_hooks.bs
pub class SteamHooks {
node: Node,
}
impl SteamHooks {
pub async fn init() {
if !steam::is_loaded() {
log::info("no Steam here; the game plays on without it");
return;
}
events::listen(self.node, "steam_event");
let name = steam_friends::get_persona_name().or("player");
log::info("playing as {name}");
let first = steam_user_stats::get_achievement("ACH_FIRST_RUN");
if first != nil && !first.unwrap().achieved {
steam_user_stats::set_achievement("ACH_FIRST_RUN");
steam_user_stats::set_stat_int32("runs", 1);
steam_user_stats::store_stats();
}
let count = await task::wait(steam_user_stats::get_number_of_current_players());
if count["kind"].as_str() == steam::EVENT_NUMBER_OF_CURRENT_PLAYERS {
log::info("{count["players"]} playing now");
}
}
pub fn on_steam_event(e: any) {
if e["kind"].as_str() == steam::EVENT_USER_STATS_STORED {
log::info("stats stored");
}
}
}
Exporting for Steam
A plain export ships no Valve file. A Steam build is the same export with one flag:
balaur export . --target windows-x64 --steam
-
--steamcopies Valve'ssteam_apibeside the game, insideContents/MacOSin a.app, signed there with the game's identity. -
It needs the AppID Steamworks gave the game, in
project.toml:[steam] app_id = 480 -
--steam --devalso writes the AppID tosteam_appid.txtbeside the game, so it starts without the Steam client. A release leaves it out: the client names the app.
Steam takes 64-bit Windows, macOS and 64-bit Linux. Forge builds the Steam build beside the plain one and uploads it to your depots; see the Forge Steam guide.
Not yet run against Steam
The platform.* calls are tested against a fake steam_api: test
functions under Valve's names, queuing callbacks and call results as a
client does, and a replay of their answers with no library loaded. A
--steam export runs with Valve's real library and no client on each
desktop. Nothing has yet run against a real Steam client, so no achievement
has reached a real profile.