Frictional Games Forum (read-only)
Preventing scripts from repeating - Printable Version

+- Frictional Games Forum (read-only) (https://www.frictionalgames.com/forum)
+-- Forum: Amnesia: The Dark Descent (https://www.frictionalgames.com/forum/forum-6.html)
+--- Forum: Custom Stories, TCs & Mods - Development (https://www.frictionalgames.com/forum/forum-38.html)
+---- Forum: Development Support (https://www.frictionalgames.com/forum/forum-39.html)
+---- Thread: Preventing scripts from repeating (/thread-9823.html)



Preventing scripts from repeating - rybray - 08-17-2011

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?


RE: Preventing scripts from repeating - Kyle - 08-17-2011

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.


RE: Preventing scripts from repeating - rybray - 08-17-2011

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?


RE: Preventing scripts from repeating - Kyle - 08-17-2011

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".


RE: Preventing scripts from repeating - rybray - 08-17-2011

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.


RE: Preventing scripts from repeating - Acies - 08-18-2011

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



RE: Preventing scripts from repeating - MegaScience - 08-18-2011

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.