I have an adventure game Alexa skill that has some user input (called intents in Alexa speak) variables, such as playerName. I also have an array of nodes, which are essentially the script of the game, with pointers to the node based on a user's decision. These messages occasionally use variables like playerName. I am having trouble updating those messages with the playerName once that is provided. I'm assuming it's some sort of asynchronous callback I need to do, but I'm not really understanding how I can do this.
My question is: how can I get playerName to update in the node messages after I receive it from the player?
var playerName = "";
//... other variables here such as asl, etc.
// Questions
var nodes = [{
"node": 1,
"message": "What is your name?",
"next": 2
},
{
"node": 2,
"message": `Oh, so your name is ${playerName}. That's nice, where are you from?`,
"next": 3
},
//...more nodes here
]
//...much further down
var askNameHandlers = Alexa.CreateStateHandler(states.NAMEMODE, {
'NameIntent': function() {
var slot = this.event.request.intent.slots.Name.value;
//this is successful! I get the playerName
playerName = slot; //also successful, see below
this.handler.state = states.AGEMODE;
this.emit(':ask', "Hello " + nodes[1].message, playerName);
//using this for debug
//playerName is correctly included in the reprompt here,
//but not nodes[1].message or via original method below
helper.yesOrNo(this, 'next');
//this is where the issues occur. This helper method essentially fetches
//the next node that corresponds to the 'next' id value, 3 in this case
},
//...more intents, more handlers, etc.
});
So my issue is I am getting the value from the NameIntent, but when I assign it to playerName, it is not showing up in the message, I get the "" default.
I've looked into this and it seems it's an asynchronous issue (callback maybe?) since obviously I am getting the name much later, but I don't really know how to implement this since I'm new to Alexa development and asynch in javascript, although I don't think Alexa knowledge is required to figure this out.