Howdy, Stranger!

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

Setting a random to be equal to a variable?

So I understood the part about how to use a random. But I'm a bit confused with the if statements as I'm used to how simple and easy it is to have a .random in unity be equal to a variable. But I'm not 100% on how to do that here.
All I need is a random:1,6 to be checked in an if statement. I found a lot of different questions from searching and they seem too advanced. Like arrays for example. (I have a limited experience in unity and 0 knowledge of HTML, so please forgive me)

Comments

  • edited October 2016
    First. Since you've tagged this with "sugarcube 2", I'd suggest starting with the SugarCube 2 documentation if you haven't already.

    Now, you haven't really given much detail about what you're trying to accomplish. You mention a random 1–6 and a variable, but that's a bit vague, honestly. In the future, it would be wise to be as specific as possible and give as many details as possible.

    That said, here's a simple example of what I think you're looking for:
    → Let's assume that you've set a variable to the value 5
    <<set $luckyNumber to 5>>
    
    → Check the to see if the value matches a random pull
    <<if random(1, 6) is $luckyNumber>>
    It's your lucky day!
    <</if>>
    
    → Check the to see if the value does not match a random pull
    <<if random(1, 6) isnot $luckyNumber>>
    No luck for you!
    <</if>>
    
    → Check the to see if the value does or does not match a random pull
    <<if random(1, 6) is $luckyNumber>>
    It's your lucky day!
    <<else>>
    No luck for you!
    <</if>>
    
    Shown: <<set>> macro, <<if>> macro, random() function, and variables.

    Again, that was a simple example. If that's not what you were looking for, then you'll need to give some details about your specific needs.
  • First. Since you've tagged this with "sugarcube 2", I'd suggest starting with the SugarCube 2 documentation if you haven't already.

    Now, you haven't really given much detail about what you're trying to accomplish. You mention a random 1–6 and a variable, but that's a bit vague, honestly. In the future, it would be wise to be as specific as possible and give as many details as possible.

    That said, here's a simple example of what I think you're looking for:
    → Let's assume that you've set a variable to the value 5
    <<set $luckyNumber to 5>>
    
    → Check the to see if the value matches a random pull
    <<if random(1, 6) is $luckyNumber>>
    It's your lucky day!
    <</if>>
    
    → Check the to see if the value does not match a random pull
    <<if random(1, 6) isnot $luckyNumber>>
    No luck for you!
    <</if>>
    
    → Check the to see if the value does or does not match a random pull
    <<if random(1, 6) is $luckyNumber>>
    It's your lucky day!
    <<else>>
    No luck for you!
    <</if>>
    
    Shown: <<set>> macro, <<if>> macro, random() function, and variables.

    Again, that was a simple example. If that's not what you were looking for, then you'll need to give some details about your specific needs.
    Now, it is a solution, but can I also do it along the lines of this which currently errors out in order to make a simple piece of code not a pain in my butt? ((set: $value "random 6")
    Sorry for not being clear but I meant feeding a random whole number into a variable. Then checking the variable each time.
    Because your current way would involve about 6 times the dice rolls compared to one dice roll being handled over 6 different if statements.
    I mean it probably doesn't matter performance wise, but it could lead to luck problems where either 2 or more of the dice rolls match a condition and conflict two actions at the same time or none of the six dice roll the correct number and therefore nothing happens.
  • StarFire wrote: »
    Now, it is a solution, but can I also do it along the lines of this which currently errors out in order to make a simple piece of code not a pain in my butt? ((set: $value "random 6")
    That is not SugarCube syntax. It's reminiscent of Harlowe syntax, but it's incorrect even for that.

    StarFire wrote: »
    Sorry for not being clear but I meant feeding a random whole number into a variable. Then checking the variable each time.
    I'm not trying to be mean, however, you really should have been able to figure that out from my previous example, as all of the necessary components were there.

    Regardless. Try something like the following:
    → Set the $luckyNumber variable to a random value in the range 1–6
    <<set $luckyNumber to random(1, 6)>>
    
    → Check to see if the random value is exactly 5
    <<if $luckyNumber is 5>> …
    
    → Check to see if the random value is greater-than-or-equal to 5
    <<if $luckyNumber gte 5>> …
    
    → Switch over the random value
    <<switch $luckyNumber>>
    <<case 4 5>>
    It's a moderately lucky day!
    <<case 6>>
    It is the luckiest of days!
    <<default>>
    No luck for you!
    <</switch>>
    
    Shown (since last time): <<switch>> macro.
  • edited October 2016
    If you don't need unique text for each fail, another version is:
    <<set $roll to random(1, 6)>>
    <<if $roll is 6>>
    Success!
    <<else>>
    Fail. You rolled a $roll.
    <</if>>
    
  • So, we're just repeating dots I've already connected now?
  • StarFire wrote: »
    Now, it is a solution, but can I also do it along the lines of this which currently errors out in order to make a simple piece of code not a pain in my butt? ((set: $value "random 6")
    That is not SugarCube syntax. It's reminiscent of Harlowe syntax, but it's incorrect even for that.

    StarFire wrote: »
    Sorry for not being clear but I meant feeding a random whole number into a variable. Then checking the variable each time.
    I'm not trying to be mean, however, you really should have been able to figure that out from my previous example, as all of the necessary components were there.

    Regardless. Try something like the following:
    → Set the $luckyNumber variable to a random value in the range 1–6
    <<set $luckyNumber to random(1, 6)>>
    
    → Check to see if the random value is exactly 5
    <<if $luckyNumber is 5>> …
    
    → Check to see if the random value is greater-than-or-equal to 5
    <<if $luckyNumber gte 5>> …
    
    → Switch over the random value
    <<switch $luckyNumber>>
    <<case 4 5>>
    It's a moderately lucky day!
    <<case 6>>
    It is the luckiest of days!
    <<default>>
    No luck for you!
    <</switch>>
    
    Shown (since last time): <<switch>> macro.
    Yeah I'm using (set: $luck to random: 1, 6) and that throws an error so I try (set: $luck to random:(1, 6)) and no luck so I try (set: $luck to random [1, 6])) ECT
    It just keeps telling me that it's missing a ( after the argument list which I thought was the (1, 6)
    So unless I'm translating it wrong as far as the inner brackets go (which I have tried every possible inner bracket I could think of) then I just don't know.
  • StarFire wrote: »
    Yeah I'm using (set: $luck to random: 1, 6) and that throws an error so I try (set: $luck to random:(1, 6)) and no luck so I try (set: $luck to random [1, 6])) ECT
    It just keeps telling me that it's missing a ( after the argument list which I thought was the (1, 6)
    So unless I'm translating it wrong as far as the inner brackets go (which I have tried every possible inner bracket I could think of) then I just don't know.
    If you're using SugarCube 2, as you tagged the thread, I've shown you the exact syntax you need—several times now. As I also noted previously, the syntax you are trying to use is reminiscent of Harlowe syntax. What story format are you actually using?

    If your tag is correct, then the following is what you want:
    <<set $lucky to random(1, 6)>>
    
    There is no translating, that is the exact code you need to enter.
  • So, we're just repeating dots I've already connected now?

    Yes. I was going to write something else but it didn't make sense so without a delete post function I edited my post into something.
Sign In or Register to comment.