Sign In Register

How can we help you today?

Start a new topic
Answered

Best way to send message to any player?

Hi there,


I'm really confused about how to have one player send a message to another. A lot of the messaging documentation seems to be deprecated and the one that is not, message(string extCode), doesn't make some things clear: how to use the extCode and if it can contain a player's message.


What I'm trying to do is allow any player to send a message to another. Preferably even if they are not friends but from what I read that could be required with some out of the box function?

The desired use cases would be,

  1. I can call at any time while logged in to retrieve my messages
  2. When a new message is sent to me, my messages are re-retrieved get the new message along with my old ones
  3. I can delete / hide my messages

Could you help point me in the right direction in case I missed it or give me an example?

Best Answer

Hey There,

Sorry about this, but our message documentation seems to be confusing some people, but you are right in thinking Spark.message is the correct message type.
The extCode of the message is the short-code for the script message you create in the portal.


So, if you would like to setup a template message in a script-message you can, but the general behavior is just to classify this as a type of message an then add in the specific conversation and other data you would like.


For example...



I create a new script message called "AWARD_MESSAGE"


Then i create a new form which is given information that the client will interperate (i.e. the template in the actual script-message is meaningless for me).


var messageForm = {};

messageForm.points =10;

messageForm.rewardItem = "armor";

messageForm.message = "Hello World!";


Then i need to populate my player-ID list for the players i want send this message to, in my example i'll use the current player.


var playerIDList = [];

playerIDList = Spark.getPlayer().getPlayerId();


And finally, you send the message...


 Spark.message("AWARD_MESSAGE").setMessageData(messageForm).setPlayerIds(playerIDList).send();


Hope that helps,
Sean


Answer

Hey There,

Sorry about this, but our message documentation seems to be confusing some people, but you are right in thinking Spark.message is the correct message type.
The extCode of the message is the short-code for the script message you create in the portal.


So, if you would like to setup a template message in a script-message you can, but the general behavior is just to classify this as a type of message an then add in the specific conversation and other data you would like.


For example...



I create a new script message called "AWARD_MESSAGE"


Then i create a new form which is given information that the client will interperate (i.e. the template in the actual script-message is meaningless for me).


var messageForm = {};

messageForm.points =10;

messageForm.rewardItem = "armor";

messageForm.message = "Hello World!";


Then i need to populate my player-ID list for the players i want send this message to, in my example i'll use the current player.


var playerIDList = [];

playerIDList = Spark.getPlayer().getPlayerId();


And finally, you send the message...


 Spark.message("AWARD_MESSAGE").setMessageData(messageForm).setPlayerIds(playerIDList).send();


Hope that helps,
Sean

Awesome! So sending works but how do I retrieve them?

Using ListMessageRequest doesn;t seem to return anything.

Hi

You will need to set up a listener

For example in Unity 


GSMessageHandler._AllMessages = (GSMessage e) => {

    Console.WriteLine ("ALL HANDLER " + e.MessageId);

}


Hope that helps

Katie


Awesome! That'll does it perfectly.

I really have to say you guys have top notch customer support. I've been talking about gamesparks all over GDC.

Quick question, I'm also trying out SendFriendMessageRequest but I get an error "NOT_FRIEND"

How do I add a friend and / or accept one?


If friend request acceptance required to send / receive a message?

If so, is there a way to automatically trigger that friend request acceptance on a friend request received?

Hi, I tried Spark.message and I would like to test it in the "Test harmless", but it seems not working


Here my code

Spark.message("CONNECTED_PLAYERS").setMessageData(documentToSend).setPlayerIds(arr_playerNotified).send();


documentToSend and arr_playerNotified are not null.


and the code for the listener in Unity don't work either


PS : Sorry for my engish

@On More Turn

If the playerId is not recognized as a friend then you will get this error.
if you are using social authentication like FB, you have no control over adding friends, FB will do that for you when you have friends registered on the same app.
It might be a good idea to create your own system for adding friend through cloud-code though.

Sean

 

@Team Savie

Can you share you code for sending the message and for setting up the listener in unity?

Thanks,
Sean

 

Hi, thanks, but after more tests, it work

I have spent all day trying to figure out how to get a damn ScriptMessage working.


The documentation is horrible on this. There is no clear start to finish tutorial. After combing through countless Q&A I still cant get it working. I get zero errors from Unity or GameSparks, it just doesn't work. A programmers nightmare. I can usually figure this stuff out but there are just to many unknown variables that are not explained.


I'm using unity 5.3.4f1.


CloudCode called with LogEventRequest:

    

var spells = Spark.runtimeCollection("Spells"); // get the collection data
var thisChallenge = Spark.getChallenge(Spark.data.challengeInstanceId);
var ChallerPlayer = Spark.loadPlayer(thisChallenge.getChallengerId());
var ChalledPlayer = Spark.loadPlayer(thisChallenge.getChallengedPlayerIds()[0]);
var playerIDs = [];
var spellName = Spark.getData().spellName;

playerIDs = ChallerPlayer; ChalledPlayer;

