Exit Points

Sometimes you will want to jump to a child node, but not immediately on making the decision or when the parent node ends, but on pre-defined points in time within the parent node.

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

export default function onInit(player, context) {

    // Set the ID of the Node to exit from
    const nodeId = 'node_lecture';

    // Double check that nodeId is valid
    if (!player.repository.has(nodeId)) {
        console.error(`${nodeId} is not a valid node id. Please check the id`);
    }

    // Exit points (in seconds)
    const customExitPoints = [5, 12, 22];

    // Get the object of the Node to exit from
    const nodeToExit = player.repository.get(nodeId);

    // Set an exit for each point in the node's time
    customExitPoints.forEach(function(exitPoint) {
        nodeToExit.on(`timeupdate:${exitPoint}`, exitIfDecisionMade);
    });

    // If the current node's decision was made then play the next node.
    function exitIfDecisionMade() {
        const currentDecision = player.decision.get(player.currentNode);
        if (currentDecision.state === 'made') {
            player.currentNodeIndex += 1;
        }
    }

};

Code Breakdown

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

  1. In Lecture (ID node_lecture) you are presented with the choice of asking a Question.
  2. The Lecture‘s length is about 30 seconds and you listen to it for 10 seconds before asking a question by clicking the corresponding button.
  3. 2 seconds after clicking the button, the Question node will start palying - avoiding interruption of the lecturer in the video. This happens because we set pre-defined points in time in which the playing sequence will “exit” to the next node.
  4. To achieve this behaviour, we first set which node to exit:
    // Set the ID of the Node to exit from
    const nodeId = 'node_lecture';
    
    and make sure it’s included in the repository:
    // Double check that nodeId is valid
    if (!player.repository.has(nodeId)) {
     console.error(`${nodeId} is not a valid node id. Please check the id`);
    }
    
  5. Next, we define the points in time in which we allow exiting Lecture:
    // Exit points (in seconds)
    const customExitPoints = [5, 12, 22];
    
    and get Lecture‘s Node object:
    // Get the object of the Node to exit from
    const nodeToExit = player.repository.get(nodeId);
    
  6. We iterate the customExitPoints array we defined earlier and set event triggers on Lecture‘s Node object. These events will fire during the playing of Lecture on each of the points in time defined in customExitPoints - after 5, 12 and 22 seconds after the Node started playing.
    // Set an exit for each point in the node's time
    customExitPoints.forEach(function(exitPoint) {
     nodeToExit.on(`timeupdate:${exitPoint}`, exitIfDecisionMade);
    });
    
    Each event fire will execute exitIfDecisionMade().
    // If the current node's decision was made then play the next node.
    function exitIfDecisionMade() {
     const currentDecision = player.decision.get(player.currentNode);
     if (currentDecision.state === 'made') {
         player.currentNodeIndex += 1;
     }
    }
    
    This function will check if the decision of the currently playing node (Lecture in our case) was made (i.e. if Question button was clicked). If so, it will start playing the next node.

Discover how to create a more complete experience by checking out our Docs.

Rate this page: X
Tell us more!