Ok, I have a complicated problem. This is a very simplified version but I think the basis is the same.
Say I have a function that prints content to the screen, appending a node:
window.printSomething = function() {
$(function () {
$('.passage').append('<p>Something</p>')
} )
}
However, I want this function to do nothing in certain conditions. So I define a condition:
window.printSomething = function() {
$(function () {
if( flag == true ) { $('.passage').append('<p>Something</p>') }
} )
}
Now, in a passage, I do this:
<% flag = true ; printSomething() %>
It works, and a paragraph with the word “Something” appears. And this:
<% flag = false ; printSomething() %>
It works! It prints nothing. But if I do this instead:
<% flag = false ; printSomething() ; flag = true ; printSomething() %>
It breaks. Instead of following the order and printing the second something while ignoring the first, it prints both.
Can you tell me why it fails? I guess it’s related to the async working of JS, but I know almost nothing about that, not enough even to search for solutions succesfully.
This is slightly more similar to my actual use. I have:
- Several passages including the
printSomething()
function - An array with the names of those passages
- A
for
loop that attaches those passages to the current one, using Snowman’sstory.render()
- Within the loop, a condition that if it’s the last iteration, change the flag to true and then print the last passage
The same problem happens. Even though the flag changes to true only in the last iteration, it seems that the functions in the passages are called after that, and all the somethings are printed.
What am I actually trying to do? In my current WIP, I don’t use the standard Twine choices but a custom function. Passages are appended to the current text, instead of replacing the whole screen. I’d like to print all the previously seen passages when I restore a savegame, but that would print all the choices, and I don’t want that: only the narration. That’s when I thought of adding a flag. But, once the previous passages are printed, the flag has to change so the last passage can be printed with its choices and you can keep playing.
I attach the basic example. Does anyone have any pointer?
Snowman problem.zip (88 KB)