Player state
Player overview
The player manager is inherited from Player and simply extends it. It includes work with authorization ad synchronization with the server. This part focuses on work with the player state.
Player properties FREE:
- JavaScript
- Unity
// ID
gp.player.id;
// Score
gp.player.score;
// Name
gp.player.name;
// Avatar link
gp.player.avatar;
// Stub – is the player default or the data in its model differs from the default
gp.player.isStub;
// Player fields
gp.player.fields;
// ID
GP_Player.GetID();
// Score
GP_Player.GetScore();
// Name
GP_Player.GetName();
// Avatar link
GP_Player.GetAvatarUrl();
// Stub – is the player default or the data in its model differs from the default
GP_Player.IsStub();
- JavaScript
- Unity
// Get the value of the key field
gp.player.get('gold');
// Set the value of the key field to value, the value is cast to the type
gp.player.set('class', 'warrior');
// Add the value to the key field
gp.player.add('gold', 50);
// Toggle the value of the key field
gp.player.toggle('vip');
// Check if the key field is present and not empty (not 0, '', false, null, undefined)
gp.player.has('vip');
// Return the player state as an object
gp.player.toJSON();
// Set the state from the object
gp.player.fromJSON({
name: 'test player',
score: 4522,
gold: 100,
vip: false,
class: 'warrior',
});
// Reset the player state to default
gp.player.reset();
// Remove the player – reset fields and clear ID
gp.player.remove();
// Get the value of the key field
GP_Player.GetInt("gold");
// Set the value of the key field to value, the value is cast to the type
GP_Player.Set("gold", 100);
// Add the value to the key field
GP_Player.Add("gold", 10);
// Toggle the value of the key field
GP_Player.Toggle("vip");
// Check if the key field is present and not empty (not 0, '', false, null, undefined)
GP_Player.Has("vip");
// Reset the player state to default
GP_Player.ResetPlayer();
// Remove the player – reset fields and clear ID
GP_Player.Remove();
Player events:
- JavaScript
- Unity
// The player state has changed
gp.player.on('change', () => {});
//Subscribe to event
private void OnEnable()
{
GP_Player.OnPlayerChange += OnPlayerChange;
}
//Unsubscribe from event
private void OnDisable()
{
GP_Player.OnPlayerChange -= OnPlayerChange;
}
//On trigger event
private void OnPlayerChange()
{
Debug.Log("Player Change");
}
Player Statistics
FREEYou can retrieve information about the number of days and time spent in the game.
- JavaScript
- Unity
// Number of days in the game
gp.player.stats.activeDays;
// Number of consecutive days in the game
gp.player.stats.activeDaysConsecutive;
// Number of seconds spent in the game today
gp.player.stats.playtimeToday;
// Number of seconds spent in the game overall
gp.player.stats.playtimeAll;
public void PlayerStats()
{
// Number of days in the game
Debug.Log("Active Days:" + GP_Player.GetActiveDays());
// Number of consecutive days in the game
Debug.Log("Active Days Consecutive:" + GP_Player.GetActiveDaysConsecutive());
// Number of seconds spent in the game today
Debug.Log("Playtime Today:" + GP_Player.GetPlaytimeToday());
// Number of seconds spent in the game overall
Debug.Log("Playtime All:" + GP_Player.GetPlaytimeAll());
}
Player fields
Player fields are set in the panel, they describe the player state and store the following properties:
- JavaScript
- Unity
/**
* Field name translated into current language
* @type {string}
*/
field.name;
/**
* Unique field key
* @type {string}
*/
field.key;
/**
* Field type
* @type {'stats' | 'data' | 'flag' | 'service' | 'accounts'}
* stats — numeric fields
* data — string fields
* flags — boolean fields
* service – ID, test, active, remote, etc.
* accounts — vkId, yandexId, okId, etc.
*/
field.type;
/**
* Important field for the player
* If you need to decide who will be left, you can display these fields
* @type {boolean}
*/
field.important;
/**
* Default field value
* @type {string | number | boolean}
*/
field.default;
/**
* Possible variants of values for the fields
* If they are not specified, then the field takes any value
* @type {ModelFieldVariant[]}
*/
field.variants;
public class PlayerFetchFieldsData
{
//Field name translated into current language
public string name;
//Unique field key
public string key;
/**
* Field type
* stats — numeric fields
* data — string fields
* flags — boolean fields
* service – ID, test, active, remote, etc.
* accounts — vkId, yandexId, okId, etc.
*/
public string type;
//Default field value
public string defaultValue; // string | bool | number
/**
* Important field for the player
* If you need to decide who will be left, you can display these fields
*/
public bool important;
/**
* Possible variants of values for the fields
* If they are not specified, then the field takes any value
*/
public Variants[] variants;
}
Field variants can also be set in the panel. Variant properties:
- JavaScript
- Unity
/**
* Variant name translated into current language
* @type {string}
*/
field.name;
/**
* Unique value of the variant
* @type {string | number | boolean}
*/
field.value;
public class Variants
{
//Variant name translated into current language
public string value; // string | number
//Unique value of the variant
public string name;
}
The list of fields is available directly in the player model FREE:
- JavaScript
- Unity
gp.player.fields;
Get by GP_Player.FetchFields()
Methods for work with fields FREE:
- JavaScript
- Unity
// Get the field by key
gp.player.getField('gold');
// Get the translated field name by key
gp.player.getFieldName('score');
// Get the translated name of the field variant by the key and its value
gp.player.getFieldVariantName('rank', 2);
Not implemented
Stay in Touch
Other documents of this chapter available Here. To get started, welcome to the Tutorials chapter.
GamePush Community Telegram
: @gs_community.
For your suggestions e-mail
: [email protected]
We Wish you Success!