Sign In Register

How can we help you today?

Start a new topic
Answered

Setting all Variables Types in Cloud Code and Retrieving them in Unity3d using C#

Hi. I'd like this forum to cover saving all variable types to a Spark.message().setMessageData() in cloud code and retrieving them in unity.


First i'll explain what I know. I know how to set up the message in messages tab:


Next you will need to set up the listener in Unity:


  

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

public class GameSparksManager : MonoBehaviour {
		ScriptMessage.Listener += GetMessages;
		ChallengeIssuedMessage.Listener += PlayerChallenged;
		ChallengeAcceptedMessage.Listener += ChallengeAccepted;
		ChallengeStartedMessage.Listener += ChallengeStarted;
	}
	void Start(){
		GSL = GameObject.Find ("Canvas").GetComponent<GameSparkLogin>();
		BI = GameObject.Find ("Battle Instance").GetComponent<BattleInstance> ();
	}

	public void PlayerChallenged(ChallengeIssuedMessage message){
			Debug.Log ("you have been challenged!!! " + message.Who);
//			Debug.Log (message.Challenge.Challenger.Id);
		GSData challer = message.Challenge.ScriptData.GetGSData("Challenger_Data");
		GSL.NewChallenge ((string)message.Challenge.ChallengeId, (string)challer.GetString("playerID"), (string)challer.GetString("playerName"), (string)challer.GetString("playerChar"), (int)challer.GetNumber("playerHealth"), (int)challer.GetNumber("playerMana"));

//		GetData (message.Challenge.Challenger.Id, message.Challenge.ChallengeId, "issued");
	}

	public void ChallengeAccepted(ChallengeAcceptedMessage message){
		Debug.Log ("CHALLENGE ACCEPTED!!! ");


		GSData challenged = message.Challenge.ScriptData.GetGSData("Challenged_Data");
		GSL.StartChallenge(message.Challenge.ChallengeId, (string)challenged.GetString("playerID"), (string)challenged.GetString("playerName"), 
			(string)challenged.GetString("playerChar"), (int)challenged.GetNumber("playerHealth"), (int)challenged.GetNumber("playerMana"), false);

	}

	public void ChallengeStarted(ChallengeStartedMessage message){
		Debug.Log ("CHALLENGE STARTED!!! ");

//		GSData challenged = message.Challenge.ScriptData.GetGSData("Challenged_Data");
//		GSL.StartChallenge(message.Challenge.ChallengeId, (string)challenged.GetString("playerID"), (string)challenged.GetString("playerName"),
//			(string)challenged.GetString("playerChar"), (int)challenged.GetNumber("playerHealth"), (int)challenged.GetNumber("playerMana"));

	}
	public void SpellCast(ScriptMessage message){
		
	}

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

//		if (message.ExtCode == "Challenger_Data")
//		{
//			Debug.Log ("received Challenger_Data");
//		}
	}
}

 I'd really like to be able to access the challenge from my scriptMessage like I do from the built in challengeMessages. 

 

So far I only know how to set individual strings and ints and interpret them in unity using C#.

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;
var beginCasting = Spark.getData().beginCasting;

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

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

