Dynamic variable name in Set app variable flow function

Hi,

I have three App Variables of Object type called “player1”, “player2”, and “player3”. I also have a Page Variable called “playerIndex” which is a Number.

Using the Set App Variable flow function, I am given the choice to select an App Variable or the Output From Previous Node.

The App Variable choices are set, with a drop down giving me “player1”, “player2” and “player3” as options. What I would like to do is choose the App Variable based on the value of another variable (in this example, the “playerIndex” variable), so it would read like:

App Variable: “player”+playerIndex
(where it would select player2 if the playerIndex variable is “2” etc)

Given the App Variable option in Set App Variable flow function is hard coded, I assume I must use the Output From Previous Node option instead.

My question is: what is the best way to do this? Do I need a JS module prior to this Set App Variable that returns the dynamically-generated name? If so, what format should it be in? Text?

Thanks!

Hi!

How about if you have only one app variable called players, which is a list (array) of objects, instead of having a variable for each player separately? This would give you a lot of flexibility, as you can have any number of players, and it would be easy to dynamically select a player by its index!

To get the current player object, you could use this kind of formula:

appVars.players[pageVars.playerIndex]

or equivalent, if you prefer:

PICK_ITEM(appVars.players, pageVars.playerIndex)

IMPORTANT: In this case, the indexing starts from zero! So the 1st player is appVars.players[0], 2nd is appVars.players[1] etc. If playerIndex starts from 1, then you could write:

appVars.players[pageVars.playerIndex - 1]

Updating the current player now, of course, means that you need to update the whole list, but that can still be done with a formula! Here’s just one example how to update e.g. a score property of the current player (at playerIndex):

  1. Add “Set app variable” flow function
  2. Set variable name to players
  3. Set the assigned value to the formula, which goes through all the players, updating only the one at the desired index:
    MAP<player>(appVars.players, IF(index == pageVars.playerIndex, SET_KEY(player, "score", 12345), player))
    
    Just replace 12345 with the new score.

The same effect can be achieved with other kind of formula functions as well! We are actually currently planning to add a couple of functions, that would allow you to write even shorter formulas.

How do you feel about this suggestion?

Bonus: having all the players in one array will also have additional benefits, for example, you can easily get the players ordered by their score, winner first:

SORT_BY_KEY(appVars.players, "score", "desc")

You could use this formula in a component repeat when listing the results of a game.

This is exactly what I needed, thanks!! And the bonus suggestion is perfect.

I went ahead and did as you suggested, creating one ‘master’ Player variable that holds an array of player objects, and the referencing as you’ve laid out works great.

One syntax question: what does the ‘’ do? Is it needed? At first glance I am reading as if you were creating a new variable that is then referenced in the formula, but the AppGyver documentation doesn’t really cover this (or at least I couldn’t find it).

Glad if I could help!

Sorry, what do you mean? Is there a word missing here?

My guess is that you were asking about MAP<player>? No, it’s not required, you can instead write this:

MAP(appVars.players, IF(index == pageVars.playerIndex, SET_KEY(item, "score", 12345), item))

So the difference is that you use the word item instead of player, which is the default for the MAP function. This special syntax allows you rename the word item if it is too ambiguous. This might not be covered well in documentation yet.

That’s what it was, yes. For some reason the posting got rid of the < (less than) and > (greater than) as well as the text in between. I think I’ll use item then!