Callbacks are choices that determine the outcome of later decisions, or even are just referred to, in later scenes.
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 subjects = {
'node_starTrek': 'Picard',
'node_drink': 'Coke',
'node_twilight': 'Team Ed...'
};
// Initialization of the 'subject' Variable
player.variables.register({
name: 'subject',
action: 'set',
initialValue: '',
dataSet: subjetcs
});
// Callback Decision
player.variables.connect({
from: 'node_scene_b1',
to: function(variables) {
if (variables.subject === 'Picard') {
return 'node_picard';
}
if (variables.subject === 'Coke') {
return 'node_coke';
}
if (variables.subject === 'Team Ed...') {
return 'node_teamEd...';
}
},
evaluateOn: 'playlistpush' // This determines when to evaluate the value of the 'subject' variable.
});
Code Breakdown
To get a better grasp of how this code works, let’s go through a simple scenerio - step by step:
- In Scene A you are presented with 3 choices. You choose Drink by clicking the corresponding button.
- The node Drink (ID
node_drink
) will start. - The
subject
variable will be set toCoke
.
This happens because we provided the// Initialization of the 'subject' Variable player.variables.register({ name: 'subject', action: 'set', initialValue: '', dataSet: subjects });
subjects
dataSet when we registered the variable. This dataSet dictates which value should be assigned to the variable, depending on the currently playing node.// Variable dataSet const subjects = { 'node_starTrek': 'Picard', 'node_drink': 'Coke', 'node_twilight': 'Team Ed...' };
- Upon the push of
node_scene_b1
(Scene B1) to the playlist, the value ofsubject
will be checked to determine which node will come next.
In this case the next node will be// Callback Decision player.variables.connect({ from: 'node_scene_b1', to: function(variables) { if (variables.subject === 'Picard') { return 'node_picard'; } if (variables.subject === 'Coke') { return 'node_coke'; } if (variables.subject === 'Team Ed...') { return 'node_teamEd...'; } }, evaluateOn: 'playlistpush' // This determines when to evaluate the value of the 'subject' variable. });
node_coke
(Coke).
If you’re aiming for a more complex Callback, the Variables Plugin API will be of great help.
You can also discover how to create a more complete experience by checking out our Docs.