Howdy, Stranger!

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

Display variable passage in a popup?

I'm using this popup macro, which works fine-
/*pop-up*/
Macro.add("popup", {
	version : { major: 1, minor: 0, revision: 0 },
	handler : function () {
		if (this.args.length < 2) {
			var errors = [];
			if (this.args.length < 1) { errors.push("link text"); }
			if (this.args.length < 2) { errors.push("passage name"); }
			return this.error("no " + errors.join(" or ") + " specified");
		}

		var	el      = document.createElement("a"),
			passage = this.args[1],
			title;
		el.innerHTML = this.args[0];
		el.className = "link-internal macro-popup";
		el.setAttribute("data-passage", passage);
		title = el.textContent;
		UI.addClickHandler(el, null, function (evt) {
			var	dialog = UI.setup(title, "popup");
			new Wikifier(dialog, tale.get(passage).processText().trim());
		});
		this.output.appendChild(el);
	}
});

I can type this and it works-
Click to view <<popup "Map" "Map complex">>

It shows the word "map", and when you click it you can view within a popup the 'map complex' passage.


Now I'm trying to make a popup that displays passages representing items in the order you found them. So you click "view items" and you get a popup which is the passage for "item 1" whichever that may be.

Passage1 sets up the array with some items
<<set $n to 0>>

<<set $Inventory = ["Revolver", "Letter", "Money", "Water"]>>

[[passage 2]]

Passage 2 adds more items, which works fine, and makes a link to a passage representing the first item in the array (Revolver). However the popup code doesn't work since $items isn't really a passage. $items works fine.
<<set $Inventory.push("Hat", "Coat")>>

<<set $items to $Inventory[$n]>>\
<<popup "view items" "$items">>

[[$items]]

"Error: the passage "Revolver" does not exist" in the popup. Revolver does exist, though.

This will be used for a glossary logging creatures and events and since they can happen in different order depending on your decision, I want to add them to an array and then have the popup let you click through them.

Comments

  • Remove the double quotes from around the $items variable in your <<popup>> macro:
    <<popup "view items" $items>>
    
  • Ahh thanks, so simple yet such a difference...
  • As a general rule, you do not quote story $variables. You should do so only for certain macros (generally, those which need to modify the $variable), which are noted within each macro's documentation.

    From the SugarCube 2.x Macro Library documentation:
    Story Variable Substitution

    Story variable substitution occurs automatically in SugarCube, so all macros which take arguments may take story variables (e.g. $pie) as arguments. Occasionally, this means that story variables may need to be quoted to protect them from automatic variable substitution for macros which need the name of the story variable and not its value; these instances are noted.
Sign In or Register to comment.