Question: how to "wait" for a function to "end"
LoveMeTende
06 Nov 2022, 19:28Hello everyone, it's my first post here.
I just started "programming", so forgive me if my question is stupid :D
So basically, I was stuck with the creation of a loop menu. I solved the problem by making a function. In this function I run a script with caption, there is a question and three options to choose. For the sake of semplicity, the options are: sit, stand and remain immobile. After the player choice, I run a switch for the result, and print a certain message when player sit or stand. But, when he chooses to remain immobile, I print a message and then I call again the function.
(It's a little bit more complicated than that, because I made a counter that goes up everytime the player chooses to remain immobile, for the sake of printing a message when he reaches a certain point. But that's not the problem.)
So, in the game, when player eneters the room I call the function and the loop stuff works fine. But, if I add after the "call function" another script, this will be run at the same time. Is there a way to "pause" other scripts while the function is running?
mrangel
07 Nov 2022, 08:19You're not really waiting for the function to end; because the function ends as soon as the menu is displayed - and is run again when the player chooses a certain option. So what you're waiting for is for the point where control is returned to the player with no menu active.
I'd suggest putting a turnscript in the room. I don't have Quest on this computer, but I think you can use a condition like: if (not HasScript (game, "menucallback"))
to make sure there isn't a menu, and if so do the next thing. And if so, your turnscript can then disable or delete itself.
I did post a piece of code months ago (maybe even years) with a modified version of ShowMenu
and OnReady
which would do this for you in the background, but it might be hard to find it in the forum.
mrangel
07 Nov 2022, 11:24I think this is one copy of that code: [link].
With that OnReady
function, you could do:
YourFunction ()
OnReady() {
// code here will run after resolving any menus created by YourFunction
}
LoveMeTende
07 Nov 2022, 17:03Thank you for the reply!
So, I created an OnReady function and copy-pasted your code in it: you saved me!
It seems to work fine! I must say: I don't fully understand how it works, but it does the job!
Thank you very much mrangel!
mrangel
07 Nov 2022, 17:28No worries :)
I think there's a better (more resilient) version of that function on the forums somewhere, but I don't usually keep track of my old posts. The later version included a modified version of the ShowMenu
function as well, which would automatically wait for any existing menus to resolve before displaying a new one, and a function which would repeat the current menu without needing to put it in a function.