It looks like you're new here. If you want to get involved, click one of these buttons!
macros.addToInv = { handler : function (place, macroName, params, parser) { if (params.length === 0) { return throwError(place, "<<" + macroName + ">>: no parameters given"); } if (state.active.variables.inventory.indexOf(params[0]) === -1) { state.active.variables.inventory.push(params[0]); } } }; macros.listInv = { handler : function (place, macroName, params, parser) { if (state.active.variables.inventory.length === 0) { new Wikifier(place, 'nothing'); } else { var list = state.active.variables.inventory.map(function (item) { return item; }); new Wikifier(place, '<div id="inventory-list">' + list.join('\n') + '</div>'); } } };My JavaScript has become reaaally rusty since I last used it over 2 years ago, so I've been mixing-and-matching different custom macro's until I found one that I liked.
macros.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"); el.innerHTML = this.args[0]; el.className = "link-internal link-popup"; el.setAttribute("data-passage", this.args[1]); UISystem.addClickHandler(el, null, function(evt) { var dialog = document.getElementById("ui-body"); $(dialog) .empty() .addClass("dialog popup"); new Wikifier(dialog, tale.get(evt.target.getAttribute("data-passage")).processText().trim()); return true; }); this.output.appendChild(el); } });
Comments
The following are modified versions of your example macros:
Using <<addToInv "item">> errors with Error: cannot execute macro <<listInv>>: Map.prototype.forEach called on incompatible object.
I can bypass this by using:
Object.keys(inventory).forEach... OR
var propNames = Object.getOwnPropertyNames(inventory);
And running propNames.forEach.... however, nothing can be seen in the actual popup window. ($inventory thus seems to be null, but the "Nothing" wikifier isn't used)
Debugging with a simple <<print $inventory>> in the popup window, only lists "[object]", even with different items added to the map.
So, for SugarCube v1.x, I'd suggest something like the following (which uses generic objects):
I used Map because the question was tagged sugarcube 2
I could have sworn that I had SugarCube 2, which is why the thread was tagged with v. 2 >_>
Now I know better... welp, time to fix that