Class TGameService
Unit
Declaration
type TGameService = class(TComponent)
Description
Integration with a game service, that can be used to show achievements, leaderboards, and store save games "in the cloud". This integrates with
Google Play Games on Android
Apple Game Center on iOS.
Usage:
Include the necessary integration code in your Android / iOS project.
For Android, declare your project type as "integrated" and add the "google_play_games" service inside CastleEngineManifest.xml. See https://castle-engine.io/android-Project-Services-Integrated-with-Castle-Game-Engine .
For iOS, add the "apple_game_center" service inside CastleEngineManifest.xml. See https://castle-engine.io/ios-Project-Services-Integrated-with-Castle-Game-Engine .
Build your project with the Castle Game Engine build tool: https://castle-engine.io/build_tool .
Create an instance of this class. Only a single instance of this class is allowed.
Call TGameService.Initialize. You usually do it from TCastleApplication.OnInitialize. The TGameService.Initialize must be called before calling any other method of this class.
Use this to manage achievements, leaderboards and so on.
The user must be "signed in" to the game service.
The methods that display some user-interface will automatically attempt to sign-in the user if needed. These include ShowAchievements, ShowLeaderboard, ShowSaveGames. So you don't have to do anything before you call them.
All other methods will not sign-in the user automatically. For example, Achievement or SubmitScore or SaveGameLoad or SaveGameSave. You should always make sure that the user is signed-in before calling them. To do this, pass AutoStartSignInFlow parameter as
True
to Initialize or call the RequestSignedIn(true). And then wait for the Status property to change to gsSignedIn (you can register OnStatusChanged to be notified about changes).
Hierarchy
- TObject
- TPersistent
- TComponent
- TGameService
Overview
Methods
procedure DoSignedInChanged; virtual; deprecated 'use DoStatusChanged'; |
|
procedure DoStatusChanged; virtual; |
|
procedure DoPlayerBestScoreReceived(const LeaderboardId: string; const Score: Int64); virtual; |
|
procedure DoSaveGameChosen(const Choice: TSaveGameChoice; const SaveGameName: string); virtual; |
|
procedure DoSaveGameLoaded(const Success: boolean; const Content: string); virtual; |
|
constructor Create(AOwner: TComponent); override; |
|
destructor Destroy; override; |
|
procedure Initialize(const AutoStartSignInFlow: boolean = true; const SaveGames: boolean = false); |
|
function SignedIn: boolean; |
|
procedure Achievement(const AchievementId: string); |
|
procedure SubmitScore(const LeaderboardId: string; const Score: Int64); |
|
procedure RequestPlayerBestScore(const LeaderboardId: string); |
|
procedure RequestSignedIn(const Value: boolean); |
|
procedure ShowAchievements; |
|
procedure ShowLeaderboard(const LeaderboardId: string); |
|
procedure ShowSaveGames(const Title: string; const AllowAddButton, AllowDelete: boolean; const MaxNumberOfSaveGamesToShow: Integer); |
|
procedure SaveGameSave(const SaveGameName, Contents, Description: string; const PlayedTime: TFloatTime); |
|
procedure SaveGameLoad(const SaveGameName: string); |
Properties
property Initialized: boolean read FInitialized; |
|
property Status: TGameServiceStatus read FStatus; |
|
property OnStatusChanged: TNotifyEvent read FOnStatusChanged write FOnStatusChanged; |
|
property OnSignedInChanged: TNotifyEvent read FOnStatusChanged write FOnStatusChanged stored false; deprecated 'use OnStatusChanged'; |
|
property OnPlayerBestScoreReceived: TPlayerBestScoreEvent read FOnPlayerBestScoreReceived write FOnPlayerBestScoreReceived; |
|
property OnSaveGameChosen: TSaveGameChosenEvent read FOnSaveGameChosen write FOnSaveGameChosen; |
|
property OnSaveGameLoaded: TSaveGameLoadedEvent read FOnSaveGameLoaded write FOnSaveGameLoaded; |
Description
Methods
procedure DoSignedInChanged; virtual; deprecated 'use DoStatusChanged'; |
|
Warning: this symbol is deprecated: use DoStatusChanged |
procedure DoStatusChanged; virtual; |
|
procedure DoPlayerBestScoreReceived(const LeaderboardId: string; const Score: Int64); virtual; |
|
procedure DoSaveGameChosen(const Choice: TSaveGameChoice; const SaveGameName: string); virtual; |
|
procedure DoSaveGameLoaded(const Success: boolean; const Content: string); virtual; |
|
constructor Create(AOwner: TComponent); override; |
|
destructor Destroy; override; |
|
procedure Initialize(const AutoStartSignInFlow: boolean = true; const SaveGames: boolean = false); |
|
Connect to the service, optionally try to sign-in player. If the player already connected this application with Google Play Games (Android) or Apple Game Center (iOS), (s)he will be signed-in automatically. If not, player will be asked to sign-in (which means asking to accept permissions on Android) only if AutoStartSignInFlow. So if you want to avoid the initial Google Play Games dialog on Android, just always pass AutoStartSignInFlow=false. Calling this when already initialized is harmless (will not do anything). In theory, you can call this at any point in your application. But you probably want to call it early, e.g. from Application.OnInitialize, otherwise user will not be signed-in automatically. Most calls (like sending the leaderboars or achievements) will be ignored until you call this. Parameters
|
function SignedIn: boolean; |
|
Is user currently signed-in. Just a shortcut for |
procedure RequestPlayerBestScore(const LeaderboardId: string); |
|
Get the best score, if available, for given leaderboard. This may (after some time, asynchronously) call OnPlayerBestScoreReceived with this score. Note that no error is signalled if loading the score fails for any reason. This includes failing to load because user is not connected to the game service (this method doesn't connect automatically; wait for OnStatusChanged before calling this, if you need). TODO: Not implemented for Apple Game Center (iOS) yet. |
procedure RequestSignedIn(const Value: boolean); |
|
Request sign-in or sign-out. The operation will be done asynchronously (it will in most cases require some network communication). Watch for changes to the Status property. You can register an event on OnStatusChanged to be notified about this. |
procedure ShowAchievements; |
|
Show the user achievements, using the default UI. Automatically connects (signs-in) player to game services, if not connected yet. |
procedure ShowSaveGames(const Title: string; const AllowAddButton, AllowDelete: boolean; const MaxNumberOfSaveGamesToShow: Integer); |
|
Show the existing saved games stored in the cloud for this user. This can be used to offer user a choice in which slot to save the game, or from which slot to load the game. Note that it's not necessary to use this method to manage savegames. E.g. if you want, you can just choose a constant savegame name for your game, and use SaveGameSave and SaveGameLoad with this name. This would imply that each game service user (Google Play Games user or Apple Game Center user) has only one savegame in the cloud for your game. The user may choose an existing savegame, or indicate creation of a new savegame (if parameter AllowAddButton is
Using this requires initializing the game service first, by calling the Initialize with Note that the OnSaveGameChosen callback is not called if the user cancels the sign-in operation, and therefore cancels the savegame choice this way. The same remark applies to all reasons why sign-in may fail (network problems etc.). If this is a problem for your logic (e.g. if you want to "wait" until OnSaveGameChosen is called), then never call TODO: Not implemented for Apple Game Center (iOS) yet.
Parameters
|
procedure SaveGameSave(const SaveGameName, Contents, Description: string; const PlayedTime: TFloatTime); |
|
Save a savegame identified by the given name. See the SaveGameLoad documentation about the conflict resolution and valid savegame names. The Contents should be a valid UTF-8 string. For implementation reasons, you should not save arbitrary binary data this way, for now (or it could result in exceptions about being unable to encode/decode UTF-8 sequences). Description and PlayedTime are shown to player in ShowSaveGames. PlayedTime may also be used for conflict resolution (if the savegame on the server was modified in the meantime, without loading it in this game). No callback is called in response, the game is saved in the background. This does not connect player to game service, if it's not connected already. An error when saving is not reported back to Pascal, for now. (The assumption here is that you will keep a local savegame anyway, in case user does not connect to game service. So inability to save the savegame to the cloud is not alarming, and does not require any special reaction. Please submit a request if you'd like to have a callback about it.) |
procedure SaveGameLoad(const SaveGameName: string); |
|
Load a savegame identified by the given name. If the savegame does not exist, it will be automatically created. If the server requires conflict resolution, the savegame with longest playtime is used (internally: we use RESOLUTION_POLICY_LONGEST_PLAYTIME flag with Google Play Games). For this to work, you must provide a proper PlayedTime parameter when saving all your savegames through SaveGameSave. Valid savegame names are defined by Google Play Games: Must be between 1 and 100 non-URL-reserved characters (a-z, A-Z, 0-9, or the symbols "-", ".", "_", or "˜"). In response, the callback OnSaveGameLoaded will be always called, with the loaded savegame contents (as a string), or the error message. This does not connect player to game service (like Google Play Games), if it's not connected already. An error will be reported to OnSaveGameLoaded callback if trying to load fails for any reason. This includes the case when loading fails because user is not connected to game service yet (this method does not connect user automatically; wait for OnStatusChanged before calling this method, if you need it). |
Properties
property Initialized: boolean read FInitialized; |
|
Was the Initialize called. |
property Status: TGameServiceStatus read FStatus; |
|
Current status of signing-in. |
property OnStatusChanged: TNotifyEvent read FOnStatusChanged write FOnStatusChanged; |
|
Event called when Status changed, for example because RequestSignedIn was called, or because user signs-in automatically (which may happen if you used AutoStartSignInFlow with Initialize, or if user was signed-in in this application previously). |
property OnSignedInChanged: TNotifyEvent read FOnStatusChanged write FOnStatusChanged stored false; deprecated 'use OnStatusChanged'; |
|
Warning: this symbol is deprecated: use OnStatusChanged |
property OnPlayerBestScoreReceived: TPlayerBestScoreEvent read FOnPlayerBestScoreReceived write FOnPlayerBestScoreReceived; |
|
Event received in response to RequestPlayerBestScore. |
property OnSaveGameChosen: TSaveGameChosenEvent read FOnSaveGameChosen write FOnSaveGameChosen; |
|
Event received in response to ShowSaveGames. |
property OnSaveGameLoaded: TSaveGameLoadedEvent read FOnSaveGameLoaded write FOnSaveGameLoaded; |
|
Event received in response to SaveGameLoad. See TSaveGameLoadedEvent for documentation of parameters. |
Generated by PasDoc 0.16.0.