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
That's probably not what you're asking, but sometimes it's crazy how we can overlook the simplest solutions. ;-)
Yeah, just check the $foo in my example, yes?
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.
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.
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!) You will see that while the second random number may be different $foo still the same as the first random number.
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:
Here is the code in it:
Twine 1.4.2. It's Sugarcane, but the code is the same for SugarCube.
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.