function Play(){
    var challengedData = thisChallenge.getScriptData("Challenged_Data");
    var challengerData = thisChallenge.getScriptData("Challenger_Data");
    if(ChallerPlayer === Spark.getPlayer()){
        thisChallenge.setScriptData("challerBeginCasting", beginCasting);
        if(beginCasting === "true"){
    
         }else{
            challengedData.playerHealth = challengedData.playerHealth - Spell.spellDamage;
            challengerData.playerMana = challengerData.playerMana - Spell.manaCost;
            thisChallenge.setScriptData("Challenger_Data", challengerData);
            thisChallenge.setScriptData("Challenged_Data", challengedData);
        }
    }
    else{
        thisChallenge.setScriptData("challedBeginCasting", beginCasting);
        if(beginCasting === "true"){
        
        }else{
            challengerData.playerHealth = challengerData.playerHealth - Spell.spellDamage;
            challengedData.playerMana = challengedData.playerMana - Spell.manaCost;
            thisChallenge.setScriptData("Challenger_Data", challengerData);
            thisChallenge.setScriptData("Challenged_Data", challengedData);
            if(thisChallenge.getScriptData("challerBeginCasting") === null){
                thisChallenge.setScriptData("challerBeginCasting", "false");
            }
        }

    }

    var messageForm = {};
        messageForm.castingTime = Spell.castingTime;
        messageForm.challengerHealth = challengerData.playerHealth;
        messageForm.challengerMana = challengerData.playerMana;
        messageForm.challerBeginCasting = thisChallenge.getScriptData("challerBeginCasting");
        messageForm.challengedHealth = challengedData.playerHealth;
        messageForm.challengedMana = challengedData.playerMana;
        messageForm.challedBeginCasting = thisChallenge.getScriptData("challedBeginCasting");
        messageForm.message = "Hello World!";
    
    Spark.message("Challenge").setMessageData(messageForm).setPlayerIds(playerIDs).send();

 

And then retrieve it in unity using:

  

	public void UpdateBattleInstance(ScriptMessage message){
		currentChallerHealth = (int)message.Data.GetInt ("challengerHealth");
		currentChallerMana = (int)message.Data.GetInt ("challengerMana");
		currentChalledHealth = (int)message.Data.GetInt ("challengedHealth");
		currentChalledMana = (int)message.Data.GetInt ("challengedMana");
		enemyCastTime = float.Parse (message.Data.GetString("castingTime"));
		Debug.Log (enemyCastTime);
		if (message.Data.GetString ("challerBeginCasting") == null) {
			challerBeginCasting = false;
		} else {
			challerBeginCasting = bool.Parse(message.Data.GetString ("challerBeginCasting"));
		}

		Debug.Log (challerBeginCasting);
		challedBeginCasting = bool.Parse(message.Data.GetString ("challedBeginCasting"));
		Debug.Log (challedBeginCasting);

		if(isChallenger){
			if (challedBeginCasting) {
				if (!enemyIsCasting) {
					enemyIsCasting = true;
					Debug.Log ("Enemy Has Begun Casting");
					BWS.EnemyBeginCasting (enemyCastTime);
				}
			} 

		}else{
			if(challerBeginCasting){
				if (!enemyIsCasting) {
					enemyIsCasting = true;
					Debug.Log ("Enemy Has Begun Casting");
					BWS.EnemyBeginCasting (enemyCastTime);
				}
			}
		}

		Debug.Log ("BI isChallenger " + isChallenger);
		if (isChallenger) {
			BWS.UpdateStats (currentChallerHealth, currentChallerMana, currentChalledHealth, currentChalledMana);
			Debug.Log (currentChallerHealth + " " + currentChallerMana + " " + currentChalledHealth + " " + currentChalledMana);
		} else {
			BWS.UpdateStats (currentChalledHealth, currentChalledMana, currentChallerHealth, currentChallerMana);
		}
	}

 

But how do I save a retrieve GSData, ScriptData, JSONData, an entire Challenge object?

 

ScriptMessage.Listener = (message) => {
    GSData data = message.Data; 
    string extCode = message.ExtCode; 
    string messageId = message.MessageId; 
    bool? notification = message.Notification; 
    GSEnumerable<GSData> scriptData = message.ScriptData; 
    string subTitle = message.SubTitle; 
    string summary = message.Summary; 
    string title = message.Title; 
};

 

and:



I know of: 

GSData myGSData = message.Data.GetGSData("name of GSData key");

But how is this set in cloud code?


Please explain what you know in detail, do not try to simplify. I'd like this forum to become a reference for saving all variable types to a scriptMessage and retrieving and applying the data in Unity. Multiple implementation examples are appreciated.


Best Answer

Hi Ian,


The 'Get' function used to retrieve your data in Unity is dependant on the type of data you add to the message's script data.


So, if you were to add an object to the script data, it would be retrieved by using GetGSData. For example:


 

var messageForm = {};
messageForm.castingTime = Spell.castingTime;
messageForm.challengerInfo = {
	"challengerHealth":challengerData.playerHealth,
   	"challengerMana":challengerData.playerMana,
	"challerBeginCasting":thisChallenge.getScriptData("challerBeginCasting"),
	"challengedHealth":challengedData.playerHealth,
       	"challengedMana":challengedData.playerMana,
       	"challedBeginCasting":thisChallenge.getScriptData("challedBeginCasting")
};
messageForm.message = "Hello World!";

 

then this data would be retrieved using:


 

GSData challengerInfo = message.Data.GetGSData("challengerInfo");

 


If you have any more questions please let us know.


Regards,

Vinnie


Answer

Hi Ian,


The 'Get' function used to retrieve your data in Unity is dependant on the type of data you add to the message's script data.


So, if you were to add an object to the script data, it would be retrieved by using GetGSData. For example:


 

var messageForm = {};
messageForm.castingTime = Spell.castingTime;
messageForm.challengerInfo = {
	"challengerHealth":challengerData.playerHealth,
   	"challengerMana":challengerData.playerMana,
	"challerBeginCasting":thisChallenge.getScriptData("challerBeginCasting"),
	"challengedHealth":challengedData.playerHealth,
       	"challengedMana":challengedData.playerMana,
       	"challedBeginCasting":thisChallenge.getScriptData("challedBeginCasting")
};
messageForm.message = "Hello World!";

 

then this data would be retrieved using:


 

GSData challengerInfo = message.Data.GetGSData("challengerInfo");

 


If you have any more questions please let us know.


Regards,

Vinnie

Excellent, thank you Vinnie! This is how I thought it would work, I thought I tried this and it threw an error but there could have been another variable messing things up at that time. Ill try this again and let you know how it goes!


Is it possible to save the entire challenge object to the ScriptMessage?


Thanks, 

Ian

Hi Ian,


Yes, this is possible provided your cloud code has access to id of the challenge you wish to send. It can be done as follows:


var chall = Spark.getChallenge(challengeId);

Spark.setScriptData("myChallenge", chall);


This, again, would be retrieved on the client end using 'GetGSData("myChallenge")'


Vinnie

That simple? I could have sworn I tried this. Ill try again.



Thank you Vinnie!!!


Ian

Login to post a comment