Home Tutorial How to implement Google Play Game Services in Godot using C#

How to implement Google Play Game Services in Godot using C#

by Samee

In this article, we’ll talk about how to integrate Google Play Game Services into Godot using C#. This can be achieved using two approaches. One method involves creating a custom plugin utilizing the available SDK, while the other leverages existing plugins. We’ll focus on utilizing an available plugin to seamlessly implement the service using C#. By following these steps, you’ll be able to integrate Google Play Game Services into your Godot projects effortlessly.

To achieve this, we’ll utilize this plugin (don’t forget to give it a star) and ensure to follow the setup process outlined on its official page. Once the plugin is correctly set up, it should function seamlessly. However, since everything is in GDScript, we need to create a wrapper around it to utilize it within C# code. This approach is not only applicable to this plugin but can also be applied to any other plugin. For simplicity, I’ll create a wrapper for initializing the plugin and SignInClient only. However, implementing other features is equally straightforward, as you’ll see below.

To begin, you can explore all the methods exposed by the plugin by referring to the Documentation Section and locating the GodotAndroidPlugin class. Once you’ve familiarized yourself with the available methods, let’s dive into the implementation process.

Define the GodotPlayGameService class as a partial class inheriting from the Node class in Godot.

public partial class GodotPlayGameService : Node
{
}

Define a property Plugin of type GodotObject with a private setter. This property will be used to store a reference to the Android plugin for the GodotPlayGameService, allowing interaction with it within the class methods.


public partial class GodotPlayGameService : Node
{
     public GodotObject Plugin { get; private set; }
}

Implement the _Ready() method override and Initialize the plugin.

public partial class GodotPlayGameService : Node
{
    // Represents the Android plugin for the GodotPlayGameService.
    public GodotObject Plugin { get; private set; }
    const string plugin_name = "GodotPlayGameServices";
    public override void _Ready()
    {
        if (Plugin == null)
        {
            if (Engine.HasSingleton(plugin_name))
            {
                Plugin = Engine.GetSingleton(plugin_name);
                Plugin.Call("initialize");
            }
            else
            {
                GD.PrintErr("No plugin found.");
            }
        }
    }
}

For the Sign In functionality, we will implement three methods of plugin: isAuthenticated, signIn, and requestServerSideAccess, along with their corresponding signals. Let’s proceed with the implementation process.

 public partial class GodotPlayGameService : Node
 {
     // Represents the Android plugin for the GodotPlayGameService.
     public GodotObject Plugin { get; private set; }
     const string plugin_name = "GodotPlayGameServices";

     public delegate void AuthenticationDelegate(bool isAuthenticated);
     // Event for when the user is authenticated
     public event AuthenticationDelegate UserAuthenticated;

     // Event for when server side access is requested
     public event AuthenticationDelegate ServerSideAccessRequested;
     public override void _Ready()
     {
         if (Plugin == null)
         {
             if (Engine.HasSingleton(plugin_name))
             {
                 Plugin = Engine.GetSingleton(plugin_name);
                 Plugin.Call("initialize");
             }
             else
             {
                 GD.PrintErr("No plugin found.");
             }
         }
         // Connects signals from the AndroidPlugin instance to corresponding methods
         Plugin?.Connect("userAuthenticated", new Callable(this, nameof(OnUserAuthenticatedSignalConnected)));
         Plugin?.Connect("serverSideAccessRequested", new Callable(this, nameof(ServerSideAccessRequestedSignalConnected)));
     }
     /// <summary>
     /// Invokes the UserAuthenticated event with the specified authentication status.
     /// </summary>
     /// <param name="isAuthenticated">The authentication status.</param>
     private void OnUserAuthenticatedSignalConnected(bool isAuthenticated)
     {
         UserAuthenticated?.Invoke(isAuthenticated);
     }

     /// <summary>
     /// Invokes the ServerSideAccessRequested event with the specified authentication status.
     /// </summary>
     /// <param name="isAuthenticated">The authentication status.</param>
     private void ServerSideAccessRequestedSignalConnected(bool isAuthenticated)
     {
         ServerSideAccessRequested?.Invoke(isAuthenticated);
     }
     /// <summary>
     /// Check if the user is authenticated.
     /// </summary>
     public void IsAuthenticated()
     {
         Plugin?.Call("isAuthenticated");
     }

     /// <summary>
     /// Sign in the user.
     /// </summary>
     public void SignIn()
     {
         Plugin?.Call("signIn");
     }

     /// <summary>
     /// Request server side access with the specified server client ID and force refresh token flag.
     /// </summary>
     /// <param name="serverClientId">The server client ID to request access for.</param>
     /// <param name="forceRefreshToken">Whether to force refresh the token.</param>
     public void RequestServerSideAccess(string serverClientId, bool forceRefreshToken)
     {
         Plugin?.Call("requestServerSideAccess", serverClientId, forceRefreshToken);
     }
 }

If you’ve noticed, all methods and signals correspond to the exposed methods of the plugin, making it incredibly easy to work with. This is just a simple demonstration; for further details and a demo, you can check out this GitHub repository.

Check out my other post :

3 2 votes
Article Rating

Related Posts

Subscribe
Notify of
guest
1 Comment
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Kushan panta
Kushan panta
1 month ago

Nice Article

This website uses cookies in order to offer you the most relevant information. Please accept cookies for optimal performance. Accept Read More

1
0
Would love your thoughts, please comment.x
()
x