Howdy, Stranger!

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

How can I get twine to "remember" variables?

I'm using Sugarcube 1.0.22 and I'm having a quandary with one function.

I'm aware of how to set a random number.
<<set $random = Math.ceil(Math.random()*10)>>
<<print $random>>
This will generate a random number from 1 and 10.

What I'm unsure how to do is how to set the number as a something static once it's revealed, even though it's a variable.

Let's just say it randomly generates the number 6.
You go to another location and it asks you to input the number that was fed to you.

Also if you have to subtract the number from something. Let's say you have $number = 50, and you have to roll enough random numbers to dwindle the number down to 0. So you'd randomly generate the number 8, and then it would display that $number has been knocked down to 42, because it took $number - $random, or 50 - 8, and gave us 42.

Is there any way I could feasibly code this in Sugarcube?

Comments

  • edited May 2015
    What I'm unsure how to do is how to set the number as a something static once it's revealed, even though it's a variable.
    Could you just set it to another variable. Say, <<set $foo= $random>>?

    That's probably not what you're asking, but sometimes it's crazy how we can overlook the simplest solutions. ;-)
    Let's just say it randomly generates the number 6.
    You go to another location and it asks you to input the number that was fed to you.
    Yeah, just check the $foo in my example, yes?
    Also if you have to subtract the number from something. Let's say you have $number = 50, and you have to roll enough random numbers to dwindle the number down to 0. So you'd randomly generate the number 8, and then it would display that $number has been knocked down to 42, because it took $number - $random, or 50 - 8, and gave us 42.

    Is there any way I could feasibly code this in Sugarcube?
    As far as I can tell, you'd just <<set $number = $number - $foo>>.

    You might find my Example Turn-Based RPG (Pre-Alpha v. 0.4) of interest, by the say.

    Hope that helps. Might have misunderstood your questions as I'm in a hurry and reading fast. Sorry if that was all irrelevant.
  • Sharpe wrote: »
    Could you just set it to another variable. Say, <<set $random = $foo>>?

    Hello Sharpe, thank you for commenting. I'm not very well-versed in java, but are you telling me here that if I randomly generate a number, and then put <<set $random = $foo>>, then the generated number in $random will be saved to $foo?

    As in, if you randomly generate the number 2 (from 1 to 10) with $random, then <<set $random = $foo>>, then $foo will be set to a value of 2, as applicable with any other random number generated? If so, that's exactly what I'm looking for.
  • The following will assign a random number to the $random variable and then copy the current value of $random to the $foo variable. So both $random and $foo will contain the same value.
    It then assigns a second random number to the $random variable (which may be the same value as the first time because it is random!)
    <<set $random to Math.ceil(Math.random()*10)>>
    <<set $foo to $random>>
    first random number: <<print $random>>
    foo: <<print $foo>>
    
    <<set $random to Math.ceil(Math.random()*10)>>
    second random number: <<print $random>>
    foo: <<print $foo>>
    
    You will see that while the second random number may be different $foo still the same as the first random number.
  • Crap, sorry, sorry, sorry, I'm an idiot. I don't even have Twine 1 installed right now, so I couldn't test. I almost always test every line of code I ever write as advice, but I didn't this time. My bad. I'm going to edit that post just so I don't throw a newbie for a loop.

    I should have typed <<set $foo = $random>>. That means "make $foo the same as $random."

    So, in answer to you question, yes.

    Seriously, though. Do take a look at the code in that game. No need to even play it more than a quick once over. It's not fun and it isn't meant to be. I recall commenting the heck out of the code, so you can read my advice right beside the lines.

    I tested this code and it worked:
    <<set $random = Math.ceil(Math.random()*10)>>
    <<print $random>>
    <<set $foo = $random>>
    <<print $random>>
    
  • Attached is a TWS and HTML.

    Here is the code in it:
    <<set $random = Math.ceil(Math.random()*10)>>
    <<print $random>>
    
    <<set $foo = $random>>
    <<print $random>>
    
    <<set $bar = Math.ceil(Math.random()*10)>>
    <<set $baz = $bar - $foo>>
    
    <<if $baz < 0>>
    <<set $baz = 0>>
    <<endif>>
    
    <<print $baz>>
    

    Twine 1.4.2. It's Sugarcane, but the code is the same for SugarCube.
  • Thank you both for your answers. I was able to get it working with the following code.

    Passage "RandomGen"

    A number will appear. You must memorize it.

    <<set $random = Math.ceil(Math.random()*10)>>
    <<print $random>>

    EnterRG

    Passage "EnterRG"

    <<set $answer = prompt ("What number was given?")>>

    <<if ($answer = $random)>> <<goto "ThisWorks">>
    <<else>> <<goto "ThisDoesNotWork">> <<endif>>

    By using this code, I was assigned a random number, say 5. If you enter anything other than 5 on the next passage, it takes you to "ThisDoesNotWork", whereas entering 5 will take you to "ThisWorks"

    I think a fundamental misunderstanding for me was not realizing <<set $random = Math.ceil(Math.random()*10)>>already assigned a static value based on a random number. The code literally says "pick a random number from 1 to 10 and assign it to $random"

    Thank you for your helps.
Sign In or Register to comment.