WhenReady

Random thoughts off the top of my head. Woke up with this in my head this morning.

It's a function that operates like on ready {, but you can call it more than once to queue up multiple scripts.

<function name="WhenReady" parameters="params, script">
  if (not IsDefined("params")) {
    params = NewDictionary()
  }
  if (TypeOf (params) = "script" and not TypeOf(script) = "script") {
    // allow parameters in either order, for convenience
    temp = params
    params = script
    script = temp
  }
  if (not HasAttribute (game, "readyqueue")) {
    game.readyqueue = NewList()
    whenready.enabled = true
  }
  if (GetBoolean (whenready, "isrunning") and ListCount(game.readyqueue) > 0) {
    // When a script is running, we likely want a newly-added one to run immediately
    newlist = NewList()
    list add (newlist, QuickParams ("script", script, "params", params))
    game.readyqueue = ListCombine (newlist, game.readyqueue)
  }
  else {
    list add (game.readyqueue, QuickParams ("script", script, "params", params))
  }
</function>

<function name="RedoThis">
  if (HasAttribute (game, "readyinvokedmenu")) {
    newlist = NewList()
    list add (newlist, game.readyinvokedmenu)
    if (HasAttribute (game, "readyqueue")) {
      newlist = ListCombine (newlist, game.readyqueue)
    }
    game.readyqueue = newlist
  }
  else {
    game.redothis = true
  }
</function>

<turnscript name="whenready">
  <enabled />
  <script>
  on ready {
    this.isrunning = true
    if (HasAttribute (game, "readyqueue") and not HasAttribute(game, "menucallback")) {
      hash = ListItem (game.readyqueue, 0)
      invoke (DictionaryItem (hash, "script"), DictionaryItem (hash, "params"))
      if (HasScript (game, "menucallback")) {
        game.readyinvokedmenu = hash
      }
      else {
        game.readyinvokedmenu = null
      }
      if (GetBoolean (game, "redothis")) {
        game.redothis = null
      }
      else {
        list remove (game.readyqueue, hash)
        if (ListCount (game.readyqueue) = 0) {
          game.readyqueue = null
          this.enabled = false
        }
      }
      do (this, "script")
    }
    this.isrunning = false
  }
  </script>
</turnscript>

Example usage:

WhenReady() {
  ShowMenu ("What is your favourite colour?", Split("red;green;blue;yellow"), false) {
    player.colour = result
    if (result = "yellow") {
      WhenReady {
        Ask ("Would you like a banana?") {
          if (result) {
            msg ("Well, tough. This was just an example to show that you can nest calls to WhenReady.")
          }
        }
      }
    }
  }
}
WhenReady() {
  ShowMenu ("What is your favourite cheese?", Split("cheddar;swiss;blue;brunost;edam"), false) {
    player.cheese = result
  }
}
WhenReady() {
  ShowMenu ("Is there any point in this?", Split("no;not really;yes;fish"), false) {
    if (result = "fish") {
      msg ("Stupid answer! Try again!")
      RedoThis()
    }
    else {
      msg ("I quite agree!")
    }
  }
}
WhenReady() {
  msg ("Thanks for answering the questions!")
  finish()
}

(This was just off the top of my head, not tested yet. But wondered if it would give anyone some ideas)


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

Support

Forums