0 votes
by (280 points)
edited by

Hi all,

a very basic question for Sugarcube 2: How to show all passages/choices made by a user till that moment, as a  sort of dynamic list of passages.

Maybe with the Story.lookup?

Please help

thank you!!

1 Answer

0 votes
by (68.6k points)

You can get an array of the titles of all past<->present moments within the history via the following:

State.passages

You may turn that array into whatever list you need.

by (280 points)

Thank you, can you please also share an hint on how to print values (let's say tags from each previous and current passages) from this one?

State.passages

Also, I cannot find the above in the Sugarcube 2 docs, just the following seems available. Am I correct?

State.passage

Thank you very much!

by (68.6k points)
edited by

You may use the tags() function to return an array of the tags for any passage.  For the current passage:

tags()

For the previous passage:

tags(previous())

An array of each moment's tags array via State.passages:

State.passages.map(function (title) { return tags(title); })

 

For the first two, simply use the <<print>> macro.  For example:

<<print tags().join(", ")>>

<<print tags(previous()).join(", ")>>

The <Array>.join() method simply turns the array into a string with each member separated by the given delimiter.

For arrays, I'd probably suggest using an unordered list, which you can style to look like pretty much whatever you'd want.  For example:

<<print "<ul><li>" + State.passages.map(function (title) { return tags(title).join(", "); }).join("</li><li>") + "</li></ul>">>

 

by (280 points)
edited by

Thank you very much for this tags overview, very clear and useful. Your third suggested option is the one I was aiming at.

Is there any docs where the latter api is documented? I mean, I understand the .map is a js native function to create an array to store all given information, but where I can find the Sugarcube State.passages in docs?

...