Possibilities of Channels
Overview
Based on channels, you can implement various options for communication and interaction between players: chats and guilds, lists of friends and activity feeds, groups, lobbies and rooms. Channels can be configured quite flexibly, with their help it is possible to create turn-based battles and even organize transfer of gifts to players.
The possibilities of using channels are huge and depend only on your ideas and imagination
To start working with channels, go to control panel, select the added game. From the menu on the left, select Channels - Add Channel, for details, see Manage Channels.
Players can create channels themselves. To do this, you need to create a channel template in the control panel: Channels - Add Template, see the Templates section for details:
Consider the concept of using channels using the example of:
- 🌳 creating a location chat
- 📢 channel change implementation
- ⚔️ creating turn-based battles
Chat of Location
For example, a developer has created a chat called Dragon Peak
with chat ID ID: 10
. When entering the game, each player connects to the server and waits for notifications from other players or the administrator.
The SDK does this automatically
If a player wants to go to the Dragon Peak
location, then this can be done by entering the channel:
- JavaScript
- Unity
gp.channels.join({ channelId: 10 });
GP_Channels.Join("10");
Once a player has joined a channel, they can receive notifications in that channel. Presence in the channel can be used as a guarantee of the presence of the player at the location in the game. The player can greet other chat participants by sending a message:
- JavaScript
- Unity
gp.channels.sendMessage({
channelId: 10,
text: 'Guys, hello everyone! Is it very difficult here?',
});
GP_Channels.SendMessage(
channel_ID: 10,
text: "Guys, hello everyone! Is it very difficult here?"
);
In order for other players to receive the message, you need to subscribe to it. When a message arrives, it can be rendered:
- JavaScript
- Unity
// message subscription
gp.channels.on('event:message', (message) => {
// here you render the chat message, example
mygame.displayMessage(message.player.name, message.text);
});
// subscribe to message
private void OnEnable()
{
GP_Channels.OnMessage += OnMessage;
}
// unsubscribe from message
private void OnDisable()
{
GP_Channels.OnMessage += OnMessage;
}
// here you render the chat message, example
private void OnMessage(GP_Data data)
{
var messageData = data.Get<Message_Data>();
Debug.Log("ON MESSAGE EVENT: TEXT: " + messageData.text);
Debug.Log("ON MESSAGE EVENT: PLAYER: NAME: " + messageData.player.name);
}
Next, consider changing the channel using the example of the same location.
Channel Change
Having increased the level, the player is ready to go to the next location House of Kings
with identifier ID: 11
. To do this, you need to exit the previous channel Dragon Peak
and enter the channel House of Kings
:
- JavaScript
- Unity
// exit the Dragon Peak Canal
gp.channels.leave({ channelId: 10 });
// join the House of Kings channel
gp.channels.join({ channelId: 11 });
// exit the Dragon Peak Canal
GP_Channels.Leave("10");
// join the House of Kings channel
GP_Channels.Join("11");
Turn-based Battles
A full-fledged multiplayer through channels will not work, but you can implement a step-by-step mode. The idea is quite simple and is to send messages in the form of a battle log:
> "Player 1234" casts "Righteous Light" on "Zombie 2"
> "Zombie 2" takes 132 damage
> "Zombie 2" attacks "Player 1234"
> "Player 1234" takes 11 damage
Combat log entries in this format are visually understandable, but they can be difficult to work with in game. Let's represent messages as JSON objects:
- JavaScript
- Unity
gp.channels.sendMessage({
channelId: 123,
text: JSON.stringify({
type: 'ATTACK',
spell: 'RIGHTEOUS_LIGHT',
target: 'ZOMBIE_2',
}),
});
// battle log data structure
[System.Serializable]
public class BattleMove
{
string type;
string spell;
string target;
}
BattleLog log = new BattleLog();
log.type = "ATTACK";
log.spell = "RIGHTEOUS_LIGHT";
log.target = "ZOMBIE_2";
string json = JsonUtility.ToJson(log);
GP_Channels.SendMessage(channel_ID: 123, text: json);
Result:
> {"type":"ATTACK","spell":"RIGHTEOUS_LIGHT","target":"ZOMBIE_2"}
In order to react to taking damage, one of the players must assume the role of a host. Usually this role is performed by the player - owner of the channel, it is he who will play the damage and reaction of the monsters.
To do this, subscribe to messages and, if the player is the owner of the channel, then play the battle mechanics:
- JavaScript
- Unity
// message subscription
gp.channels.on('event:message', (message) => {
// battle channel
if (message.channelId === battleChannel.id) {
// Parsing JSON messages
const result = JSON.parse(message.text);
// message type - attack and channel owner our player
if (result.type === 'ATTACK' && channel.ownerId === gp.player.id) {
handleAttackMessage(result);
}
// message type - the result of the attack, notification for everyone, including the owner of the channel
if (result.type === 'ATTACK_RESULT') {
handleAttackResultMessage(result);
}
}
});
// catch attack message
function handleAttackMessage(result) {
// get spell type
const spell = getSpell(result.spell);
// get the target of the spell
const target = getTarget(result.target);
// calculate damage
const damage = spell.damage - target.armor;
// notify everyone that the target has been damaged
gp.channels.sendMessage({
channelId: battleChannel.id,
text: JSON.stringify({
type: 'ATTACK_RESULT',
target: 'ZOMBIE_2',
damage: damage,
}),
});
}
// catch the attack result message
function handleAttackResultMessage(result) {
// get the target of the spell
const target = getTarget(result.target);
// change target's HP value to damage
target.hp -= result.damage;
}
// battle log data structure example
[System.Serializable]
public class BattleLog
{
public string type;
public string spell;
public string target;
public int damage = 0;
}
// target data structure example
public class TargetData
{
public int hp;
public int armor;
}
// spell data structure example
public class SpellData
{
public string spellName;
public int damage;
}
// subscribe to message
private void OnEnable()
{
GP_Channels.OnMessage += OnMessage;
}
// unsubscribe from message
private void OnDisable()
{
GP_Channels.OnMessage += OnMessage;
}
private void OnMessage(GP_Data data)
{
var messageData = data.Get<Message_Data>();
// battle channel
if(messageData.channelId == battleChannel.id) {
// Parsing JSON messages
BattleLog result = JsonUtility.FromJson<BattleLog>(message.text);
// message type - attack and channel owner our player
if (result.type == "ATTACK" && messageData.authorId == GP_Player.GetID()) {
HandleAttackMessage(result);
}
// message type - the result of the attack, notification for everyone, including the owner of the channel
if (result.type == "ATTACK_RESULT") {
HandleAttackResultMessage(result);
}
}
}
// catch attack message
private void HandleAttackMessage(BattleLog result)
{
// get spell type
SpellData spell = GetSpell(result.spell);
// get the target of the spell
TargetData target = GetTarget(result.target);
// calculate damage
int damage = spell.damage - target.armor;
// create battle log
BattleLog log = new BattleLog();
log.type = "ATTACK_RESULT";
log.target = "ZOMBIE_2";
log.damage = damage;
// put battle log into JSON
string json = JsonUtility.ToJson(log);
// notify everyone that the target has been damaged
GP_Channels.SendMessage(channel_ID: battleChannel.id, text: json);
}
// catch the attack result message
private void HandleAttackResultMessage(BattleLog result) {
// get the target of the spell
TargetData target = GetTarget(result.target);
// change target's HP value to damage
target.hp -= result.damage;
}
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!