I have a passage (Dungeon Status) set up that when called, lets me print the entries in a Javascript array (dungeon) into a string (dungeonList):
<script>
dungeonList = "";
if (dungeon.length == 0) {
dungeonList = "empty";
} else {
for (i=0; i < dungeon.length; i++) {
dungeonList += dungeon[i] + ", ";
};
};
</script>
[(print: dungeonList)]
dungeon and dungeonList are both originally defined in my startup passage as an empty array and an empty string respectively.
The problem I have is that if I run (display:"Dungeon Status") on the starting passage, I get an error that dungeonList is undefined, which, I assume, means that it's trying to (print: dungeonList) before it runs my definitions. No matter where I put the definitions, it won't run them before it tries to (print:).
However, if I open a different passage first, then open one that runs (display: "Dungeon Status"), I don't get the undefined error, but I do get an empty string, which I take to mean that it's not running the script yet, but it has had time to run my definition.
It works perfectly fine when I call it after adding a new entry to the array, like so:
(link-repeat: "room")[
<script>dungeon.push("room");</script>
(replace: ?dungeonlist)[$dunPrint]
]
I've figured out how to get around it by adding in a (live:) that waits 1ms, then calls (display: "Dungeon Status") and then stops, but I feel like that's an odd hack I'd like to avoid if possible.