Sign In Register

How can we help you today?

Start a new topic

cloud code variable derived from define array object is undefined for some reason

So I'm doing the hearthstone tutorial and getting this error when running the action_pushCard challenge event. The variables are being defined fine until it reaches cardObj, derived from the array playerHand by the index cardName.


playerHand and cardName are defined just fine. But for some reason, cardObj is not? I have a screencap of the error below and the code as well.

 


//Load the challenge
var chal = Spark.getChallenge(Spark.getData().challengeInstanceId);
//Retrieve player Id
var pId = Spark.getPlayer().getPlayerId();

//Retrieve the slot
var cardName = Spark.getData().slotNum;
//Retrieve the current hand
var currentHand = chal.getScriptData("currentHand");
//Retrieve the current player's hand
var playerHand = currentHand[pId];
//Get the cards name or type
var cardObj = playerHand[cardName];

//Retrieve player stats
var playerStats = chal.getScriptData("playerStats");

//Retrieve Mana
var playerMana = playerStats[pId].currentMana;

//Is there enough mana to spawn the card?
if (playerMana >= cardObj.spawnCost) {

    var playerStats = chal.getScriptData("playerStats");
    //New mana
    playerStats[pId].currentMana = playerStats[pId].currentMana - cardObj.spawnCost;

    //Retrieve the playing field
    var playField = chal.getScriptData("playField");

    if (cardObj.effect == "Charge") {
        //Set value in the correct position (playField -> Players ID -> The card's name/number) with correct name
        playField[pId][cardName] = {
            "type": cardObj.type,
            "atk": cardObj.atk,
            "hp": cardObj.hp,
            "maxHP": cardObj.hp,
            "effect": cardObj.effect,
            "canAtk": true
        };
    } else {
        //Set value in the correct position (playField -> Players ID -> The card's name/number) with correct name
        playField[pId][cardName] = {
            "type": cardObj.type,
            "atk": cardObj.atk,
            "hp": cardObj.hp,
            "maxHP": cardObj.hp,
            "effect": cardObj.effect,
            "canAtk": false
        };
    }
    if (cardObj.effect == "Direct Attack") {
        //Determine enemy Id
        if (Spark.getPlayer().getPlayerId() == chal.getChallengerId()) {
            //If equal to challenger, then Id is challenged[0] Id
            var opponentId = chal.getChallengedPlayerIds()[0];
        } else {
            //If not equal to challenger then other player Id is challenger Id
            var opponentId = chal.getChallengerId();
        }
        //Set opponent players' health
        playerStats[opponentId].playerHealth = playerStats[opponentId].playerHealth - cardObj.atk;
    }
    if (cardObj.effect == "Taunt") {
        playerStats[pId].tauntProtection = true;
    }
    //Remove that card from current hand
    delete currentHand[pId][cardName];

    //Return script message with card pushed
    Spark.setScriptData("result", "Card " + cardName + " of type " + cardObj.type + " is on the playing field");

    //Save jsons
    chal.setScriptData("currentHand", currentHand);
    chal.setScriptData("playField", playField);
    chal.setScriptData("playerStats", playerStats);
} else {
    //if there isn't enough mana, send error report through scriptData
    Spark.setScriptData("Error", "Not enough mana");
}

 Thanks for you time!

1 Comment

Hi,


This bug has been encountered before and it's a tricky one to diagnose on the outset. Hopefully this fix will work for you, and if not, I will continue to investigate more on my end.


Basically, when numbers are retrieved through Cloud Code, the JavaScript engine we use (RhinoJS) has some trouble using them with indexing in arrays.


A (silly) workaround for this is to Parse and Stringify the "cardName" variable.


 

cardName = JSON.stringify(JSON.parse(cardName))

 


See if this works for you and if not, I'll look for another solution. I've dealt with this error myself and it's frustrating :)


-Pádraig

Login to post a comment