Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Zany on Fri 13/10/2017 09:25:23

Title: Set boolean as global variable still cant retrieve it in room script from dialog
Post by: Zany on Fri 13/10/2017 09:25:23
My global variable that I'm trying to retrieve is: bool interacted = false;

Dialog script command used: EGO:I should find a good spot to place my lunch
                              cEgo.FaceObject(object[5]);
                              interacted = true; <--- This command

Room script: function room_AfterFadeIn(){
              dialogInteract();
 
              if(interacted == true){
                  oLunchSpot.Visible = true;
                  oLunchSpot.SetView(2);
                  oLunchSpot.Animate(2, 3, eOnce, eBlock);
                  hLunchSpot_UseInv();
             }
   
              if(iLunch.GetProperty("lunched") == true){
                 gFade.Visible = false;
                 FadeOut(30);
                 oLunch.Visible = false;
                 room_Load();
            } 
        }

So basically Ego needs to have a discussion with someone and once the dialog has ended then an animation should take place and a new scene will take place.

Title: Re: Set boolean as global variable still cant retrieve it in room script from dialog
Post by: Snarky on Fri 13/10/2017 09:55:00
To insert code into your posts in a nice way, use the "code=ags" tag, like this: [code=ags]Code goes here[/code]

The explanation of your problem is almost certainly that you haven't made your variable global in the sense of being shared between scripts. To do so, you need to add the line "export interacted;" to your script where you define the variable, and "import bool interacted;" to the header of that script.
Title: Re: Set boolean as global variable still cant retrieve it in room script from dialog
Post by: Zany on Fri 13/10/2017 14:04:25
Sorry about that and thank you for the tip will definitely post my code in that format next time. My only query is that I have made use of the "global variable pane" to initialise this variable and in my understanding import and export should not be needed, I am not sure if that is correct.
Title: Re: Set boolean as global variable still cant retrieve it in room script from dialog
Post by: Snarky on Fri 13/10/2017 14:31:38
Yeah, that's fine.

Looking at your code more closely, I don't think this has anything to do with the global variable, which is probably working fine. It's more likely about how dialogs are started. Presumably dialogInteract() is a custom function that calls Dialog.Start() on the relevant dialog? The problem is that dialogs aren't launched immediately when you call Dialog.Start(), they're launched after all the user script code for this turn has ended.

So all the code after the dialogInteract() doesn't run after the dialog, but before it. Therefore the variable isn't set yet, and the condition isn't true.
Title: Re: Set boolean as global variable still cant retrieve it in room script from dialog
Post by: Zany on Fri 13/10/2017 16:06:43
So that makes perfect sense however I still need the condition to be true before it executes the next scene.

This is all the code I've used above(Just so that it makes more sense):
Code (ags) Select

function room_Load()
{
  if(iLunch.GetProperty("lunched") == true){
    Wait(20);
    oEmptyLunch.Visible = true;
    FadeIn(30);
    gFade.Visible = true;
  }
}

function hLunchSpot_UseInv(){

  if(cEgo.ActiveInventory == iLunch)
  {
    oLunch.Visible = true;   
    iLunch.SetProperty("lunched", true);
  }
  cEgo.LoseInventory(iWrench);
  oLunch.Visible = true;
}

function dialogInteract()
{
  if(iWrench.GetProperty("wrenched") == true){
    if(cEgo.HasInventory(iWrench)){
      player.Walk(165,185,eBlock);
      dWrenchFound.Start(); 
    }else{
      dCongrats.Start();
    }
  }
}


Any Ideas how to get my condition set to true after dialog has occurred but before the next scene takes place. Much Appreciated
Title: Re: Set boolean as global variable still cant retrieve it in room script from dialog
Post by: Snarky on Sat 14/10/2017 00:04:12
Basically, you can't put any code after a Dialog.Start() â€" it won't run when it's supposed to.

There are two workarounds. One is to put all the code that is supposed to happen after the dialog inside the dialog. Since you can put normal AGS script into dialog scripts as long as you indent it, this usually works fine. However, calls like room changes might be problematic (I haven't played around with it).

The other is to just set a flag in the dialog, and check that flag in repeatedly_execute(). So take a global bool isInterActionDialogOver, and
put in the dialog script:

Code (ags) Select
@4
character: blah blah blah.

  isInterActionDialogOver = true

stop


And in the global script or room script:

Code (ags) Select
function repeatedly_execute()
{
  if(isInteractionDialogOver)
  {
    if(interacted == true){
      oLunchSpot.Visible = true;
      oLunchSpot.SetView(2);
      oLunchSpot.Animate(2, 3, eOnce, eBlock);
      hLunchSpot_UseInv();
    }
    if(iLunch.GetProperty("lunched") == true){
      gFade.Visible = false;
      FadeOut(30);
      oLunch.Visible = false;
      room_Load();
    }

    // Reset flag so this doesn't run next game loop
    isInteractionDialogOver = false;     
  }
Title: Re: Set boolean as global variable still cant retrieve it in room script from dialog
Post by: Zany on Mon 16/10/2017 10:00:37
Thank You (nod)