Issues with repeatedly execute function

Started by Glenjamin, Thu 16/06/2016 19:05:30

Previous topic - Next topic

Glenjamin

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


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!

CaptainD

Maybe you could set a bool to skip the routine once it's been done?

Crimson Wizard

One of the builin methods to ensure that some action takes place only once in AGS is Game.DoOnceOnly:
Code: ags

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

(the string you pass to DoOnceOnly is arbitrary, but unique to identify particular case)

dayowlron

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.
Pro is the opposite of Con                       Kids of today are so much different
This fact can clearly be seen,                  Don't you know?
If progress means to move forward         Just ask them where they are from
Then what does congress mean?             And they tell you where you can go.  --Nipsey Russell

Danvzare

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

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;
}
}

Crimson Wizard

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.

Danvzare

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.

Khris

Here's the standard way, like dayowlron suggested:
Code: ags
// 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.)

SMF spam blocked by CleanTalk