Sign In Register

How can we help you today?

Start a new topic
Answered

Obtain formatted messages using specified format string and arguments.

Hello,


as far as I can see your API does not provide a method to get formatted messages in cloud code for later use? 


Imagine you have a message with some arguments you can pass on runtime, something like "${name} is awesome!". Now I need to obtain the formatted message in Cloud Code. Is there a method like the following to get such a string?    

var msg = Spark.message("SOME_SHORT_CODE")
.setMessageData({"name":"GameSparks"})
.getFormattedMessage();

// returns "GameSparks is awesome!" 

 

Thanks

Artem  






Best Answer

Hi Artem,


Yes you can do this but your syntax is slightly off here. If you navigate to your custom scriptMessage "SOME_SHORT_CODE" you can add the following to the default message field to get the name value.


"${data.name} is awesome!"


 

var myMessage = Spark.message("SOME_SHORT_CODE")

    //send to the current player to test
    myMessage.setPlayerIds(Spark.getPlayer().getPlayerId())
    myMessage.setMessageData({"name":GameSparks"})
    myMessage.send()

 


This should result is the value for "name" in the message data be substituted in the message. If you send that you should get the "GameSparks is awesome!" message. I hope that helps. If you have any further questions just let us know.


Thanks,

Liam


1 Comment

Answer

Hi Artem,


Yes you can do this but your syntax is slightly off here. If you navigate to your custom scriptMessage "SOME_SHORT_CODE" you can add the following to the default message field to get the name value.


"${data.name} is awesome!"


 

var myMessage = Spark.message("SOME_SHORT_CODE")

    //send to the current player to test
    myMessage.setPlayerIds(Spark.getPlayer().getPlayerId())
    myMessage.setMessageData({"name":GameSparks"})
    myMessage.send()

 


This should result is the value for "name" in the message data be substituted in the message. If you send that you should get the "GameSparks is awesome!" message. I hope that helps. If you have any further questions just let us know.


Thanks,

Liam


Login to post a comment