Making recursion work with commands.

Hi,

(note: I've since realized that its better to have enemy attacks as a turn script, over a retaliation to a command, but still asking this question for clarity purposes)

I'd like to know how to get recursion / while loops to work with commands. I created a simple program, to give an opponent 4 attacks, and thus it would repeat the attack code each time for each attack and that it should attack the player with these 4 attacks, every time the player would use the command. This works properly the first time I use command. However, every time I use the command afterwards, the loop does not initiate. I've created debug commands to post on the screen what the current value is for the Boolean condition for the loop and it should be initiating the loop, but it isn't.

Why does a while loop OR a for loop only work once, with the use of a command but will refuse to work every time afterwards?


are you using an Attribute for your condition? If so, did you remember to change it's Value back, so that the while loop will do its scripting again?

for example:

game.boolean_attribute = false

// Command's scripting:

while (not game.boolean_attribute) {
  msg ("hi")
  game.boolean_attribute = true // short while loop, lol
}

// you do the Command again:

the 'while' checks the condition (game.boolean-attribute: true), since it's not 'not true', and thus, it doesn't do the while's (nested) scripting.

// the fix:

// Command's scripting:

while (not game.boolean_attribute) {
  msg ("hi")
  game.boolean_attribute = true // short while loop, lol
}
game.boolean_attribute = false

by the way, Pertex pointed out to me that you can call/re-call a Command (as it is an OBJECT, quest's 'Elements' are its OBJECTS - underlying code of quest's OOD/OOP design, not to be confused with the user-level 'Object' Element), just as you would a Function or a Script Attribute, though I can't remember it's exact syntax... argh... and can't remember where Pertex' post was on it.... argh....


also... you can always use a Function or Script Attribute within the Command's scripting for doing looping/recursion too.


I forgot to add in the parenthesis for my 'msg' Scripts, editted them in now...


another possible issue:

if you're iterating through a list... it might be remaining at the end of the list when you do the next iteration of it...

if this is the case, I'm not sure or have forgotten how'd you reset it to be starting at the beginning of the list again...


Hrmph. Well, I must have did something wrong, as I just did a quick test with a command and a while loop, and it properly initiated as expected. Well, if I find out what I did wrong, as I appear to have removed the problematic code and thus can't post it, then I'll update this post with problematic code and see if you guys can figure out its issue... maybe I have an old backup that has the problematic code... I'll have to check.


it's usually human error, even for professional programmers... though sometimes you do actually find a bug in the software/language, and no mistake of your own doing, but 90% of the time at least, it is ourselves - we messed something up in our coding. We're not perfect as we break down and mess up (and nor are machines, as they too break down and mess up, just as we do, hehe). Entropy and/or Corruption exists for both silicon/silver/gold/etc cells/chips and carbon cells/DNA/synapses/neurons :D


If you know it is going to be four times, use a for loop. while loops are best for when neither you nor the software know at the start how often the loop will run, it just has to keep going until it gets it right.

  PlayerMakesAttack(foe)
  for (i, 1, 4) {
    PlayerIsAttackedBy(foe)
  }

The issue though is that I'd want to have the number of attacks vary based on a condition, and thus a for loop wouldn't work. For example; if you were attacking a 5 headed hydra, it gets 5 attacks because it has 5 heads. But if you cut off two heads, it should now have 3 attacks and not 5. I don't think a for loop would work if you changed the number of iterations outside of the loop.


You can do:

for (i, 1, hydra.number_f_heads) {
  PlayerIsAttackedBy(hydra)
}

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

Support

Forums