Author Topic: Set boolean as global variable still cant retrieve it in room script from dialog  (Read 155 times)  Share 

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
                              interacted = true; <--- This command

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

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.


  • Global Moderator
  • Mittens Earl
  • Private Insultant
    • I can help with proof reading
    • I can help with translating
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.

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.


  • Global Moderator
  • Mittens Earl
  • Private Insultant
    • I can help with proof reading
    • I can help with translating
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.

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: Adventure Game Studio
  1. function room_Load()
  2. {
  3.   if(iLunch.GetProperty("lunched") == true){
  4.     Wait(20);
  5.     oEmptyLunch.Visible = true;
  6.     FadeIn(30);
  7.     gFade.Visible = true;
  8.   }
  9. }
  11. function hLunchSpot_UseInv(){
  13.   if(cEgo.ActiveInventory == iLunch)
  14.   {
  15.     oLunch.Visible = true;    
  16.     iLunch.SetProperty("lunched", true);
  17.   }
  18.   cEgo.LoseInventory(iWrench);
  19.   oLunch.Visible = true;
  20. }
  22. function dialogInteract()
  23. {
  24.   if(iWrench.GetProperty("wrenched") == true){
  25.     if(cEgo.HasInventory(iWrench)){
  26.       player.Walk(165,185,eBlock);
  27.       dWrenchFound.Start();  
  28.     }else{
  29.       dCongrats.Start();
  30.     }
  31.   }
  32. }

Any Ideas how to get my condition set to true after dialog has occurred but before the next scene takes place. Much Appreciated


  • Global Moderator
  • Mittens Earl
  • Private Insultant
    • I can help with proof reading
    • I can help with translating
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: Adventure Game Studio
  1. @4
  2. character: blah blah blah.
  4.   isInterActionDialogOver = true
  6. stop

And in the global script or room script:

Code: Adventure Game Studio
  1. function repeatedly_execute()
  2. {
  3.   if(isInteractionDialogOver)
  4.   {
  5.     if(interacted == true){
  6.       oLunchSpot.Visible = true;
  7.       oLunchSpot.SetView(2);
  8.       oLunchSpot.Animate(2, 3, eOnce, eBlock);
  9.       hLunchSpot_UseInv();
  10.     }
  11.     if(iLunch.GetProperty("lunched") == true){
  12.       gFade.Visible = false;
  13.       FadeOut(30);
  14.       oLunch.Visible = false;
  15.       room_Load();
  16.     }
  18.     // Reset flag so this doesn't run next game loop
  19.     isInteractionDialogOver = false;      
  20.   }