How to do a loop to repeat a script until a condition is met.

Well this is the first time i've run into a script that i think got into an infinite loop, so i'm probably doing something wrong. I suppose i graduated into a higher rank of coding novices to have messed things up to crash a system.

My aim is this, to make a quiz that the player takes, each wrong answer increases the testfail attribute, each right increases the testpass.

For the test itself I have the script randomly generate a number between 1 and 10, then have an if statement that uses else to cover each possible result,ask a question, then increase the proper counter. Very simple and logical

I'd like the test to keep running until either of them reaches 3, then the rest of the script

I've tried to use "while" and set the parameters appropriately and quest has hanged each time i run the test.

If i can't use looks i could always have the player ask the tester for the next question each time, or maybe use a timer to ask a question every some number of seconds, stopping once one hits 3, not as nifty solutions but its what i'll do if i need to.


It is not trivial, but there is a guide to asking questions inside a loop here:
https://github.com/ThePix/quest/wiki/Getting-Input-from-the-Player-inside-a-Loop


Quite interesting, I think I'll start with the clunky method mentioned, since i got it to work last night, but when i'm ready to move on i'll try to make it more elegantly, at this point still not skilled enough to use lists of questions and answers line up..


You're definately learning to code better, hehe, though don't be embarassed about messing up code, as that indeed means you're learning to code: you're learning to code a bit and thus are now able to try to code (which usually results in mistakes, such as endless/infinite loops), whereas before you couldn't even code an endless/infinite loop, as you didn't even know how to code or how to code a loop. Learning to code is a very slow process, with a lot of mistakes and thus a lot of troubleshooting, but this is the learning process of coding, laughs.

endless/inifinite loops are very common, everyone, even programming experts, has done them in their code! The good thing is that the computer is protected and the software you use is protective too. This wasn't the case in the early days of computers, where you could even worsely (if you don't know what you're doing or if you're the victim of a malicious hacker) access the critical computer data/hardware in memory and also the halts/interupts controls/flags of computer processes/IO:input-output, which could completely block you off from using your computer ever again.

Why the infinite/endless loop crashes the software/computer:

the processes/operations/scripting that is the infinite/endless loop, uses resources to store data about it (google search: 'activation records' / 'coding overhead', and/or something like this 'why can recursion be bad' or just read up on 'recursion' and you'll probably eventually find a source that gets into activation records / coding overhead), and while the looping is infinite/endless, the computer's resources are not, and hence the crash. (also if interested further, google search: 'memory leaks' and also 'dangling pointers' )

a loop is simple, dialogue/conversation is not... lol

to do a loop, you can use quest's built-in 'while' loop Function, or you just have a Function call itself within its scripting, or you have a Script Attribute / Verb call itself within its scripting.

while not quite a loop technically, there's also iteration (which can be set up to act like a limited looping loop):

the 'foreach' and 'for' Functions/Scripts

the 'foreach' requires a List Attribute, as it iterates through a List Attribute's items

the 'for' just iterates the number of times (and optionally also its steps/intervals) you specify for it, doing whatever scripting you give it, that many times.


P.S.

I'd recommend taking a look at the 'RandomChance' Function for use with your 'testfail' Integer Attribute (though you'll be needing to decrease your 'testfail' Integer Attribute, for the lowering of your % of success, instead with using the 'RandomChance' Function:

http://docs.textadventures.co.uk/quest/functions/corelibrary/randomchance.html

spoiler:

// the higher/greater your 'testfail' Integer Attribute's Value (which for using RandomChance has to be between 0 and 100), the higher/greater your percent chance of success (if TRUE)

if (RandomChance (game.testfail)) {
  // success script(s)
} else {
  // failure script(s)
}

and an example of having a terminating (non-crashing, not endless/infinite) loop:

(my 'msg' Scripts allow for you to see what is going on with it at every important step --- 'tracking' of: scripting, the order of operations, what scripts are accessable/not-accessable, and also of Attributes' Values changing --- which is what debuggers, and if there was/are any mistakes, be able to easily locate them due to my 'msg' Scripts --- this is an extremely useful 'sure-fire' way of trouble shooting scripting issues)

<game name="example_game">
  <attr name="loop_counter_integer_attribute" type="int">0</attr>
  <attr name="start" type="script">
    msg ("Message 1, and Loop Counter Value: " + game.loop_counter_integer_attribute) 
    example_of_terminating_loop_function
    msg ("Message 5, and Loop Counter Value: " + game.loop_counter_integer_attribute) 
  </attr>
</game>

<function name="example_of_terminating_loop_function"><![CDATA[
  msg ("Message 2 and Loop Counter Value: " + game.loop_counter_integer_attribute) 
  if (game.loop_counter_integer_attribute < 3) {
    msg ("Message 3 and Loop Counter Value: " + game.loop_counter_integer_attribute) 
    game.loop_counter_integer_attribute = game.loop_counter_integer_attribute + 1
    msg ("Message 4 and Loop Counter Value: " + game.loop_counter_integer_attribute) 
    example_of_terminating_loop_function
  }
]]></function>

Yeah, though recent to learning programming i've been around computers most of my life, in the nitty gritty days of dos and having to manage 640k conventional memory and when floppy disks were floppy.. and i know how easy it was to MAJORLY screw up things, hell had to reinstall windows 95 and 2k so many times because of my..dabbling with the code messed things up so bad a clean install was really the only thing..admittedly SOME of those habits i keep are still quite smart.(Backups that aren't connected online) but anyhow

Have learned the joys of having simple messages for debuggers in many cases, it tells me also which of alot of turnscripts/timers might be misfiring as well since those are many times global and often harder to pin down

Though really don't see how a random number for the testfail figures in, each question has a right answer and that adds to your score, not just a random roll of the dice sort of test. using it to keep scores of the strikes someone has. using random to make the test questions chosen for the player vary a bit for replayablity


This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums