Online
Online system provides the access to player user profile, friends list, achievements, online presence, cloud saves and more which are exposed to desktop, mobile and console games on various online platforms such as Steam, Xbox Live or PlayStation Network.
Online Platform Interface
Flax contains IOnlinePlatform
interface designed for online platform providers for communicating with various multiplayer services such as player info, achievements, game lobby or in-game store. Each Online Platform implements this interface and is provided via plugin which can be used by your game project:
Online Platform Setup
In your game code setup the Online system with Online Platform (eg. when game opens the main menu). Depening on the runtime platform you can pick the online service to use (eg. Xbox Live on Xbox, Google Play on Android and Steam on Windows/Mac/Linux):
using FlaxEngine.Online;
using FlaxEngine.Online.Steam;
var platform = platform = new OnlinePlatformSteam();
Online.Initialize(platform);
Online Platform Usage
Once Online
system is setup it exposes the current online platform to be used across the whole game project. Here is an example script that gets some basic information from online platform:
using FlaxEngine.Online;
// Setup
var platform = Online.Platform;
if (platform == null)
{
Debug.LogError("Missing Online Platform");
return;
}
if (platform.UserLogin())
{
Debug.LogError("Failed to login to Online");
return;
}
// User profile
platform.GetUser(out var user);
Debug.Log("User: " + user.Name + ", state: " + user.PresenceState);
// User friends info
platform.GetFriends(out var friends);
Debug.Log("Friends: " + friends.Length);
foreach (var f in friends)
Debug.Log(" Friend: " + f.Name + ", state: " + f.PresenceState);
// Achievements
platform.ResetAchievements();
platform.UnlockAchievementProgress("ACH_WIN_100_GAMES", 40);
platform.GetAchievements(out var achievements);
Debug.Log("Achievements: " + achievements.Length);
foreach (var a in achievements)
Debug.Log($" Achievement: {a.Name}, Progress={a.Progress}, IsHidden={a.IsHidden}, desc: {a.Description}");
// Savegames
platform.GetSaveGame("savegame_0", out var saveGame);
if (saveGame != null && saveGame.Length != 0)
Debug.Log("Has savegame of size: " + saveGame.Length);
else
Debug.Log("No savegame");
platform.SetSaveGame("savegame_0", new byte[] { 1, 2, 3, 4 });
Note that Online API is sync and always returns true
on failure. In some cases, such as player freiends list reading, it might take a bit to download this information from online service provider thus you might consider using it asynchroniusly (eg. from async task or a custom thread). To learn about multithreading see this page.
Finally, many of the Online API methods contain the last argument as User localUser
which is null
by default. It can be used to redirect Online Platform for a specific local player which is usefull in case of local-multiplayer games. The default value null
can be used to access the first player info.