Skip to content

Search

Dokumentation

Stores

The platform module is one script API over every store: sign-in, achievements, leaderboards, cloud saves and purchases, its answers, and rollback.

På denne side

The platform module is one script API over every store.

  • One call unlocks an achievement on Game Center, Play Games or Steam, whichever runs the game.
  • One call buys a product from the App Store or Google Play, in one shape.
  • What only one platform has lives in that platform's module: apple, google, or Steam's steam_* modules (Steam).
  • With no store loaded (the editor, a desktop run, a replay) every call answers kind = "unsupported".
pub class Player {
    node: Node,
}

impl Player {
    pub async fn init() {
        events::listen(self.node, "platform_event");
        let me = await task::wait(platform::sign_in());
        log::info("playing as {me["alias"].as_str()}");
        platform::unlock("first_blood");
    }

    pub fn on_platform_event(e: any) {
        if e["kind"].as_str() == platform::EVENT_SIGNED_OUT {
            // The player signed out in the OS. A node that listens hears it.
        }
    }
}
  • Delivery is the networking and Gamend contract. A call returns an id at once. The store answers on a later tick, to whoever awaits the id.
  • A sign-in or a sign-out also reaches on_platform_event on a node that called events::listen(node, "platform_event").
  • Every answer carries a kind, an EVENT_* constant: signed_in, done, scores, read, purchased, error, unsupported and the rest.

The portable module

Call What it does
sign_in, player Who is playing. A node listening for platform_event hears a later sign-out
unlock, progress Award an achievement, or report progress on it
submit_score, scores Post to a leaderboard, and read it: scope, period, count and start in the options
cloud_read, cloud_write A value the store syncs between the player's devices
identity A proof of the player a server checks with the store
set_presence What the player is doing, where the store shows it
products, purchase, finish_purchase, restore_purchases, entitlements Buying, below
is_available Whether a service works here, before asking

A store without one of these answers unsupported: Game Center and Play Games have no presence, and Steam leaves purchases to a server.

Purchases

The same five calls buy on the App Store and on Google Play, in one shape: platform::Product for what is for sale, and platform::Transaction for what was bought.

  1. products with a list of product ids answers products, each a Product: title, description, price, display_price, currency and whether it is a subscription.
  2. purchase opens the store's sheet. It answers purchased with the transaction's fields, cancelled, pending, or error.
  3. The game posts the transaction's token to its server, which checks it with the store and grants what was bought.
  4. Then finish_purchase with the transaction's id. A store hands an unfinished one back on every launch.
// scripts/shop.bs
pub class Shop {
    node: Node,
}

impl Shop {
    pub async fn init() {
        events::listen(self.node, "platform_event");
        if !platform::is_available(platform::SERVICE_PURCHASES) {
            return;
        }
        let reply = await task::wait(platform::products(["coins_100", "no_ads"]));
        let rows: [platform::Product] = reply["products"];
        for row in rows {
            log::info("{row.title}: {row.display_price}");
        }
    }

    pub async fn buy(product: str) {
        let bought = await task::wait(platform::purchase(product));
        if bought["kind"].as_str() != platform::EVENT_PURCHASED {
            return; // cancelled, pending or error
        }
        let sale: platform::Transaction = bought;
        // Post sale.token to the server. Once it has granted the coins:
        platform::finish_purchase(sale.id, platform::FinishPurchaseOptions { consume: true });
    }

    pub fn on_platform_event(e: any) {
        if e["kind"].as_str() == platform::EVENT_TRANSACTION {
            // A purchase that landed unasked: a pending one cleared, a renewal.
            let sale: platform::Transaction = e;
            log::info("{sale.product} arrived; check {sale.token} on the server");
        }
    }
}
  • consume = true is for what is bought again and again, such as coins. Play consumes it rather than acknowledging it; StoreKit finishes either the same way.
  • restore_purchases asks the store to hand the player's purchases back, the button a review expects. Read them with entitlements after.
  • entitlements answers what the player owns now, as items, each a Transaction.
  • A Transaction holds id, product, purchased_at (seconds since 1970), quantity, revoked, token and verified.

token is the App Store's signed JWS, or Play's purchase token. Gamend checks either at /api/v1/payments/validate/<provider>: see Apple services and Google services for what each store needs. Steam answers unsupported: its purchases are a Web API a server calls.

Achievements and rollback

  • An achievement cannot be un-awarded. A rollback game can re-run a predicted tick with different inputs.
  • A call that changes the store waits until the tick that made it can no longer be re-run. A purchase opens the store's sheet, so it waits too.
  • Reads never wait. Repeating one costs a round trip.
  • Without a rollback session every tick is final at once.

Per platform

What answers each call on each platform, and how to set each store up, is in Platform services. Apple's own calls are in Apple services, Google's in Google services, Steam's in Steam, and sign-in pages in Sign-in pages.

The purchase calls are tested through a canned store and stand-ins only. No purchase has yet gone through the App Store or Google Play. The full function list is in the script modules reference.

Er du sikker?