How to wait the entire resolution of a function before continuing the caller ?

Selsynn
22 Aug 2018, 12:40

I have a verb that called a function. The function awaits the answer of the player, but quest go and continue with the instruction after the function.
What do I need to do await all is clear ?

My actual code (I suppress all that is not important for the loop)
Verb

deck.RedoPowerCards = NewObjectList()
PassThroughAllCardsForTheirPower (GetDirectChildren(deck))
while (ListCount(deck.RedoPowerCards) > 0 ) {
  redoCards = deck.RedoPowerCards
  deck.RedoPowerCards = NewObjectList ()
  PassThroughAllCardsForTheirPower (redoCards)
}

Function PassThroughAllCardsForTheirPower

foreach (card, list) {
  if (card.prototype="card" ) {
    player.card = card
    ExecutePowerCard
  }
}

Function ExecutePowerCard

  options = Split( "Yes, now;Wait, not now;I don't want to use this power", ";")
  ShowMenu ("Do you want to run the power ?", options, false) {
    switch (CapFirst(result)){
      case ("Wait, not now","Wait") {
        list add (deck.RedoPowerCards, player.card)
      }
    }
  }

Problem : the menu does not wait in the switch to have the answer before doing the end of the function, and so the end of the caller... So he never go in the while.

Question: how to force quest to await the player result ?
I understand that we can nest the code, but in this case, it's strange to nest as it is in function.

Thank you in advance!


mrangel
22 Aug 2018, 13:15

The function finishes immediately.

ShowMenu does not wait for the player to enter a response. It displays a message to the player, and then makes a note of some code to run when the player responds. So ShowMenu has finished as soon as the menu is displayed, and the function containing it has also finished.

If you've got a loop which gives the player several menus, it might be more efficient to instead set it as a turnscript, which works out the next question the player needs to answer and displays a single menu.

I've dealt with similar situations by giving the game object a "pending_code" attribute, a list of scripts and parameters. You can have a function which adds code to that list, and a turnscript which goes through the list removing items and executing them, until one of them displays a menu which requires user input.


Selsynn
22 Aug 2018, 20:35

Thank you.
I'm redesigning this aspect of the game to get away from the problem.


hegemonkhan
23 Aug 2018, 03:27

(filler for getting my edited post, updated/posted)


unfortunately, with/in quest, its 'while', 'for', and 'foreach' Scripts/Functions can NOT be stopped/breaked/paused (they do NOT wait for player input, and thus you get an ERROR, as the scripting is a lot faster than you can physically input, and thus two (or more) instances of getting inputs come up, which quest/computer can't handle, and hence the ERROR)

so, you got to use a check (the 'if/switch/WHATEVER' Scripts/Functions) along with looping and/or recursion, (aka, calling the function within the function), if you're getting any input by the player, a simple example (using a Function):

<function name="loop_function">
  msg ("loop? (y/n)")
  get input {
    if (result = "y") {
      loop_function
    } else {
      msg ("You chose to quit the looping")
    }
  }
</function>

if you want to have following scripts wait until the above scripts are done... you can try the 'on ready' (but ask mrangel, or pixie or pertex, to explain when/why/how/for-what it works and doesn't work, as it was a bit confusing for me with trying to understand mrangel's, or was it pertex' or pixie ??? - agh - getting old can't remember who it was who explained it to me, lol, attempt at explaining it to me, or jsut try it... if it works... it works... if it doesn't work... it doesn't work... lol):

http://docs.textadventures.co.uk/quest/scripts/on_ready.html

// whatever scripting 1
on ready {
  // scripting 2 that you want to wait before running/executing until the scripting 1 is done
}

if all else (all of the above) fails....

you just got to get more creative with your scripting/code design.... unfortunately (or fortunately, lol)... quest requires you to be creative... some times... lol