Let's say you want a guard to say hello everytime someone enters the room. But you have 3 guards with the same vnum, and a group of 4 players just walked in... that's 12 "hello"s. which is probably 11 more than you want. Ever wonder how to make it only happen once? Check out the following code:
Code:
Trigger type: catch_entry_act
protect
{
static @alreadyRunning
if( %contextIsValid( @alreadyRunning ) )
{
//
// This script is already running.
//
return
}
endif
#@alreadyRunning = %contextGetId()
wait 0 secs
say Welcome... TO YOUR DOOM!
hit @n
}
How it works... A static var is shared by all instances of the script and is never deleted. So everytime the script is run the variable @alreadyRunning contains the last assigned value. In this case we assign the value of %contextGetId(). This is a unique ID assigned to a script when it is started. Using %contextIsValid() we can determine if a context ID is valid (the script is currently running). If it is valid then we know another script is already handling the "hello" and so we just exit the script since there's nothing to do. The "wait 0" line is also very important, because our script is encased in "protect/endprotect" then the script is guaranteed to run till it hits the endprotect statement without any other script running, however, if that happens then the script will finish and the next script will find that there is no valid running script, so wait 0 says, pass control back to the script engine for now, and that will allow all the other scripts trying to run to see that a script is already handling "hello".