Value Accumulation

Different paths throughout the project can update a Score which will later drive score-based decisions that will affect the storyline.

The following snippet goes in the Code Panel of your project in Eko Studio, inside the onInit function.
Don’t forget to replace the Node IDs, variable names and values with the ones in your project.

// Variable dataSet
const throws = {
    'node_spare1' : 1,
    'node_spare2' : 3,
    'node_strike' : 10, 
};

// Initialization of the 'score' Variable
player.variables.register({
    name: 'score',
    action: 'sum', // The 'sum' action will cause to variable to accumulate values
    initialValue: 0,
    dataSet: throws
});

// Score-based Decision
player.variables.connect({
    from: 'node_summary',
    to: function(variables) {
        if (variables.score <= 5) {
            return 'node_lose';
        }
        if (variables.score > 5) {
            return 'node_win';
        }
    },
    evaluateOn: 'playlistpush' // This determines when to evaluate the value of the 'score' variable.
});

Code Breakdown

To get a better grasp of how this code works, let’s go through a simple scenerio - step by step:

  1. In Throw you are presented with 2 choices. You choose Spare 1 by clicking the corresponding button.
  2. The node Spare 1 (ID node_spare1) will start.
  3. The score variable value will be incremented by 1.
    // Initialization of the 'score' Variable
    player.variables.register({
     name: 'score',
     action: 'sum', // The 'sum' action will cause to variable to accumulate values
     initialValue: 0,
     dataSet: throws
    });
    
    This happens because we provided the throws dataSet when we registered the variable. This dataSet dictates which amount should be added to the variable’s value, depending on the currently playing node.
    // Variable dataSet
    const throws = {
     'node_spare1' : 1,
     'node_spare2' : 3,
     'node_strike' : 10, 
    };
    
  4. The node Spare 2 (ID node_spare2) will start and the score variable value will be incremented by 3, repeating the process from the previous step.
  5. Upon the push of node_summary (Summary) to the playlist, the value of score will be checked to determine which node will come next.
    // Score-based Decision
    player.variables.connect({
     from: 'node_summary',
     to: function(variables) {
         if (variables.score <= 5) {
             return 'node_lose';
         }
         if (variables.score > 5) {
             return 'node_win';
         }
     },
     evaluateOn: 'playlistpush' // This determines when to evaluate the value of the 'score' variable.
    });
    
    In this case the next node will be node_lose (Lose).

If you’re aiming for a more complex Scoring system, the Variables Plugin API will be of great help. Add some visual representation of the score with a UI Component. Discover how to create a more complete experience by checking out our Docs.

Rate this page: X
Tell us more!