Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Questor on Tue 19/07/2016 23:50:40

Title: Stop script before the dialog starts
Post by: Questor on Tue 19/07/2016 23:50:40
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) Select

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 (http://www.adventuregamestudio.co.uk/forums/index.php?topic=53456.msg636534107#msg636534107) topic, but it didn't work.
Title: Re: Stop script before the dialog starts
Post by: Crimson Wizard on Wed 20/07/2016 00:04:08
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 (http://www.adventuregamestudio.co.uk/forums/index.php?topic=53456.msg636534107#msg636534107) 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?).
Title: Re: Stop script before the dialog starts
Post by: morganw on Wed 20/07/2016 01:38:26
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?
Title: Re: Stop script before the dialog starts
Post by: Khris on Wed 20/07/2016 01:59:43
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:
cEgo.ChangeRoom(res + 1);
Title: Re: Stop script before the dialog starts
Post by: 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.
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.
Title: Re: Stop script before the dialog starts
Post by: Crimson Wizard on Wed 20/07/2016 10:05:55
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) Select

import function AfterDialog(int res);


in global script body:
Code (ags) Select

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) Select

@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 (http://www.adventuregamestudio.co.uk/manual/ags56.htm#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.
Title: Re: Stop script before the dialog starts
Post by: 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.
Title: Re: Stop script before the dialog starts
Post by: Questor on Thu 21/07/2016 21:13:38
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.
Title: Re: Stop script before the dialog starts
Post by: Khris on Sat 23/07/2016 14:51:42
Just fyi: I tried the solution I suggested and it worked on the spot.
Title: Re: Stop script before the dialog starts
Post by: Crimson Wizard on Sat 23/07/2016 17:38:23
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.