[Tutorial] Automatic gates - easy & explained.
#1

Automatic gates
Simple as the name.
First, let's store the object.
pawn Code:
new AutoGate;
Now let's create the object and something to store it., put this anywhere in OnGameModeInIt
pawn Code:
AutoGate = CreateObject(modelid, Float:X, Float:Y, Float:Z, Float:rX, Float:rY, Float:rZ, Float:DrawDistance)
* Visit https://sampwiki.blast.hk/wiki/CreateObject for more information about this function.

Now let's set a timer , anywhere in OnPlayerConnect.
pawn Code:
SetTimerEx( "AGCheck", 750, true, "i", playerid );
If you followed the tutorial and did everything correctly, this is what you should have :
pawn Code:
public OnPlayerConnect(playerid)
{
    SetTimerEx( "AGCheck", 750, true, "i", playerid );
    return 1;
}
* Remember that you need to edit modelid's, XYZ, etc.

Now, let's register the callback of the timer.
pawn Code:
forward AGCheck(playerid);
Now, we got to the part of the object itself.
pawn Code:
public  AGCheck( playerid );
{
    if ( IsPlayerInRangeOfPoint( playerid, Float: range, Float: x, Float: y, Float: z ) )
    {
        MoveObject( AutoGate, Float: x, Float: y, Float: z, Float: speed );
    }
    else
    {
        MoveObject( AutoGate, Float: originalX, Float: originalY, Float: originalZ, Float: speed );
    }
 
    return 1;
}
Now, you need to go ingame, infront of your gate, count a few steps from it and /save playerrange
Go to My Documents\GTA User files\SAMP and open ' savedpositions '.
You'll see addplayerclass(..) etc.
All you need is the XYZ, nothing less.
Example :
Quote:

AddPlayerClass(2,2029.8596,1351.5167,10.8203,90.4238,0,0,0,0,0,0); // playerrange

All you need is marked in red.
When we got those positions, now we can set the range using the IsPlayerInRangeOfPoint function.
pawn Code:
if(IsPlayerInRangeOfPoint(i, Float:range, Float:x, Float:y, Float:z)
* Note : I used 'i' so range will be for all players and not for a specified playerid.
Now, if there's a player near it, so we need to make the gate automatic, using MoveObject.
pawn Code:
MoveObject(autogate,Float:X, Float:Y, Float:Z, Float:Speed)
* Note : No need for objectid since it's on autogate already.
Example :
pawn Code:
MoveObject(autogate,2088.9011,1433.1863,10.8203,1.0)
Now, we want that the gate will close when a player gets near it, so we'll use the else command.
In the end, we'll get something like this
pawn Code:
forward AGCheck( playerid );
public  AGCheck( playerid )
{
    if ( IsPlayerInRangeOfPoint(playerid, 8.0, Float: x, Float: y, Float: z ) ) // when a player is near at some point;
    {
        MoveObject( AutoGate, Float: x, Float: y, Float: z, Float: speed ); // moves the object;
    }
    else
    {
        MoveObject( AutoGate, Float: originalX, Float: originalY, Float: originalZ, Float: speed ); // moves the object;
    }

    return 1;
}




* If you didn't understand any function / callback etc, check www.wiki.sa-mp.com

This is my first tutorial, so yeah, be nice.
Reply
#2

SetTimer("autogate",1000,l);

but autogate this callback with params..
Reply
#3

I didn't understood, explain.
Reply
#4

you need write this:

SetTimerEx("autogate",1000,true,"i",playerid);
Reply
#5

Seems like I sorta screwd up the script; can someone tell me what I did wrong so I can fix ?
Reply
#6

Why in the name of god are you using PlayerToPoint?! SA-MP has a native implementation for that: IsPlayerInRangeOfPoint
Reply
#7

I edited the whole tutorial to a better one, more explained.
Reply
#8

Nice tutorial, but I'll do something like this
pawn Code:
public OnPlayerConnect( playerid ) // When a player connects;
{ // opening curly bracket(s);
    SetTimerEx( "autoGateCheck", 750, true, "i", playerid ); // set's a timer, i = integer, the same as 'd' but it's decimal;

    return 1;
} // closing curly bracket(s).

forward autoGateCheck( playerid ); // Register the callback;
public  autoGateCheck( playerid ) // the timer;
{ // opening curly bracket(s);
    if ( IsPlayerInRangeOfPoint( playerid, Float: range, Float: x, Float: y, Float: z ) ) // when a player is near at some point;
    { // opening curly bracket(s);
        MoveObject( objectID, Float: x, Float: y, Float: z, Float: speed ); // moves the object;
    } // closing curly bracket(s);
    else // when a player is not in the gate's range
    { // opening curly bracket(s);
        MoveObject( objectID, Float: originalX, Float: originalY, Float: originalZ, Float: speed ); // moves the object;
    } // closing curly bracket(s);
 
    return 1;
} // closing curly bracket(s);
I think you need to return 1; on your code.

Overall, good tutorial.
Reply
#9

The gate doesnt want to move, however, I followed the directions exactly. Nor does it return any errors.
Reply
#10

Quote:
Originally Posted by Bigbucks
View Post
The gate doesnt want to move, however, I followed the directions exactly. Nor does it return any errors.
That's weird, did you lower down the Z point ?
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)