Sign In Register

How can we help you today?

Start a new topic
Answered

Adding Numbers in Cloud Code (Doing Math)

Im simply trying to add two values together in cloud code then save them to my message's script data.


I have everything working with subtracting but now that i'm trying to add i'm getting errors that say the data list i'm saving to is null when I try to access it in Unity. I've narrowed it down to the attempted + command.


  

            challengedData.playerHealth = challengedData.playerHealth - Spell.spellDamage;
            challengerData.playerMana = challengerData.playerMana - Spell.manaCost;
            challengerData.playerHealth = challengerData.playerHealth + Spell.spellHeal;
            challengerData.playerMana = challengerData.playerMana + Spell.manaReplenish;

 The "-" commands work as expected. The "+" commands cause the challengerData.playerHealth to be null.


All works when the two bottom lines are commented out.

So... How do I add values together in cloud code? 


Best Answer

Hi Ian,


Have you tested this through the Test Harness, or solely via the client? 


If it's being done through the client, and you're using 'response.ScriptData.getInt("challengedData.playerHealth") or something similar; it's possible that the value itself isn't null, but merely of the wrong type, which would result in the 'getInt' function returning a null value. For example, if one of the values you're attempting to add is stored as a string, it would result in a string concatonation rather than an addition, for example:


x = "10" + 10 //result will be "1010" - a string.


Testing this event via Test Harness will let you see what value is being returned for playerHealth.


Could you check your initial values and confirm that neither of them are strings. If this does indeed turn out to be the case, you can resolve this either by altering the initial values wherever they're being set so that they're both ints, or by parsing the string to int when adding, e.g.


 challengeData.playerHealth = challengeData.playerHealth - parseInt(Spell.spellHeal);


Try this out and let me know if it resolves the issue for you.


Regards,

Vinnie


***EDIT***


So I got this working with:

            challengerData.playerHealth = challengerData.playerHealth - Spell.spellDamage;
            challengedData.playerMana = challengedData.playerMana - Spell.manaCost;
            challengedData.playerHealth = challengedData.playerHealth - (-Spell.spellHeal);
            challengedData.playerMana = challengedData.playerMana - (-Spell.manaReplenish);

 

But I feel like this is not the best way to do this...

Answer

Hi Ian,


Have you tested this through the Test Harness, or solely via the client? 


If it's being done through the client, and you're using 'response.ScriptData.getInt("challengedData.playerHealth") or something similar; it's possible that the value itself isn't null, but merely of the wrong type, which would result in the 'getInt' function returning a null value. For example, if one of the values you're attempting to add is stored as a string, it would result in a string concatonation rather than an addition, for example:


x = "10" + 10 //result will be "1010" - a string.


Testing this event via Test Harness will let you see what value is being returned for playerHealth.


Could you check your initial values and confirm that neither of them are strings. If this does indeed turn out to be the case, you can resolve this either by altering the initial values wherever they're being set so that they're both ints, or by parsing the string to int when adding, e.g.


 challengeData.playerHealth = challengeData.playerHealth - parseInt(Spell.spellHeal);


Try this out and let me know if it resolves the issue for you.


Regards,

Vinnie

Nice Vinnie, 

thanks for the excellent explanation.


That explains why the "-" command worked while the "+" command appeared to be failing. In truth the "+" command was working perfectly to add two strings together where there is no use for the "-" in regard to strings so it defaulted to math.


Thanks again!

Ian

Login to post a comment