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:
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.
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.