[Snowman] Pausing a loop and waiting for user input

I have a list with an arbitrary number of passages that I want to render. Something like this:

p=["1", "2","3"]
for( var i=0; i<p.length; i++ ) {
    story.render( p[i] ) 

But suppose each passage has an action, a set of buttons (not taking you to another passage). I want to make sure that the player reads the passage and takes a choice. The best option would be pausing the loop until a button is clicked: printing a passage, waiting for player input, after player input printing the next passage, etc.

My Javascript is not deep enough and I don’t have any idea of how to pause the loop and prevent all passages from being printed right away. Can you point me in any direction about how to do this?


Probably you don’t pause the loop, but write a function that displays the next passage and then call that after player input. But how you do that, or how you detect player input, would depend on the details of what you’re doing, I think?

If you don’t mind removing items from the array as you display them, the “display the next passage” code could just be story.render( p.shift() )

Hooking into the player input is probably the tricky part. Can you attach that action to the buttons on each passage? Or maybe you can do it after rendering? Something like:

p=["1", "2", "3"]
function renderNext() {
	story.render( p.shift() )
	$("button").on("click", renderNext)

I doubt that’s a complete solution (what if the user activates the button with the keyboard? does that fire the “click” event?), and it might not even work (does Snowman render the new passage right away, or do you have to wait until the new buttons exist before you can find them?) but it might be sort of in a possibly working direction…

Hey! I didn’t say anything yet because I got sidetracked by a different function, but I hope I will get to try this during the week. It looks exactly what I need!

Thanks Josh! Your idea works! Now, instead of running a loop, I display the next passage, if there’s one, when clicking a button in the previous one.

The only inconvenience is that I need to remember the “next” function at the end of every narrative passage, but it’s a fairly minimal one.

1 Like