Facebook Twitter YouTube Frictional Games | Forum | Privacy Policy | Dev Blog | Dev Wiki | Support | Gametee


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Preventing scripts from repeating
rybray Offline
Junior Member

Posts: 31
Threads: 9
Joined: Aug 2011
Reputation: 1
#1
Preventing scripts from repeating

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?
08-17-2011, 04:16 PM
Find
Kyle Offline
Posting Freak

Posts: 911
Threads: 36
Joined: Sep 2010
Reputation: 7
#2
RE: Preventing scripts from repeating

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.

08-17-2011, 04:30 PM
Find
rybray Offline
Junior Member

Posts: 31
Threads: 9
Joined: Aug 2011
Reputation: 1
#3
RE: Preventing scripts from repeating

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?
08-17-2011, 05:46 PM
Find
Kyle Offline
Posting Freak

Posts: 911
Threads: 36
Joined: Sep 2010
Reputation: 7
#4
RE: Preventing scripts from repeating

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:

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

(This post was last modified: 08-17-2011, 05:59 PM by Kyle.)
08-17-2011, 05:57 PM
Find
rybray Offline
Junior Member

Posts: 31
Threads: 9
Joined: Aug 2011
Reputation: 1
#5
RE: Preventing scripts from repeating

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.
(This post was last modified: 08-17-2011, 09:18 PM by rybray.)
08-17-2011, 09:12 PM
Find
Acies Offline
Posting Freak

Posts: 1,643
Threads: 60
Joined: Feb 2011
Reputation: 73
#6
RE: Preventing scripts from repeating

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

[Image: mZiYnxe.png]


(This post was last modified: 08-18-2011, 12:02 AM by Acies.)
08-18-2011, 12:01 AM
Find
MegaScience Offline
Member

Posts: 213
Threads: 1
Joined: Aug 2011
Reputation: 2
#7
RE: Preventing scripts from repeating

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.
08-18-2011, 12:31 AM
Find




Users browsing this thread: 1 Guest(s)