Hello, I'm new to Twine and this forum and coding in general. I've looked where I know to look and read the resources I'm aware of, yet I cannot resolve this, so I'll just ask.
I was able to create a cute little random dialogue with a receptionist using:
<<set $array = ["foo", "bar", "baz", "qux"]>>
<<set $length = $array.length>>
<<set $i = Math.floor(Math.random()*$length)>>
<<print $array[$i]>>
which Sharpe suggested in
twinery.org/forum/discussion/1581/choosing-at-random-from-an-array. The key feature was that it allowed me to identify the position of the phrase within the array that was printed. Which allows my receptionist to provide a coherent response. The natural progression of course is to grant my reader agency to climb over the desk and initiate combat with the receptionist. Sooo I tried to evolve the above into a looping auto-resolving combat system.
<<for $pchealth and $enemyhealth are gte 1>>
Doesn't work. Because you guessed it: the variables do not change upon each iteration, once "foo" attack is randomly selected and deflected by the "baz" defense, every iteration repeats those same two...uh arguments ad infinitum.
Upon further modification I did find a way to have each iteration select different options, however from what I could tell in the output, each option, "foo", "bar" etc...could only be selected once (like plucking?), afterward the loop just spits out blank space until someone's health is magically reduced to 0.
I would post what I've come up with, but at this point they've mutated beyond comprehension, and Twine can no longer process them.
tl;dr
How do I create a loop that randomly selects components of its array while retaining them, and also allows me to identify which element of the array was utilized in the most recent loop iteration?
I'm pretty sure the answer is temporary variables, but I've been unable to plug them in correctly to create working code.
Also, I have no problem scraping what I've developed thus far and utilizing a more efficient approach.
Comments
SugarCube 2 comes with several randomness functions and methods built-in: either() function, random() function, randomFloat() function, <Array>.pluck() method, Array.random() method, <Array>.random() method (among others). There is no reason to use Math.random() directly.
Also, please use the code tag, not the quote tag, for code.
The code shown doesn't work because the syntax is invalid. You didn't bother to show your random attack code, so I have no idea what's wrong with it.
Something like the following is what you were looking for with your shown code:
Assuming the following setup: You could try something like the following: Or the equivalent:
This still loops the same phrase from my array upon each iteration.
And since it's probably not clear-- what I'm trying to do with the <<if>> statements is trigger health loss because the phrase at position 1 in the $defend array, and those at positions 2 and 5 in the $enemydefend array indicate an attack has inflicted between 1 and 4 damage.
Thanks
That said, try something like the following to do it by index: You'll probably need to police the line breaks a bit, but you should get the idea.