0

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.

mosher912
  • 13
  • 5
  • Yes, it's a [timing issue](http://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron?rq=1). Just create the `nodes` inside the callback function? What other code is using them? – Bergi Mar 30 '17 at 16:32
  • @Bergi I have other states with their handler with their own sets of intents, and they are all looking at this nodes data. I guess I could consolidate them to just one state and handler, but then Alexa might hear for example an AgeIntent when I only want her to expect a NameIntent like in this case. The states also allow me to implement the intents in different ways depending on state. – mosher912 Mar 30 '17 at 17:09
  • You can also keep a global state in the variables and just update them (pushing a new node whenever a `NameIntent` occurs or so), but then you have to consider what should happen in those other states when the `nodes` are still empty. – Bergi Mar 30 '17 at 17:12
  • @Bergi I'm not sure I'm following. If you're talking about pushing a new node, problem then becomes if I reference something like playerName but I assigned it several steps back if I'm understanding you correctly. Even so, what if I need playerName in another intent? Then I feel the same async issue would arise. Also, I don't know what you mean by global state in the variables. – mosher912 Mar 30 '17 at 17:30
  • I meant that you `push` inside of the handler, where `playerName` is available. But yes, if you need the `playerName` in the other intents, you have to put it in your global state as well; *and* consider the case that it doesn't exist when the steps are called in the wrong order. You always need to handle that case somehow. – Bergi Mar 30 '17 at 17:36

0 Answers0