Things to do with 'while'

K.V.

The only thing I've ever gotten out of while is something like this:

while (not player.happy) {
  foreach (o, AllObjects()) {
    msg (o)
    if (o = beer) {
      player.happy= true
    }
  }
}

I use it to wait for all of my start scripts to run before the player can do anything when the game initially loads up.


NOTES:

If "happy" is not an attribute which is set on 'player', it will throw this error:
Error running script: Error compiling expression 'not player.happy': NotElement: Operation not defined for type 'Object'


If 'beer' is not an object, it throws this one:

Error running script: Error compiling expression 'o = beer': Unknown object or variable 'beer'


BUT, if you use if (o.name = "beer") { when beer is NOT an object name, your game will freeze up. In fact, Quest freezes up. (The condition is never met, so while just keeps on waiting!)

image


I've never got any type of player input loop thingy to work using while, either.

I theorize that's because it's the opposite of what while is for.

It's purpose is to only allow its script to run until whatever condition is met, and Quest can't handle more than one instance of ask, get input, ShowMenu, or anything of that nature. (I read that in the docs or on the forum somewhere, but I can't find it now.)


Anyway, what do YOU use the while function for?

Something cool? Something boring?

Either way, let us know!


Most recent use that pops into my head is when you want to randomize a list. Example:

input_list = Split("foo,bar,baz,quux,thingy", ",")
output_list = NewStringList()
while (ListCount(input_list) > 0) {
  item = PickOneString (input_list)
  list add (output_list, item)
  list remove (input_list, item)
}

(I used a variant of this for my random dungeon; the dungeon generator keeps running as long as there are rooms that need to be connected up)


Another thought: character is staggering around drunkenly.

hasexits = 1
while (player.drunk*player.health*hasexits > 0) {
  player.drunk = player.drunk - GetRandomInt(1, player.parent.scariness)
  if (player.drunk <= 0) {
    player.drunk = 0
  }
  else {
    exit = PickOneExit (player.parent)
    if (exit.locked) {
      msg ("You walk into the door")
      player.health = player.health - GetRandomInt(2,10)
    }
    else {
      msg ("You stagger drunkenly "+exit.alias)
      player.parent = exit.to
      hasexits = ListCount (ScopeExits())
    }
  }
}

Now, you could have a drunkenness script that makes you stagger in random directions using a for loop to count the number of moves. But in this case, it ensures that the loop ends when either your drunkness or your health hits zero (quite useful if there are rooms around there that will damage you on arrival), or if you somehow end up in a room without exits.

A while loop is for things that you want to keep on doing as long as a condition is true.


in normal programming, using 'while / do-while' loops, instead of tail-recursion, is much more efficient. (Tail-recursion can always be replaced by a 'while / do-while' loop). But, with quest... you got the issue as KV pointed out already.


K.V.

Nice!

...and, while the player is drunk, the player has no control at all, and the game just keeps printing turns to the screen until the condition is met, right?


K.V.

with quest... you got the issue as KV pointed out already.

I'm just copying and pasting stuff. (I keep trying to tell everyone that.)

https://textadventures.co.uk/forum/quest/topic/6074/wait-while-a-loop-recursive-function-is-running#41995


man, my memory is getting worse and worse, totally forgot about that thread/post, laughs. KV has been doing a lot of digging/research, lol, as that post of mine is like 1-2 years old, lol.


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

Support

Forums