Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Trying to get a conditional link

Hi, I'm trying to get it so a link will only show up if a certain passage has been comlpete.

I've looked all over and only find stuff from 1.4 which is frustrating cause to me its changed a bit since then, i'm using snowman because i want more customization for my story. Currently got background buttons and stuff sorted but i cannot get a link that is only there if a certain passage has been hit.

To clarify a bit more

I'm trying to get passage 3 to display a link only if passage 2 is selected.

Start

Passage 1 Passage 2

Passage3

Thanks if anyone understands, i'm kind of new to HTML scripting, getting good so far though.

Comments

  • The basic conditional is easy enough:
    <% if (/* passage 2 has been viewed */) { %>[[some link]]<% } %>
    
    The "passage 2 has been viewed" is the sticky bit. You could do it in one of two ways, use a state variable or check the story history. The former is easy, the latter is a bit more difficult because I don't believe that Snowman includes a built-in method for checking if a passage has been visited before (i.e. it's in the history).


    Using a variable:
    You'd simply add something like the following to Passage 2:
    <% s.visitedPassage2 = true; %>
    
    And then in Passage 3:
    <% if (s.visitedPassage2) { %>[[some link]]<% } %>
    


    Checking the state history:
    First. Put the following code in your Story JavaScript:
    /*
    	Returns whether the Passage object corresponding to either the
    	ID or name exists within the story history.
    */
    _.extend(Story.prototype, {
    	hasVisited : function (idOrName) {
    		var id;
    		if (_.isNumber(idOrName)) {
    			id = idOrName;
    		} else if (_.isString(idOrName)) {
    			try {
    				id = _.findWhere(this.passages, { name : idOrName }).id;
    			} catch (e) { /* no-op */ }
    		}
    		if (id == null) { /* lazy equality for null */
    			return false;
    		}
    		return this.history.indexOf(id) !== -1;
    	}
    });
    
    And then in Passage 3:
    <% if (story.hasVisited("Passage 2")) { %>[[some link]]<% } %>
    
  • Thank you.
    Using a variable has helped a lot.
    My problem now however is the way i have set it to go back to the start of the story, the second link on passage 3 has stayed even though this time i went through passage 1 but used a link back to start as it is a loss for the reader.

    I know the reasoning is because it is still the same story and the code hasn't refreshed.

    Now would i have to make a new way to get back to the beginning of the story or when the start passage is visited it would reset all code.

    Thanks again.
  • AFAIK, Snowman doesn't implement any kind of session management, so simply reloading the page should reset everything. Try the following:
    <a href="javascript:void(0)" onclick="window.location.reload()">Restart</a>
    
  • You could initialize all your variables with default values in your start passage like so:
    <% s.visitedPassage2 = false; %>
    
  • edited December 2015
    greyelf wrote: »
    You could initialize all your variables with default values in your start passage […]
    While that is a good idea (that or in the Story JavaScript via story.state.variable), and one which I heartily recommend, it doesn't reset any of the rest of a story's state (e.g. the history).

    Using the code I gave, or something similar, is the correct way to completely restart a story.
  • Thank you Exile, This has really helped me make the story i wanted. I will post here again or start a new discussion if i hit another barrier that i can't seem to find an answer for.
Sign In or Register to comment.