Howdy, Stranger!

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

How do I create a randomness generator effected by Variables???

So here's the dealio,

I am using the <<set $var1 = Math.floor(Math.random()*3)>> code, to allow me to use a kind of hit chance thing. I need to replace the 3 with a variable, but whenever I try it doesn't work. I am using it for this piece of code-
<<set $hit = Math.floor(Math.random()*$dexterity)>>
<<if $hit == "0">>Text<<set meleewep to "holylongsword">>
<<endif>>
<<if $hit== "1">>Text<<set $wounds to "+1">> <<set meleewep to "holylongsword">> <<endif>>

<<if $hit == "2">>Text <<set $wounds = "2">> <<endif>>

<<if $wounds gte "6">>death scene<<endif>>
Also, would it be possible to use two variables in this? For example, <<set $var1 = Math.floor(Math.random()*$dexterity*strength.)>>

Thanks guys,
Bladezy Boo

Comments

  • Are you sure that $dexterity is set by that point?  If it's not, then you'll only ever get 0 or NaN out of the expression (depending).


    Additionally, you're conflating numbers and numeric strings.  Don't do that.
    • Numbers: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
    • Numeric strings: "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"
    IOW, stop quoting your numbers.


    Finally, just use the random() function built into the story formats (docs: vanilla formats or SugarCube).  For example, these will give you the same 0(n-1) range that you're using now:

    <<set $hit to random(0, $dexterity - 1)>>

    <<set $hit to random(0, $dexterity * $strength - 1)>>
    These would, instead, yield the same basic range but starting at 1 (i.e. 1n):

    <<set $hit to random(1, $dexterity)>>

    <<set $hit to random(1, $dexterity * $strength)>>
  • Your a genius.
    It took me a while to fix a few errors, but that was due to missing $signs.

    Thanks so much,
    Bladezy Boo
    P.S,
    Would you happen to know how I would go about making a statement that said if a variable was between 10 and 50, it would do something, but if it was more or less than 10/50, then it wouldn't?
  • The following should work.
    (note: using TWEE notation, the line starting with a double colon '::' represents a new passage with a passage title as indicated.

    :: StoryInit
    <<set $var to 9>>

    :: Start
    Change the value of the $var set in the StoryInit passage to test the following.
    $var = <<print $var>>

    Testing using an OR.
    <<if $var < 10 or $var > 50>>\
    The variable is not between 10 and 50.
    <<else>>\
    The is between 10 and 50.
    <<endif>>

    Testing using an AND.
    <<if $var >= 10 and $var <= 50>>\
    The is between 10 and 50.
    <<else>>\
    The variable is not between 10 and 50.
    <<endif>>
  • Wow, Thanks guys. It works pretty much flawlessly now. of course, I made a few mistakes while putting it in, but that's to be expected.

    Without you, I might never be able to finish this. Thanks
Sign In or Register to comment.