How would I track last visited passages throughout a maze game to activate/deactivate links?

0 votes
asked Jul 11 by arvensis (160 points)

Hi, I'm new to Twine and not great with JavaScript. I'm trying to make a maze puzzle game in the form of a "building" (inspired by Christopher Manson's MAZE), with room entrances offered as choices. Once the player has entered a new room, I want the room from which the player arrived to be "locked", with link text deactivated, until the player reaches it from a different path—meaning the player should not be able to go directly backward to a room that has just been visited, but can revisit if the room is offered as an option again at a later time. 

I poked around and the lastVisited function looks perfect for this since it can track how many clicks/rooms away from a particular Room X the player has gone. 

With a value of -1 being never visited Room X , 0 being in Room X, 1 being just past Room X, and 2 being two turns/clicks past the room, I'd want the link to Room X to deactivate if lastVisited("Room X") is equal to 1. For all other cases, if lastVisited("Room X") is less than 1 or equal to or greater than 2 the link should stay activated or be reactivated. This would have to work for each of around 20 rooms.

How would I implement this? 

 

1 Answer

+1 vote
answered Jul 11 by HiEv (7,020 points)
selected Jul 12 by arvensis
 
Best answer

It's probably easiest to use the previous() function inside of a widget.  So make a passage for your widgets (the name doesn't matter) and add the "widget" and "nobr" tags.  Then put this in that passage:

<<widget "Exit">>
	<<if $args[0] !== previous()>>
		<<if $args[1] === undefined>>
			[[$args[0]]]<br>
		<<else>>
			[[$args[1]|$args[0]]]<br>
		<</if>>
	<</if>>
<</widget>>

(Note: This will create a passage titled "$args[0", you will need to delete that passage for this widget to work.)

Now you can do something like this in your passages:

Exits:
<<Exit "Room1" "North">><<Exit "Room2" "South">><<Exit "Room3" "East">><<Exit "Room4" "West">>

That will display something like this:

Exits:
North
South
East
West

With those links going to Rooms 1-4, but if any were the previous room you were in then they won't be listed.

Hope that helps! smiley

commented Jul 12 by arvensis (160 points)

Thanks you! It works awesomely. What does the $args and the

[[$args[1]|$args[0]]]

mean (if it's not too complicated to explain)?

commented Jul 12 by HiEv (7,020 points)
edited Jul 12 by HiEv

"Args" is short for arguments.  And the number in the brackets is the number of the argument (starting with zero).

So when you call a widget like:

<<widgetName "argument1" 2 "argument number 3" $value>>

then $arg[0] will equal "argument1", $arg[1] is the number 2, $arg[2] is "argument number 3", and $arg[4] will be whatever the value of $value is.

For more information see the <<widget>> macro in the documentation.

So if you call:

<<Exit "Room1" "North">>

Then:

[[$args[1]|$args[0]]]

would be:

[[North|Room1]]

Which would show a link that displays "North", but clicking that link takes you to the passage titled "Room1".

See links markup in the documentation for details.

Hope that helps! smiley

Welcome to Twine Q&A, where you can ask questions and receive answers from other members of the community.

You can also find hints and information on Twine on the official wiki and the old forums archive.

See a spam question? Flag it instead of downvoting. A question flagged enough times will automatically be hidden while moderators review it.
...