Class GameServices

java.lang.Object
com.codename1.gaming.GameServices

public final class GameServices extends Object

The gaming platform the application runs under: achievements, stats, cloud saves, the signed in player and their presence.

One API for every platform. The first backend is Steam (the steam build target); others plug in behind the same calls. Everything here is safe to call everywhere: with no platform, or one that has not connected, #isAvailable() is false and every call does nothing and answers its default, so a game needs no platform checks around it.

GameServices.unlockAchievement("FIRST_WIN");
GameServices.setStat("wins", GameServices.getStatInt("wins", 0) + 1);
GameServices.commit();

Identifiers are the ones configured with the platform (on Steam, the API names entered in the Steamworks partner site).

Pausing. Register a GameServicesListener to hear when the platform's own interface (the Steam overlay) opens over the game; pause while it is up.

Call from the EDT.

  • Method Details

    • setBackend

      public static void setBackend(GameServicesBackend b)
      Installs the platform backend. Called by ports.
    • getPlatformName

      public static String getPlatformName()
      The platform name ("steam"), or an empty string when there is none.
    • isAvailable

      public static boolean isAvailable()
      True when a gaming platform is connected and a player signed in.
    • getPlayerId

      public static String getPlayerId()
      The player's stable platform id (a Steam id, for instance), or an empty string.
    • getPlayerName

      public static String getPlayerName()
      The player's display name, or an empty string.
    • unlockAchievement

      public static boolean unlockAchievement(String id)
      Unlocks an achievement. Platforms show their own notification. Persisted by #commit().
    • resetAchievement

      public static boolean resetAchievement(String id)
      Locks an achievement again, for testing; platforms may refuse it outside development builds.
    • isAchievementUnlocked

      public static boolean isAchievementUnlocked(String id)
      Whether an achievement is unlocked.
    • getStatInt

      public static int getStatInt(String id, int def)
      An integer stat, or def when unknown.
    • setStat

      public static boolean setStat(String id, int value)
      Sets an integer stat. Persisted by #commit().
    • getStatFloat

      public static float getStatFloat(String id, float def)
      A float stat, or def when unknown.
    • setStat

      public static boolean setStat(String id, float value)
      Sets a float stat. Persisted by #commit().
    • commit

      public static boolean commit()
      Sends achievement and stat changes to the platform. Call it at natural points (end of a level), not every frame: platforms rate limit it.
    • isCloudSaveAvailable

      public static boolean isCloudSaveAvailable()
      Whether saves are synced through the platform for this player.
    • writeSave

      public static boolean writeSave(String name, byte[] data)
      Writes a cloud save, replacing one with the same name.
    • readSave

      public static byte[] readSave(String name)
      Reads a cloud save, or null when there is none.
    • hasSave

      public static boolean hasSave(String name)
      Whether a cloud save exists.
    • deleteSave

      public static boolean deleteSave(String name)
      Deletes a cloud save.
    • setPresence

      public static boolean setPresence(String key, String value)
      Sets what friends see the player doing. On Steam key is a rich presence key ("status" is shown as is).
    • clearPresence

      public static boolean clearPresence()
      Clears every presence value.
    • ownsContent

      public static boolean ownsContent(String id)
      Whether the player owns an add-on (on Steam, a DLC app id).
    • openPlatformUI

      public static boolean openPlatformUI(String target)
      Opens the platform's own interface over the game. target names a page: on Steam one of the overlay dialogs ("friends", "achievements", "stats", "community", "settings") or an https:// URL. Answers false when the platform has no such interface.
    • isControllerFirst

      public static boolean isControllerFirst()
      True when the player is on a controller-first interface (a Steam Deck, Steam's Big Picture / Gaming Mode): size touch targets and text for it and expect no mouse.
    • getLanguage

      public static String getLanguage()
      The language the player chose for the game on the platform, as the platform names it (Steam: "english", "german"...), or an empty string.
    • addListener

      public static void addListener(GameServicesListener l)
      Registers a listener.
    • removeListener

      public static void removeListener(GameServicesListener l)
      Unregisters a listener.
    • firePlatformUIChanged

      public static void firePlatformUIChanged(boolean shown)
      Tells listeners the platform's interface opened or closed. Called by backends, on the EDT.