Howdy, Stranger!

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

Calling on Java script in a passage [Sugarcube, Twine 2x]

I'm assuming that javascript is interchangable between both versions of sugarcube here so I sincerely hope I am right x)

So I had been browsing the Sugarcube wiki for some time and still hadn't found too much relevant information to what I was looking for.

I was searching for a method to have 6 $object values that were completely randomised from a set amount from 0 - 150, but added up together would always add up to a certain amount say 600 or 150 (not both though but I havent decided on the final amount). I would also like to avoid decimals places in the concurrent values at all costs if possible.

I didn't have much luck on the twine databases I had found, but I did manage to find some javascript relating to this issue on a javascript website dubbed Stack Overflow. However, problems automatically arose as I could not find much in detail upon how to call JavaScript from the javascript section into an individual passage - whilst simultaneously using sugarcube/twine macros along with it.

I'm also wondering if this is the best method for this task, but I had no idea how to implement this with the twine/sugarcube macros (besides the random number generation really) so I was a little stuck.

Much obliged chaps :)

Here is the code in question but it might be easier just to avoid this in the first place, again - i'm not particularly sure whether this is covered in the macros or not.
var max = 36;
var r1 = randombetween(1, max-3);
var r2 = randombetween(1, max-2-r1);
var r3 = randombetween(1, max-1-r1-r2);
var r4 = max - r1 - r2 - r3;


function randombetween(min, max) {
  return Math.floor(Math.random()*(max-min+1)+min);
}
function generate(max, thecount) {
  var r = [];
  var currsum = 0;
  for(i=0; i<thecount-1; i++) {
     r[i] = randombetween(1, max-(thecount-i-1)-currsum);
     currsum += r[i];
  }
  r[thecount-1] = max - currsum;
  return r;
}

Much obliged :)

P.S Apologies for the somewhat strenuous link between the title of this question and it's content, I was hoping this would also help in benefitting the community somewhat and hopefully avoid these sort of questions being asked in future x)

Thank you.

Comments

  • * Anyone worried about the source, I personally wouldn't. The site is basically a database of information for user queries and viable answers, much like this site but with javascript instead! From what I can already gather at least.
  • You don't need to use javascript to do the first part of your example:
    <<nobr>>
    <<set _max to 36>>
    <<set $r1 to random(1, (_max - 3))>>
    <<set $r2 to random(1, (_max - 2 - $r1))>>
    <<set $r3 to random(1, (_max - 1 - $r1 - $r2))>>
    <<set $r4 to _max - $r1 - $r2 - $r3>>
    <</nobr>>\
    \
    max: _max
    
    r1: $r1
    r2: $r2
    r3: $r3
    r4: $r4
    
    sum r1..r4: <<print $r1 + $r2 + $r3 + $r4>>
    

    And you only need a little Javascript to implement the generate method from your example:
    <<set $r to []>>
    <<set _max to 36>>
    <<set _thecount to 5>>
    <<set _currsum to 0>>
    
    <<for _i to 0; _i lt _thecount - 1; _i++>>
    	<<set _upper to (_max - (_thecount - _i - 1) -_currsum)>>
    	<<set _rand to random(1, _upper)>>
    	<<set $r.push(_rand)>>
    	<<set _currsum += _rand>>
    <</for>>
    <<set $r.push((_max - _currsum))>>
    
    max: _max
    r array: $r
    sum r array: <<print $r.reduce((prev, curr) => prev + curr)>>
    
    ... I added a _upper and _rand to make the code slightly more readable.
  • Thank you greyelf that was very useful!

    I managed to implement that into my own version and give it a test run.
    <<nobr>>
    <<nobr>>
    <<set $max to 450>>
    <<set $r1 to random(20, ($max))>>
    <<set $r2 to random(20, ($max - $r1))>>
    <<set $r3 to random(10, ($max - $r1 - $r2))>>
    <<set $r4 to random(10, ($max - $r1 - $r2 - $r3))>>
    <<set $r5 to random(20, ($max - $r1 - $r2 - $r3 - $r4))>>
    <<set $r6 to $max - $r1 - $r2 - $r3 - $r4 - $r5>>
    <</nobr>>\
    \
    max: $max
    
    r1: $r1
    r2: $r2
    r3: $r3
    r4: $r4
    r5: $r5
    r6: $r6
    
    sum r1..r6: <<print $r1 + $r2 + $r3 + $r4 + $r5 + $r6>>
    

    Unfortunately some of these values tend to revert to negative values, which unfortunately defeats the object of the system somewhat. I was also uncertain of how to implement an upper ceiling for the maximum amount any $r can equate to, but I wasn't sure where exactly to place the maximum value as each attempt was met with an error message.

    I wasn't entirely sure of how this method worked in the first place because I had no means of testing it solely on a javascript level (currently), so I wasn't sure what the application of the second segment was.. Is it integral to this first part in any way? I honestly have no idea as on it's own it appears to do nothing:
    <<set $r to []>>
    <<set $max to 150>>
    <<set $thecount to 5>>
    <<set $currsum to 0>>
    
    <<for $i to 0; $i lt $thecount - 1; $i++>>
    	<<set $upper to ($max - ($thecount - $i - 1) -$currsum)>>
    	<<set $rand to random(1, $upper)>>
    	<<set $r.push($rand)>>
    	<<set $currsum += $rand>>
    <</for>>
    <<set $r.push(($max - $currsum))>>
    
    max: $max
    r array: $r
    sum r array: <<print $r.reduce((prev, curr) => prev + curr)>>
    

    And this is how it appears in passage when the two are together:
    max: 450

    r1: 81
    r2: 309
    r3: 25
    r4: 18
    r5: 18
    r6: -1

    sum r1..r6: 450
























    max: 450
    r array: $r
    sum r array: Error: <<print>>: bad expression: Unexpected token =>

    Is there anything that can be done to rectify this? Much appreciated.
Sign In or Register to comment.