Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: evildustmite on Wed 12/11/2014 13:42:49

Title: [SOLVED] trying to make a region event only happen once. Not working :(
Post by: evildustmite on Wed 12/11/2014 13:42:49
ok, What I want to happen is when a character walks in front of the window of the room, a camera will flash outside, but I only want this to happen once.

here is the script I wrote,
Code (ags) Select
function region1_WalksOnto()
{
  int Flash;
  Flash = 1;
 
  if (Flash > 0)
    {Wait(40);  //waits 1 second 
    oWindowFlash.Visible = true;//turns on flash
    Wait(5);                    //waits(keeps flash on for 1/8th of a second
    oWindowFlash.Visible = false;}//turns flash off
    {Flash --; //subtracts 1 from variable Flash
  }
 
}


everything works except that it does it every time I walk in front of the window.
Title: Re: trying to make a region event only happen once. Not working :(
Post by: Vincent on Wed 12/11/2014 13:52:04
Quote from: evildustmite on Wed 12/11/2014 13:42:49
I only want this to happen once.

booleans

Code (ags) Select

bool first_time = true;
function region1_WalksOnto()
{
  int Flash;
  Flash = 1;
 
  if (Flash > 0 && first_time)
    {Wait(40);  //waits 1 second 
    oWindowFlash.Visible = true;//turns on flash
    Wait(5);                    //waits(keeps flash on for 1/8th of a second
    oWindowFlash.Visible = false; //turns flash off
    first_time = false;
    // Flash = 0;
   }
   // {Flash --; //subtracts 1 from variable Flash
  }
 
}
Title: Re: trying to make a region event only happen once. Not working :(
Post by: NickyNyce on Wed 12/11/2014 14:01:56
You could also look into this.
Quote from: evildustmite on Wed 12/11/2014 13:42:49

Code (ags) Select
function region1_WalksOnto()
{
  if(Game.DoOnceOnly("FlashOnce"))

Title: Re: trying to make a region event only happen once. Not working :(
Post by: Crimson Wizard on Wed 12/11/2014 14:07:48
Guys, you really did not explain why it did not work with the first code. This is very important.

evildustmite, when you declare a variable inside the function, it will reset every time you run the function again, therefore the value you set won't be remembered. That is why it runs every time.

The simpliest fix is to carry the variable out of the function:

Code (ags) Select

int Flash; // will be 0 at game start
function region1_WalksOnto()

  if (Flash == 0)
  {
    Wait(40);  //waits 1 second 
    oWindowFlash.Visible = true;//turns on flash
    Wait(5);                    //waits(keeps flash on for 1/8th of a second
    oWindowFlash.Visible = false;//turns flash off
    Flash++; //adds 1 to variable Flash
  }
}


I made this example to show that you had a correct idea in general, with the exception of how to declare a variable.
The variables can be local and global; local (declared inside function) are destroyed when the function ends, and re-created when it is run again. Global variables exist until game ends.

But ofcourse, for the cases like this you may want to use Game.DoOnceOnly, as NickyNyce suggested:
http://www.adventuregamestudio.co.uk/wiki/Game_/_Global_functions#Game.DoOnceOnly
Title: Re: trying to make a region event only happen once. Not working :(
Post by: evildustmite on Wed 12/11/2014 14:14:44
thanks Crimson Wizard, I had just had this same idea when I saw that Vincent had placed the boolean variable outside the function and thought maybe that was what my problem was. that everytime the function ran the variable was reset. so I placed it outside of the function and it works correctly now.
Title: Re: trying to make a region event only happen once. Not working :(
Post by: Vincent on Wed 12/11/2014 14:21:59
Oh well, or if you just miss this part from the manual :)

Global Variables


If you just need to store some information locally (for example, a "door opened" flag that only applies to one particular room)
then you should declare the variable manually at the top of the room's script file instead.


How do I use global variables ?

Code (ags) Select

if (myVariable == 3)
{
  myVariable = 4;
}
Title: Re: trying to make a region event only happen once. Not working :(
Post by: evildustmite on Wed 12/11/2014 14:35:58
This is my first time doing this type of code, the only real computer language I've ever learned was some BASIC in middle school,(my dad taught me some as well when I was really young) and we never did anything real advanced. So I understand variables and how they work, but functions are new to me, I understand how they work, but since it's new to me and I don't have much experience in programming this way, it's going to take a bit of time till my brain thinks in the correct way.
I really do appreciate everybody's help.