Using OnGateOpenClose and OnPlayerTriggerGate
These two functions are useful for many things. In this example we will create the gates at LS airport, and add 'clang' sounds when they open/close using OnGateOpenClose. We will also play the 'This area is restricted to pilots only.' voice from single-player on OnPlayerRequestGate if entrance is denied, as we only want pilots (skin 61) to enter.
First we need to create the gate, and store its ID in a global variable to handle it later. Two gates are required at the LS airport, but we will only handle one for the sounds, but still need to provide an ID for it for the permissions:
pawn Code:
new GATE_LSAP[2];
public OnGameModeInit()
{
// LS airport gates
GATE_LSAP[0] = CreateAutomaticGate(988, 1958.742797, -2189.757324, 13.586877, 0.000000, 0.000000, 180.000000, 1954.142822, -2189.757324, 13.586876, -1000, -1000, -1000, 1961.5413,-2189.3660,13.5469, 15, 4, 1);
GATE_LSAP[1] = CreateAutomaticGate(988, 1964.242919, -2189.757324, 13.586877, 0.000000, 0.000000, 180.000000, 1968.842895, -2189.757324, 13.586876, -1000, -1000, -1000, 1961.5413,-2189.3660,13.5469, 15, 4, 1);
return 1;
}
Now, when OnGateOpenClose is called, we want to play the sound of the gate opening to players that are in range:
pawn Code:
public OnGateOpenClose(gateid, openclose, Float:gatex, Float:gatey, Float:gatez)
{
if(gateid == GATE_LSAP[0])
{
if(openclose == GATE_STATUS_OPENING || openclose == GATE_STATUS_CLOSING)
{
foreach(new i : Player)
{
if(IsPlayerInRangeOfPoint(i, 30, gatex, gatey, gatez)) PlayerPlaySound(i, 1100, gatex, gatey, gatez);
}
}
else
{
foreach(new i : Player)
{
if(IsPlayerInRangeOfPoint(i, 30, gatex, gatey, gatez)) PlayerPlaySound(i, 1101, gatex, gatey, gatez);
}
}
}
return 1;
}
When a player triggers the gate, we need to play the 'They'll give a pilot's license to anybody these days!' sound:
pawn Code:
public OnPlayerTriggerGate(playerid, gateid)
{
if(gateid == GATE_LSAP[0]) SetTimerEx("pilot_speech", 321, 0, "i", playerid);
return 1;
}
forward pilot_speech(playerid);
public pilot_speech(playerid)
{
PlayerPlaySound(playerid, 16802+random(2), 0, 0, 0);
return 1;
}
The reason we have to delay it is so that it does not over-ride the 'clang' sound of the game opening. ~300 miliseconds is good.
Finally we must set the conditions for players to pass, which in this case, they must be using skin ID 61 which is a pilot:
pawn Code:
new pGateTick[MAX_PLAYERS];
public OnPlayerRequestGate(playerid, gateid)
{
if((gateid == GATE_LSAP[0] || gateid == GATE_LSAP[1]) && GetPlayerSkin(playerid) != 61)
{
if(gettime()-pGateTick[playerid] > 10) // If 10 seconds have passed since the last trigger (to prevent spam)
{
PlayerPlaySound(playerid, 16800+random(2), 0, 0, 0);
pGateTick[playerid] = gettime();
}
return 0;
}
return 1;
}
If they are not using skin 61, entry is denied (return 0
and the 'This area is restricted to pilots only' sound is played. The pGateTick and gettime() stuff is an 'antispam' as this function would be called once a second for a player standing in the trigger zone, so we don't want to 'spam' the sound.
Here's that code in action:
http://www.youtube.com/watch?v=PoJ4qhdoA2U