Quantcast
Channel: Help! with 2.0 - Twine Forum
Viewing all articles
Browse latest Browse all 1844

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

$
0
0
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.

Viewing all articles
Browse latest Browse all 1844

Trending Articles