Stop script before the dialog starts

Started by Questor, Tue 19/07/2016 23:50:40

Previous topic - Next topic

Questor

Hi, I have this problem which might be simple, but unfortunately I can't come up with any solution.
I have a room script. Let's say it looks like this:

Code: ags

dMain.Start();
if (res == 0) {cEgo.ChangeRoom(1);}
else if (res == 1) {cEgo.ChangeRoom(2);}
else {cEgo.ChangeRoom(3);}


dMain starts a branching dialog, where the end options set the value of the global variable res. When I run it, the dialog is being "ignored" and res still holds its default value.
I've tried to use the solution provided in this topic, but it didn't work.

Crimson Wizard

Yes, Dialog.Start is not executed immediately, but queued and run at the end of the function. Same thing happens with ChangeRoom command.

Quote from: Questor on Tue 19/07/2016 23:50:40
I've tried to use the solution provided in this topic, but it didn't work.
Please elaborate, what exactly did you try to do, and what does "didn't work" mean (there was an error, something did not work as intended - what and how?).

morganw

Quote from: Questor on Tue 19/07/2016 23:50:40the end options set the value of the global variable res
Would it not be easier to avoid the event queue and just call ChangeRoom from the end options?

Khris

Yes, you can put normal script commands in Dialog scripts if you indent them by at least one space.

Also, in general, you can probably do this instead of three lines you have:
Code: ags
 cEgo.ChangeRoom(res + 1);

Questor

The script I've presented is just a simplified version of the real thing. There are a lot more lines for every outcome and I just don't want to copy&paste all that to every end option. If I'll have to then I will, but I was just wondering if there's a more elegant way to handle this.
As to the solution from the other topic, I've created a boolean global variable, added it at the end of every end option and created a repeatedly execute just as instructed. Nothing has changed.

Crimson Wizard

#5
Quote from: Questor on Wed 20/07/2016 07:53:04
The script I've presented is just a simplified version of the real thing. There are a lot more lines for every outcome and I just don't want to copy&paste all that to every end option. If I'll have to then I will, but I was just wondering if there's a more elegant way to handle this.

You could try creating a function in your global script, declare it with "import" keyword in the script header, and then you can call it from dialog script.
For example:

in global script header:
Code: ags

import function AfterDialog(int res);


in global script body:
Code: ags

function AfterDialog(int res)
{
   if (res == 0) {cEgo.ChangeRoom(1);}
   else if (res == 1) {cEgo.ChangeRoom(2);}
   else {cEgo.ChangeRoom(3);}
}


in the dialog:
Code: ags

@1
 AfterDialog(1);
stop


E: Two more things:
- you can also create such function in any custom script module, except room modules, because latter cannot declare functions for external use. If you really want to put such function into a room script, you will have to use CallRoomScript from dialog, instead of calling custom function directly.
- you should only call ChangeRoom, or your custom function calling ChangeRoom, from option that has "stop" in the end, because room change will only occur after dialog ends.

Khris

Questor, since you tried the method suggested by me, how did you add room_RepExec()? Because it has to be linked in the room's events or it won't get called.

Questor

I've used Crimson Wizard's solution and it works great. Thank you all for your help :)

Quote from: Khris on Wed 20/07/2016 13:40:12
Questor, since you tried the method suggested by me, how did you add room_RepExec()? Because it has to be linked in the room's events or it won't get called.

I've created the function through Events in room's settings and filled it just as you've instructed in that other topic.

Khris

Just fyi: I tried the solution I suggested and it worked on the spot.

Crimson Wizard

#9
That could be anything really, for example, one could mistakenly declare two separate variables and set one in the dialog but check another in repeatedly execute.
Without checking details it would be impossible to know for sure why it did not work.

SMF spam blocked by CleanTalk