Sign In Register

How can we help you today?

Start a new topic

Trying to understand messages

 I am trying to create a simple event and I am unable to understand the examples here:

https://docs.gamesparks.com/tutorials/multiplayer/sharing-data-between-players-using-game-data-service.html


My goal: A player sends a JSON with information to other player. I created an event named "SendCreature". This event contains the fields:

  • FromPlayer (string)
  • CreatureData (JSON)
  • ToPlayer (string)
  • ExpirationDate (string)


All used in script. Then, I saved and went to Cloud code to script the behavior of the SendCreature event.  I am trying to follow the code example but it's extremely hard to understand what's going on, but I tried to imitate it with my properties:

 

var fromPlayer = Spark.getPlayer().getPlayerId();
var toPlayer = Spark.getData().ToPlayer;
var expirationDate = new Date.toISOString();
var creatureData = Spark.getData().CreatureData;

//Create entry and get its data object
var API = Spark.getGameDataService();
var entry = API.createItem("CreaturesSent", fromPlayer + "-" + toPlayer);
var data = entry.getData();

data.fromPlayer = fromPlayer;
data.toPlayer = toPlayer;
data.expirationDate = expirationDate;
data.creatureData = creatureData;
data.playerID = fromPlayer;

//Persist and return any errors
var status = entry.persistor().persist().error();

if(status){
    Spark.setScriptError("ERROR", status);
}


Well, it gives a warning in the status parameter and it just doesn't run. But I cannot debug it because I don't even know what's going on in the originall script. So, questions:


1- Why the parameters I created in the event does not appear in the autofill? Does it mean that I cannot access the parameters from Spark.getData()?

2- where does the data.ghostData come from? The only reference of that parameter I see in the example code is the one on the top, but it is already assigned. What is that data field?

3- The example creates an item named "raceData". Where does this name come from? I suppose here I am creating an item in the database, but it's opaque what's doing this.

4- What is the persistor thing about? Seems like I am forcing an error.

5- Why my script is failing? The error I get is:


TypeError: org.mozilla.javascript.Undefined@2f30d38a is not a function, it is undefined. (event/SendCreature.js#10)


6- What is the ID required by the data explorer to see what do I have in "CreaturesSent"?

7- Am I doing it correctly? The documentation is confusing with so many options but very little where to know how to. Maybe using messages is the wrong way to send this kind of data. I need to let the player send a bunch of information to other player (probably a friend in the friend list), like sending lives to a friend in candy crush or similar games.


ghostData and raceData are names from the example you linked. 


Did you really save your script to that event, because it looks like you saved the example to it and not your actual script? Please check your event/SendCreature.js again. Maybe you forgot to save?

ghostData and raceData are questions I have about the example.

I don't understand where the data.ghostData parameter comes from or where/when the raceData was created.

Alright, understood. Let's get into this:


1. Why do your parameters not appear in the "autofill"?


You mean in the test harness? You need to make sure you define them properly in the event, like in the screenshot below. Then they will show up in the test harness.


image


2. Where does the data come from?


In your code the "ToPlayer" and the "CreatureData" come from the client. So if you are using unity for example, you would do something like this:

 

private string _yourCreatureDataJson;

private void SendCreatureDataRequest()
{
    new LogEventRequest().SetEventKey("SendCreature")
        .SetEventAttribute("ToPlayer", "UserIdOfOtherPlayer")
        .SetEventAttribute("CreatureData", _yourCreatureDataJson)
        .Send(OnCreatureDataResponse);
}

private void OnCreatureDataResponse(LogEventResponse response)
{
    // do what you want with the data you send back
}

 

3. Where does the name "raceData" come from?


See where you call "CreateItem"? The first parameter is the name of the data type that you are using. If it is missing, it will be created and you can then find it in the Data Explorer under "Data Types". 


4. What is the persistor thing about?


Yeah, I don't really like the syntax here either. It looks weird.

 

var status = entry.persistor().persist().error();

So you have your data type entry and from that you get the persistor. The persistor is responsible for writing your entry to the database. By calling persist() you are telling it to do the "standard save", even though there are other methods you could use for more special cases. Saving with persist() gives you a result object and that one has only one method on it called error(). This one gives you the actual error message. If it is undefined, then no error has happened and all is fine.


You could also write it like this:


 

var saveManager = entry.persistor();
var saveResult = saveManager.persist();
var errorMessage = saveResult.error();

if(errorMessage !== undefined){
    // we have an error
    Spark.setScriptError("ERROR", status);
} 

5. Why is my script failing?



var expirationDate = new Date.toISOString();

 needs to be

 

var expirationDate = new Date().toISOString();


6. What is the id in the data explorer?


It is the second parameter of your "CreateItem" call, so  

 

fromPlayer + "-" + toPlayer

Be careful though. This id has to be unique per data type. Usually we are advised to use a player id for it, it becomes a bit more difficult if you want to create your own ids. There are things you can do with timestamps and such. You can also define data indexes to perform additional queries. I have added the tutorial link for those in the end.


7. Am i doing it correctly?


Well, in the basic implementation you are doing it fine. There was just one syntax error in it and that's not bad. One thing that does not make sense in your data type is that data.fromPlayer and data.playerID are the same value. You are also setting the expirationDate to the current date, so it would expire immediately.. 


I would advise these things for now:


I think that's it for now. Man, what a writeup. I hope it helps. :-P

Login to post a comment