There are a number of questions asking how to do a for loop in Harlowe, since no macro is provided for that:
this, and
this, and also this
bug report. Some workarounds that are proposed include using the (live:) macro or embedding (display:) within itself. I have used extensively the (live:) trick and I know its issues (mainly, that the content under the macro gets printed before the loop has finished running).
But I didn't have this idea until today: is there any way to directly use Javascript loops within Harlowe?
Please forgive that I don't provide any experiment or test, but I can't speak Javascript (and I tried to learn, believe me). But if this can be done it could be a major improvement for the game I'm writing (I need use (live:) loops all over the place). Thanks!
Comments
It perfectly prints the Harlowe macros with the correct values, three times. Of course, it doesn't run the Harlowe macros. That's about as far as my Javascript knowledge gets.
Any suggestions, please? The lack of a loop macro is by far my biggest problem with Harlowe.
It is very effective even if you need to be very carefull to set up some controls, to avoid infinite loops, etc.
If you really need some javascript I have some code that works, I like to use Harlowe code because it is more fun but it can be done. As far as I know javascript is only usefull to return a value, it can not generate Harlowe code as you found because there is no way to recompile the code in an already displayed passage.
Take the following very basic three passage implementation of a for macro I quickly put together as an example:
1. The setup passage: 2. The for-macro passage: 3. The for-macro-inner passage:
The above very basic example generates the following HTML elements with just two items in the array: ... the above has nine levels of nesting.
Now look what happens if we change the $list array in the setup passage to have four items instead: eg (set: $list to (array: "One", "Two", "Three", "Four")) ... the above has fifteen levels of nesting.
There are a number of ways to improve the above basic for-marco example so that it has less nesting but the issue still remains.
no moonnot XML. Those are simply tags for custom (HTML) elements.As TheMadExile stated, that is not XML, that is some of the HTML dynamically generated by Harlowe's engine when the Reader viewed the setup passage. All I did was add indentation and line-breaks to make it slightly more readable.
So far I have been working with (live:) loops but as I add more loops in more places, there are more bugs and more things that occasionally fail. Speed of execution seems to be an issue: I had a loop that didn't work unless I made it slow enough. I'm afraid of fine-tuning for my own computer and then finding out it doesn't work anywhere else.
I fear the recursive methods because of the nesting problem (I easily hit 30 levels of nesting in my tests). Any other solution would be a blessing.
So your javascript would be: NB: In your version the loop started at 0, but arrays in Harlowe start at the index 1.
I've also changed it so you can pass in how many times you want the loop to run as a parameter.
Then in the passage you'd write: