Howdy, Stranger!

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

Workaround for SugarCube not supporting either()?

So I just switched to SugarCube to use the awesome save features, and am very happy with all that--but of course, other things don't work quite so well!

For one thing, it appears the either() function is not supported? I was kinda using that, but now it's telling me that "either is not defined."

Can someone suggest a workaround? I was using it to add variability to my very simple combat system-- $skill + either(0,1,2,3,-1) + $weapon gte $monsterpower sorta stuff. Is there a random number function for the code illiterate?

And secondly, I was having fun using either() to create random events in my central hub card...weather, one of the characters wandering through, that sort of thing, by putting text strings in. I was hoping to use that to cause semi-random events, like merchants appearing and rains of frogs and so forth.

If there's a random number function, I suppose I can use that to make a clunky equivalent--assign $random_happening using the random number, and then build a really large list of if/else? But either() seemed a easier.

Does anybody have a workaround? Am I just doing it wrong?

Comments

  • SugarCube uses <Array>.random(min , max) or  Array.random(array , min , max) as can be seen here

    Following example from SugarCube docs:

    // given: $pies = [ "Blueberry", "Cherry", "Cream", "Pecan", "Pumpkin" ]
    $pies.random() Returns a random pie
  • As noted by greyelf, SugarCube handles that functionality in other ways, via <Array>.random(min , max) and/or Array.random(array , min , max).

    If you really wanted to use a function by that name, you can always use a very simple shim.  For example:

    window.either = function (/* variadic */)
    {
    if (arguments.length === 0) { return; }
    return Array.random(arguments);
    };
    And yes, there are random functions as well: random(min , max) and randomFloat(min , max).  It's also possible to enable a seedable PRNG, to make random results within a playthrough deterministic, if you needed/wanted something like that.
  • Thank you! I have managed to get that to work, with tweaking. You guys are awesome.
  • Since this seems to be coming up more often than I'd like, the next release of SugarCube will include a built-in shim for either().
  • FIFFIF
    edited January 2017
    My either function is still not working.
    either( "great job being honest most of the time. I'm proud of you.", "great job being so honest. You must drink a lot of honest tea.", "we think people who are honest are cuties. Great job today.", "it's a great feeling when you do the right thing and are honest as you do it. So, we commend you for doing just that.", "we know it's not easy going room the tasty alien zombie apocalypse and being honest the entire time. But somehow you managed to do that! Very impressive.", "honesty isn't just the best policy, it is the only policy you live by. Impressive!"
    

    Shows all the code. I'm using the most recent version of sugarcube.
  • If you're try to print the selected text, then you need to use <<print>>. For example:
    <<print either("A", "B", "C")>>
    
    Using your given code: (w/ line-breaks and indention for readability)
    <<print either(
    	"great job being honest most of the time. I'm proud of you.",
    	"great job being so honest. You must drink a lot of honest tea.",
    	"we think people who are honest are cuties. Great job today.",
    	"it's a great feeling when you do the right thing and are honest as you do it. So, we commend you for doing just that.",
    	"we know it's not easy going room the tasty alien zombie apocalypse and being honest the entire time. But somehow you managed to do that! Very impressive.",
    	"honesty isn't just the best policy, it is the only policy you live by. Impressive!"
    )>>
    
  • OHHHH Thank you once again.
Sign In or Register to comment.