Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Glenjamin on Thu 16/06/2016 19:05:30

Title: Issues with repeatedly execute function
Post by: Glenjamin on Thu 16/06/2016 19:05:30
In my game, picking a dialog option causes a string of events.
I currently have the if statement in the room's repeatedly execute function.
This is problematic, because once the dialog option is chosen, it repeats the event forever.

Here is my code.

Code (ags) Select


function room_RepExec()
{
if (ddanger.HasOptionBeenChosen(1)){
   ddanger.SetOptionState(2, eOptionOn);
    ddanger.SetOptionState(3, eOptionOn);
}
if (ddanger.HasOptionBeenChosen(3)){
   cego.Walk(45, 60, eBlock, eAnywhere);
}
}


How can I solve this problem? Thank you!
Title: Re: Issues with repeatedly execute function
Post by: CaptainD on Thu 16/06/2016 19:17:33
Maybe you could set a bool to skip the routine once it's been done?
Title: Re: Issues with repeatedly execute function
Post by: Crimson Wizard on Thu 16/06/2016 19:30:29
One of the builin methods to ensure that some action takes place only once in AGS is Game.DoOnceOnly:
Code (ags) Select

if (ddanger.HasOptionBeenChosen(3) && Game.DoOnceOnly("DangerOption3Chosen")){

(the string you pass to DoOnceOnly is arbitrary, but unique to identify particular case)
Title: Re: Issues with repeatedly execute function
Post by: dayowlron on Thu 16/06/2016 20:52:44
I may be totally off base but if you really just want it to run once during the whole game then Crimson Wizard's would work great since that is what DoOnceOnly was created for, but why are you doing this code in the RepExec function.
How about in the dialog code for option 1 you turn on option 2 and 3, and the dialog code for option 3 you put your walk command.
Title: Re: Issues with repeatedly execute function
Post by: Danvzare on Fri 17/06/2016 09:56:03
Like everyone says, it would be better to use DoOnceOnly. But if for whatever reason you want to use RepExec, here's the code I would use.

Code (ags) Select

function room_RepExec()
{
if (ddanger.HasOptionBeenChosen == 1){
    ddanger.SetOptionState(2, eOptionOn);
    ddanger.SetOptionState(3, eOptionOn);
    ddanger.HasOptionBeenChosen = 0;
}
if (ddanger.HasOptionBeenChosen == 3){
    cego.Walk(45, 60, eBlock, eAnywhere);
    ddanger.HasOptionBeenChosen = 0;
}
}
Title: Re: Issues with repeatedly execute function
Post by: Crimson Wizard on Fri 17/06/2016 10:05:22
Quote from: Danvzare on Fri 17/06/2016 09:56:03
Like everyone says, it would be better to use DoOnceOnly. But if for whatever reason you want to use RepExec, here's the code I would use.
Danvzare, your code won't even compile... HasOptionBeenChosen is a function, and you are using it as a variable.
Title: Re: Issues with repeatedly execute function
Post by: Danvzare on Fri 17/06/2016 13:04:59
Quote from: Crimson Wizard on Fri 17/06/2016 10:05:22
Quote from: Danvzare on Fri 17/06/2016 09:56:03
Like everyone says, it would be better to use DoOnceOnly. But if for whatever reason you want to use RepExec, here's the code I would use.
Danvzare, your code won't even compile... HasOptionBeenChosen is a function, and you are using it as a variable.
Doh. I did a dumb thing. :-[
Thanks for the correction.
Title: Re: Issues with repeatedly execute function
Post by: Khris on Sat 18/06/2016 12:36:43
Here's the standard way, like dayowlron suggested:
// dialog script
...
@1
...
option-on 2
option-on 3
return

@2
...

@3
...
  cego.Walk(45, 60, eBlock, eAnywhere);
...
return


(Note that the .Walk() command is indented, while option-on X and return are standard dialog commands that mustn't be indented.)