var Spell = spells.findOne({
    "spellName": spellName
    });
Play();

function Play(){
    
    if(ChallerPlayer === Spark.getPlayer()){
    var challengedData = thisChallenge.getScriptData("Challenged_Data");
        
    challengedData.playerHealth = challengedData - Spell.spellDamage;
    thisChallenge.setScriptData("Challenged_Data", challengedData);
    
        var messageForm = {};
messageForm.challengerHealth = challengedData.playerHealth;
messageForm.mana = challengedData.playerMana;
messageForm.message = "Hello World!";
    // Spark.sendMessageExt({"ChallengeData": thisChallenge}, "Challenge", playerIDs);
    // var messageData = Spark.message.setScriptData("Challenged_Data", challengedData);
    Spark.message("Challenge").setMessageData(messageForm).setPlayerIds(playerIDs).send();
    // Spark.SendMessageExt({"Challenger_Data": playerHealth},"Challenger_Data", playerIDs).Send();
    }
    else{
    var challengerData = thisChallenge.getScriptData("Challenger_Data");
        
    challengerData.playerHealth = challengerData.playerHealth - Spell.spellDamage;
    thisChallenge.setScriptData("Challenger_Data", challengerData);
    
    var messageForm = {};
messageForm.challengerHealth = challengerData.playerHealth;
messageForm.mana = challengerData.playerMana;
messageForm.message = "Hello World!";
    
    // Spark.sendMessageExt({"Challenge": thisChallenge}, "Challenge", playerIDs);
    // var messageData = Spark.message.setScriptData("Challenger_Data", challengerData);
    Spark.message("Challenge").setMessageData(messageForm).setPlayerIds(playerIDs).send();
    }
}

   My Unity Code that adds the listener:

  

using UnityEngine;
using System.Collections;
using GameSparks.Core;
using GameSparks.Api.Messages;
using GameSparks.Api.Requests;
using GameSparks.Api.Responses;
using GameSparks.Api;

 I'm using these all to ensure its not the issue.

In the awake function:

 

ScriptMessage.Listener += GetMessages;

 

Then a function:

 

	public void GetMessages(ScriptMessage message){
		Debug.Log ("received message");
		if (message.ExtCode == "Challenge")
		{
			Debug.Log ("received Challenge Data");
		}
	}

 What I'm trying to do is cast a spell on another player. The battle is 1v1 and there are no turns, players cast as their spells and mana allow, first to die loses. Everything appears to be working properly but the message is not being heard by Unity so I cannot get the updated health and mana of the damaged player. I know that all the game logic doesn't make sense right now I'm just trying to get the damn message working.


I tried using:

GSMessageHandler._AllMessages = (GSMessage e) => {
    Console.WriteLine ("ALL HANDLER " + e.MessageId);
}

 

in Unity instead of:

 

ScriptMessage.Listener += GetMessages;

 

but it caused errors when placed in the awake method and I have no idea where to put it. Wheres the documentation!?!


I love that you guys are active on these forums but why develop an excellent platform and not document it properly.

***EDIT***


I'm calling LogChallengeEventRequest not LogEventRequest.


Also, I created the Script Message Extension with:



Do I need to set the Device Types? I tried setting it to IOS and Android and also leaving it blank, same result, nothing.


I tried a many variations of the top four bools with the same result, nothing.

***EDIT***


I tried to use:


 

void Start(){
GameSparksApi.GameSparksMessageReceived  += HandleGameSparksMessageReceived;

}


 In the start method and declared:

 

void HandleGameSparksMessageReceived (object sender, GameSparksMessageReceivedEventArgs e)

{

Debug.Log(“MSG” + e.Message);

}

 But its still throwing errors at me as though it doesn't recognize the GameSparksApi.GameSparksMessageReceived function.

So I got the message working. My problem was with my playerIDs[]. I wasn't declaring them properly. The proper declaration is:


 

var playerIDs = [];
playerIDs = [ChallerPlayer.getPlayerId(), ChalledPlayer.getPlayerId()];

 

Other than that, I'm using: 

ScriptMessage.Listener += GetMessages;


 In the Awake function and:

  

	public void GetMessages(ScriptMessage message){
		Debug.Log ("received message");
		if (message.ExtCode == "Challenge")
		{
			Debug.Log ("received Challenge Data");
		}
	}

 And its working perfectly.


My question now, is, is there a way to save a challenge to a SparkMessage? All of my needed data is saved to my challenge but I need that data to be sent with my SparkMessage. I can re save that data to the message but I would prefer to keep it clean and just save the challenge to the message then access the challenge data in Unity. I tried:

Spark.message("Challenge").setMessageData(thisChallenge).setPlayerIds(playerIDs).send();


 Whereas thisChallenge is set to the challenge object I'd like to save to the message. This gave me an error along the lines of (GSdata obj cannot be converted to MongoDB or something like that.


Thanks for any help. Sorry for the disgruntled posts, its been a long day.

In setPlayerIds(array_of_player_ids), do all of the player ids in the array need to be ids of friends of the sender? What happens if they are not friends?

Login to post a comment