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
onReady
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:
- In Throw you are presented with 2 choices. You choose Spare 1 by clicking the corresponding button.
- The node Spare 1 (ID
node_spare1
) will start. - The
score
variable value will be incremented by 1.
This happens because we provided the// 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 });
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, };
- The node Spare 2 (ID
node_spare2
) will start and thescore
variable value will be incremented by 3, repeating the process from the previous step. - Upon the push of
node_summary
(Summary) to the playlist, the value ofscore
will be checked to determine which node will come next.
In this case the next node will be// 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. });
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.