I found this question with a really amazing answer How do I implement branching dialogue in javascript?
Unfortunately I can't seem to understand how to get this to work since the code posted isn't complete.
I've gotten my script to print out everything up to the decisions the player has to make, but I just can't understand how to feed the answer back into the script and give the player back the appropriate response.
Here's what I have so far
var current_step;
var story = [
{ m: "Hi!" },
{ m: "This is my new game." },
{ question: "Do you like it?", answers: [
{ m: "yes", next: "like_yes" },
{ m: "no", next: "like_no" },
] },
{ label: "like_yes", m: "I am happy you like my game!", next: "like_end" },
{ label: "like_no", m: "You made me sad!", next: "like_end" },
{ label: "like_end" },
{ m: "OK, let's change the topic" }
];
function execute_game() {
var current_line = 0;
while (current_line < story.length) {
current_step = story[current_line];
if (undefined !== current_step.m) {
display_message(current_step.m);
if (undefined !== current_step.next) {
current_line = find_label(current_step.next);
} else {
current_line = current_line + 1;
}
} else if (undefined !== current_step.question) {
display_message(current_step.question);
display_answers(current_step.answers);
return;
// display the question: current_step.question
// display the answers: current_step.answers
// choose an answer
// and change current_line accordingly
}
}
}
function display_message(varStore) {
document.getElementById("container").innerHTML += varStore + "<br>";
}
function display_answers(varStore) {
for (i = 0; i < varStore.length; i++) {
document.getElementById("container").innerHTML += "<a href='" + varStore[i].next + "'>" + varStore[i].m + "</a><br>";
}
}
What do I need to do from here?
href
attribute of the links indisplay_answers
, because those seem to result in links to other documents, which doesn't seem right to me. Is that correct? \$\endgroup\$