Automatic Gates

From SA-MP Wiki

Jump to: navigation, search

0.2X versions and lower

If you are using an outdated version of SA-MP, then I suggest you update to the newest version found HERE, but if you absolutely have to use older versions (lower than 0.3a BETA releases) then you will be needing a function to determine if a player is within a radius of a given three dimensional point. So you will add the following to your script: (near the top, but below "#include <a_samp>")

#if !defined IsPlayerInRangeOfPoint
stock IsPlayerInRangeOfPoint(playerid, Float:radius, Float:X, Float:Y, Float:Z)
{
    new Float:px,Float:py,Float:pz;
    GetPlayerPos(playerid,px,py,pz);
    return ( ( ((px-X)*(px-X))+((py-Y)*(py-Y))+((pz-Z)*(pz-Z)) ) >= radius*radius );
}
#endif

Getting Ready

You will need to add the following before creating the gate function.

//This can be added anywhere above the open/close gate function
new mygate;
//This has to be in either "public OnGameModeInit" or "public OnFilterScriptInit"
mygate = CreateObject(object_ID, closed_X, closed_Y, closed_Z, closed_rad_X, closed_rad_Y, closed_rad_Z);//Substitute where necessary

Making automatic gates

Firstly, we will set a timer from OnGameModeInit() which will call after your selected time (mine has checked after 500ms, always). Feel free to change the 500ms to some other amount, but keep in mind that 1000ms equals one second. Learn more about the SetTimer function.

SetTimer("CheckGate",1000,true);

Now, we'll move on to the timer, which we'll forward properly.

// this will go anywhere above "public CheckGate"
forward CheckGate();
public CheckGate()
{
    new mygate_status;
    for(new i;i<MAX_PLAYERS;i++)
    {
        if(!IsPlayerConnected(i)) continue;
        if(IsPlayerInRangeOfPoint(i,10.0,closed_X,closed_Y,closed_Z))mygate_status=1;
    }
    if(mygate_status)MoveObject(mygate, open_X, open_Y, open_Z,Moving Speed);
    else MoveObject(mygate, closed_X, closed_Y, closed_Z, Moving Speed);
}

There, your gates should be ready, if you've done all that the guide has said, properly.

Personal tools
Navigation
Toolbox