Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Increase Loop Iterations

Hi!

I am currently using twine for something rather untypical, a random map generator. Which works fine by the way. I only have the problem, that in some situations i need very long loops, which are aborted after a certain amount of iterations. I could split the loops up, but that would just bloat the code. There seems to be a way to change the number of maximum iterations in sugarcube, but is there one for vanilla twine too?

Comments

  • Are you using one of the JavaScript loop statements or simulating a loop via recursive passage display?
  • I am not sure what javascript loop statements are, but i am using the recursive passage display :)
  • SugarCube has the for macro which may do what you want.
  • I am considering swiching to sugarcube, if there is no other way, but i already used a lot of stuff like the either macro, which, if i got this correctly, is not included in sugarcube.
  • engine wrote:

    I am not sure what javascript loop statements are, but i am using the recursive passage display :)


    Then, for the vanilla story formats, the only solutions are likely to either split up your recursive passage display into more manageable chunks or to begin using JavaScript loop statements.

    You might be able to restructure your code to get some extra mileage out of it, but that would depend on what exactly the "abort" is.  I'm guessing you're probably running into the browser's built-in execution limit, but I suppose it's possible you're hitting something else.  When you say your code is getting "aborted", are you talking about the "script is taking too long" popup?


    engine wrote:

    I am considering swiching to sugarcube, if there is no other way, but i already used a lot of stuff like the either macro, which, if i got this correctly, is not included in sugarcube.


    While there are differences between the vanilla story formats and SugarCube, either() is not one of them.
  • Hey! :)

    I get the following error-message under chrome: <<if>> bad condition: $auswahl[$auswahl.length - 1] neq -1

    i thought it was a twine thing, but if it is a browser related issue I guess I'll really have to resize the chunks...

    Thanks for the posts so far...
  • I have tried splitting the loop up, but I failed... it just seemed to get buggier and slower.

    The original loop <<suche>> did the following: It would search the array $map for a value ($gesucht), write the position of every find into the $auswahl array, starting at 0 and always picking up the search one position after the last find, until the indexOf method would return -1 because there are no more values like the one searched for in the array.
    <<if $auswahl[$auswahl.length - 1] neq -1 >>

    <<set $auswahl.push($map.indexOf($gesucht, $suche))>>

    <<set $suche = $auswahl[$auswahl.length - 1] +1>>

    <<suche>>
    <<else>>
    <<set $suche = 0>>
    <<endif>>
    Could this loop be implemented as sugarcube for-loop without creating errors even at large numbers of itinerations? Unfortunately I have no clue about JS...
  • engine wrote:

    I get the following error-message under chrome: <<if>> bad condition: $auswahl[$auswahl.length - 1] neq -1


    That's kind of an odd error, considering the expression it's complaining about.  I get the feeling that something else is going on there.


    engine wrote:

    Could this loop be implemented as sugarcube for-loop without creating errors even at large numbers of itinerations? Unfortunately I have no clue about JS...


    Yes.  For example, using code similar to what you're using now (i.e. using indexOf()):

    <<for $suche to 0; true; $suche++>>\
    <<set
    $suche to $map.indexOf($gesucht, $suche);
    $auswahl.push($suche);
    >>\
    <<if $suche eq -1>><<break>><</if>>\
    <</for>>
    The backslashes are there solely to keep output from being generated (from the line-breaks).


    Alternatively, for something which will also work in the vanilla story formats, since you're already tapping the array indexOf() and push() methods (which are JavaScript, BTW), you could also simply do it with the array every() method.  For example:

    <<set $map.every(function (v, i) {
    if (v === $gesucht) {
    $auswahl.push(i);
    }
    return true;
    })>>
    <<set $auswahl.push(-1)>>
    The v and i parameters are the current array element's value and index, respectively.  The function passed to every() only pushes the valid indices onto $auswahl, so that is why the second &lt;&lt;set&gt;&gt; is there, to push a final -1 on, just like your original faux-loop does.
  • I Implemented the second method and it worked just like a charm. Thank you! I am a bit annoyed about myself though, I searched for a method like this myself, but I couldn't find it. However, here's a small preview on how a map generated with my tool can look like:

    [URL=http://imgbox.com/9OgLZOSv][IMG]http://5.t.imgbox.com/9OgLZOSv.jpg[/img][/URL]
  • Nice.  Glad everything worked out for you.


    engine wrote:

    I am a bit annoyed about myself though, I searched for a method like this myself, but I couldn't find it.


    I find the MDN JavaScript reference to be fairly friendly.

    I'll also note that using every() as I did is a little contrived.  It works perfectly well, but what you really want to use there is forEach(), which would obviate the need for the return true in the callback.  I used every() instead, since I don't believe the vanilla story formats polyfill forEach() and they support older browsers where it doesn't exist.
Sign In or Register to comment.