Frictional Games Forum (read-only)

Full Version: Preventing scripts from repeating
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I was thinking about a problem one might encounter when making an area that the player character walks through several times. Lets say that you have a function that pulls the players screen toward an entity and then makes some screen effects happen. What do you do if you only want these effects to happen the first time the character goes through the area?
AddEntityCollideCallback("Player", "ScriptArea_1", "Func01", true, 1);

The bolded text above with it saying "true" means that it can only happen once. When it is "false", then it can happen as many times as you want.

If you want it to happen 2 times, for example, then you use a local variable integer to increase the value when it runs the first time and second time, and if it equals 3, then it breaks out of the function before running it for a 3rd time.
So...even if you exited that specific map, and went into another, and then returned the the previous map with the function you only want to happen once, the function wouldn't happen again?
It doesn't matter if you leave a map and then come back to it, it would still work if it is set to false, else if it is true, then it would only happen once. If you wanted to have it only work when you are in that map, and if you leave it and come back, you could have it stop working. For example:

Code:
void OnStart()
{
     SetLocalVarInt("Var01", 0);
     AddEntityCollideCallback("Player", "ScriptArea_1", "Func01", true, 1);
}
void Func01(string &in asParent, string &in asChild, int alState)
{
     if (GetLocalVarInt("Var01") == 0)
     {
          //Do whatever you want to happen here.
          Func02();
     }
}
void Func02()
{
     AddEntityCollideCallback("Player", "ScriptArea_1", "Func01", true, 1);
}
void OnLeave()
{
     SetLocalVarInt("Var01", 1);
}

If you notice in the script above, I set it to "true" for the AddEntityCollideCallback command functions. There are ways like this that don't require for it to be "true".
Im sorry to continue this, but I think I'm on the verge of understanding it. What I understand is that the variable is set to 0 on the starting of the map. then the function checks to see if the variable is 0, and if it is then the function is done, and on leaving the map, the variable is then set to 1, so that theoretically if the function was run again it would be stopped at the variable check.

So lets say that you exited that map through a level door and loaded up a new map with a new script, and then returned to the first map- wouldnt the onstart() function reset the variable to 0, therefore causing the function to run again?

I am very thankful for all of your replies.
Yes it would run again (if the addcollide & setlocalvar was placed under OnEnter() ) . Kyle explained how you get a function to run once everytime you enter a map. If you want it to run once "ever" then simply use this line:
AddEntityCollideCallback("Player", "ScriptArea_1", "Func01", true, 1);
What I remember is onStart only runs on the first time the map loads, and onEnter runs every time. So since the setting of Var01 to 0 only happens the first load, but is set to 1 whenever the map is left, it will only happen the first time you've entered the map.