Player

There are several methods associated with the person playing your game. These are all accessible with:

// Note this is just an attribute, not a method
var player = Clay.Player;
Login

This allows you bring up the login modal to start the player login process.

Clay.Player.login( function( response ) {
    // Function that is called on successful login/signup, or failed login/signup
    // response is an object { success: boolean, error: "truthy" }
} );
Signup

This allows you bring up the signup modal to start the player login process.

Clay.Player.signup( function( response ) {
    // Function that is called on successful login/signup, or failed login/signup
    // response is an object { success: boolean, error: "truthy" }
} );
onLogin

Say you want to get the username of the user currently logged in as soon as it's available. To do that, use the Clay.Player.onLogin() method:

    var name = '';
    var identifier = '';
    Clay.Player.onLogin( function( response ) {
        name= response.name; // name or 'Anonymous' if none
        identifier = response.identifier; // unique id associated with user (even if anonymous)
    } );
Detect If "Logged In"

A player is considered logged in if they have either a) signed in with their Clay.io account, or b) entered their name into the system.

To detect if a player is logged in, use the boolean attribute:

// true = logged in, false = not logged in
if( Clay.Player.loggedIn; )
{
    // Do something
}
Clearance

You can check how a player is logged in with Clay.Player.clearance. It is set to "site" by default, and "clay" if they are logged into Clay.io

Purchase Game

If your game is not a free game, you can use the method Clay.Player.purchaseGame( callback ) to start the pay flow for your game. The callback parameter is optional.

Require Login

If you want to force your players to have given either their name or logged into Clay.io, you can use Clay.Player.requireLogin( callback, requireClay ). If they're already logged in, the callback function will be called right away:

var requireClay = false; // If you pass true for 2nd param, it will make sure they've logged into clay (not anonymous)
Clay.Player.requireLogin( function( response ) {
    // Function that is called on successful login, or failed login
    // response is an object { success: boolean, error: "truthy" }
}, requireClay );
Fetch Items

You can find all the items the user has in your game with the method:

Clay.Player.fetchItems( function( arrayOfItems) {
    // Returns an array of item objects the user has
    // An item object has the properties: id (the item id) and quantity (the amount of that item that have)
    console.log( arrayOfItems );
}
Grant and Remove Items

Often times the granting and removing of items will be done through the Payments aspect of the API, but you can also do each of these manually with:

Clay.Player.grantItem( { item_id: item_id, quantity: quantity }, function( response ) { console.log( response ); } );

and

Clay.Player.removeItem( { item_id: item_id, quantity: quantity }, function( response ) { console.log( response ); } );
Has Installed

You can check if the user has installed your game with Clay.Player.hasInstalled(). An installed game means they have a) If it's a free game, added it to their list of games. b) If it's a paid game, have purchased:

Clay.Player.hasInstalled(); // true if "installed" false otherwise.

Some additional options like requiring game players to login to Clay.io to post their high score, or hiding/showing the Clay.io login window on load are available to set in the developers' area in your game settings.

Provide Feedback

We take customer support, and the quality of our developer tools and documentation very seriously. We want to hear how you think we can improve our documentation! Let us know if anything is missing, or unclear on this documentation page, and we'll get that fixed!

1406221281