It looks like you're new here. If you want to get involved, click one of these buttons!
<<if
SaveSystem.autosaveOK
&& storage.getItem("saves").autosave !== null
>>\
You have a session already in progress. Continue where you left off?
<<script>>
var resumeBtn = insertElement(
output,
"button",
"resume-button",
"ui-autostart-button",
"Resume where I left off"
);
var restartBtn = insertElement(
output,
"button",
"restart-button",
"ui-autostart-button",
"Start over"
);
$(resumeBtn).click(function() {
SaveSystem.loadAuto();
});
$(restartBtn).click(function() {
SaveSystem.deleteAuto();
state.display(state.active.variables.first);
});
<</script>>\
<<else>>\
<<script>>
alert("Testing!");
state.display(state.active.variables.first);
<</script>>\
<</if>>\
Basically, the <<if>> condition is false on first run (there are no autosaves yet present), so the script in the <<else>> should display the requested passage, yet it fails to do so. (The alert in the <<else>> does appear, so the overall logic is working correctly.) The state.display there has no effect whether I use the $first variable or name the passage directly.
Comments
You have a grave error in your initial conditional logic. You're using the method SaveSystem.autosaveOK() as if it were a data property, when it's a function (see:
SaveSystem.autosaveOK()
). That will return the function itself, rather than calling it, which means that bit will always evaluate to true, so you're only really testing whetherstorage.getItem("saves").autosave !== null
.Also, if you want to check for the existence of the autosave, simply use
SaveSystem.hasAuto()
(see:SaveSystem.hasAuto()
).TL;DR: The above code is broken and not leveraging the API fully (to be fair,
.hasAuto()
might be more recent than your code). Anyway, do this instead:Second issue:
Your big problem is how you're calling state.display() in the
<<else>>
clause. You cannot callstate.display()
from within a call tostate.display()
. The call in the "Start over" button works because it gets called long after the original call has completed, not so with the call in the<<else>>
clause.The technical details are that the second/inner call to
state.display()
happens during the rendering of the passage by the first/outer call, which actually does work as you'd expect (the history is updated, output is generated and displayed, etc.), but once that call completes the first/outer call continues and overwrites the output of the second/inner call. So, you're left in a position where the history updates of both the first/outer and second/inner calls have happened (you can check this), but the display updates of the first/outer call have won out in exclusion of those by the second/inner call.The fix, while a little daft, is simple, use a timeout to delay the call to
state.display()
until after the original call completes.TL;DR: The above code cannot work, so do this instead:
Third issue: (just an annoyance really)
Your two button are snuggling together hardcore, which (IMO) looks terrible. I'd suggest adding a space between them. For example:
Putting it all together:
To blather on a bit more about that topic. Essentially, by calling
state.display()
within a passage, you're doing a kind of recursion. Furthermore, because of the way thatstate.display()
is structured, the history updates act as a FIFO, while the display updates act as a LIFO.To use pseudo-code,
state.display()
works like this: So, when you callstate.display()
within a rendering passage, it would look like this: (I used three calls here) As you can see, the history updates happen in-order of call, but the display updates happen in reverse-order.Your explanation is very helpful. I have made the nested display error enough times that I should have caught that on my own, but I think that the fact that the nesting allows the history to update as normal but overwrites the text output threw me off the scent. (There's also the fact that, crazily enough, the bare state.display call did at one time "work"--in the sense that the passage displayed even on the first run in a given browser--before--though I can't see why that would be.)
It's all working now!