[Tutorial] Moving Gate (Key, Manual, and Automatic) (More that 1 method)
#1

Remember, some code need to be changed. This is tutorial, not copy-paste working code.
If you want to see example-working code, check the bottom of the post.
This isn't a beginner tutorial.
https://sampwiki.blast.hk/wiki/CreateObject
https://sampwiki.blast.hk/wiki/MoveObject

First, we create a global variable
pawn Code:
new Gate,//To store the gate-objectid
    bool:Open;//Boolean if you want to use one command instead 2 for close and open
/*
Note:
Using boolean is (a little) slower instead make two command and take (a little) file size*/
If you want to create more than one gate, create multi-dimensional variable (which is faster) instead multiple variable.
pawn Code:
//Multi-dimensional variable
new Gate[3],
    bool:Open[3 char];//For more information about char: https://sampforum.blast.hk/showthread.php?tid=216730
//--------------------------
//Multiple variable
new Gate1,Gate2,Gate3,
    bool:Open1,bool:Open2,bool:Open3;
//--------------------------


Now we can create the gate.
pawn Code:
//Single gate
public OnFilterScriptInit()
{
    Gate = CreateObject(modelid,Float:X,Float:Y,Float:Z,Float:rX,Float:rY,Float:rZ,Float:DrawDistance = 0.0);
    return 1;
}
//--------------------------
//Multiple gate
public OnFilterScriptInit()
{
    Gate[0] = CreateObject(modelid,Float:X,Float:Y,Float:Z,Float:rX,Float:rY,Float:rZ,Float:DrawDistance = 0.0);
    Gate[1] = CreateObject(modelid,Float:X,Float:Y,Float:Z,Float:rX,Float:rY,Float:rZ,Float:DrawDistance = 0.0);
    Gate[2] = CreateObject(modelid,Float:X,Float:Y,Float:Z,Float:rX,Float:rY,Float:rZ,Float:DrawDistance = 0.0);
    return 1;
}
//--------------------------
You can do any different method f.e:
pawn Code:
new Float:GatePos[3][6] =
{
    {xxx,yyy,zzz,rx,ry,rz,x1,y1,z1},
    {xxx,yyy,zzz,rx,ry,rz,x1,y1,z1},
    {xxx,yyy,zzz,rx,ry,rz,x1,y1,z1}
};

/*
xxx, yyy, and zzz are the original coordinate of the objectid
rx, ry, and rz are the rotation of the objectid
x1, y1, and z1 are the second coordinate after the object moved/opened
*/


public OnFilterScriptInit()
{
    for(new a;a<sizeof(GatePos);a++)
    {
        Gate[a] = CreateObject(modelid,GatePos[a][0],GatePos[a][1],GatePos[a][2],GatePos[a][3],GatePos[a][4],GatePos[a][5],Float:DrawDistance = 0.0);
    }
    return 1;
}
This method is (a little) slower but you can change the coordinate dinamically and use it for another purpose(Get range from player, etc.).
f.e:
pawn Code:
new OnGate[MAX_PLAYERS char];

stock IsNearGate(playerid)
{
    for(new a;a<sizeof(GatePos);a++)
    {
        if(IsPlayerInRangeOfPoint(playerid,10,GatePos[a][0],GatePos[a][1],GatePos[a][2])
        {
            OnGate{playerid} = a;
            return 1;
        }
    }
    return 0;
}

CMD:gateopen(playerid,params[])
{
    if(!IsNearGate(playerid)) return SendClientMessage(playerid,color,"There's no gate nearby.");
    MoveObject(Gate[OnGate{playerid}],GatePos[OnGate{playerid}][6],GatePos[OnGate{playerid}][7],GatePos[OnGate{playerid}][8],Float:Speed);
    return 1;
}

CMD:gateclose(playerid,params[])
{
    if(!IsNearGate(playerid)) return SendClientMessage(playerid,color,"There's no gate nearby.");
    MoveObject(Gate[OnGate{playerid}],GatePos[OnGate{playerid}][0],GatePos[OnGate{playerid}][1],GatePos[OnGate{playerid}][2],Float:Speed);
    return 1;
}


Now we have the gate. The last step is to make the code for the moving gate.
In this tutorial, I'll explain 3 different methods of how to move the gate(Key, command, and automatic).

Key:
pawn Code:
#define PRESSED(%0) \
    (((newkeys & (%0)) == (%0)) && ((oldkeys & (%0)) != (%0)))
//For more information about PRESSED: https://sampwiki.blast.hk/wiki/OnPlayerKeyStateChange

public OnPlayerKeyStateChange(playerid,newkeys,oldkeys)
{
    switch(PRESSED)//Always use switch
    {
        case KEY_HANDBRAKE://(Hand Brake/aim)
        {
            if(IsPlayerInRangeOfPoint(playerid,10,xxx,yyy,zzz)//Gate 1
            {
                switch(Open{0})//Check whether the gate opened or closed
                {
                    case true://If the gate opened
                    {
                        MoveObject(Gate[0],Float:X,Float:Y,Float:Z,Float:Speed);//Close the gate
                        Open{0} == false;//false for closed
                    }
                    case false://If the gate closed
                    {
                        MoveObject(Gate[0],Float:X,Float:Y,Float:Z,Float:Speed);//Open the gate
                        Open{0} == true;//true for opened
                    }
                }
            }
            else if(IsPlayerInRangeOfPoint(playerid,10,xxx,yyy,zzz)//Gate 2
            {
                switch(Open{1})//Check whether the gate opened or closed
                {
                    case true://If the gate opened
                    {
                        MoveObject(Gate[1],Float:X,Float:Y,Float:Z,Float:Speed);//Close the gate
                        Open{1} == false;//false for closed
                    }
                    case false://If the gate closed
                    {
                        MoveObject(Gate[2],Float:X,Float:Y,Float:Z,Float:Speed);//Open the gate
                        Open{1} == true;//true for opened
                    }
                }
            }
            else if(IsPlayerInRangeOfPoint(playerid,10,xxx,yyy,zzz)//Gate 3
            {
                switch(Open{2})//Check whether the gate opened or closed
                {
                    case true://If the gate opened
                    {
                        MoveObject(Gate[3],Float:X,Float:Y,Float:Z,Float:Speed);//Close the gate
                        Open{2} == false;//false for closed
                    }
                    case false://If the gate closed
                    {
                        MoveObject(Gate[3],Float:X,Float:Y,Float:Z,Float:Speed);//Open the gate
                        Open{2} == true;//true for opened
                    }
                }
            }
        }
    }
    return 1;
}
Command(ZCMD only so you people will learn how to use ZCMD):
For more information about ZCMD: https://sampforum.blast.hk/showthread.php?tid=91354
pawn Code:
CMD:gate(playerid,params[])
{
    if(IsPlayerInRangeOfPoint(playerid,10,xxx,yyy,zzz)//Gate 1
    {
        switch(Open{0})//Check whether the gate opened or closed
        {
            case true://If the gate opened
            {
                MoveObject(Gate[0],Float:X,Float:Y,Float:Z,Float:Speed);//Close the gate
                Open{0} == false;//false for closed
            }
            case false://If the gate closed
            {
                MoveObject(Gate[0],Float:X,Float:Y,Float:Z,Float:Speed);//Open the gate
                Open{0} == true;//true for opened
            }
        }
        return 1;
    }
    if(IsPlayerInRangeOfPoint(playerid,10,xxx,yyy,zzz)//Gate 2
    {
        switch(Open{1})//Check whether the gate opened or closed
        {
            case true://If the gate opened
            {
                MoveObject(Gate[1],Float:X,Float:Y,Float:Z,Float:Speed);//Close the gate
                Open{1} == false;//false for closed
            }
            case false://If the gate closed
            {
                MoveObject(Gate[1],Float:X,Float:Y,Float:Z,Float:Speed);//Open the gate
                Open{1} == true;//true for opened
            }
        }
        return 1;
    }
    if(IsPlayerInRangeOfPoint(playerid,10,xxx,yyy,zzz)//Gate 3
    {
        switch(Open{2})//Check whether the gate opened or closed
        {
            case true://If the gate opened
            {
                MoveObject(Gate[2],Float:X,Float:Y,Float:Z,Float:Speed);//Close the gate
                Open{2} == false;//false for closed
            }
            case false://If the gate closed
            {
                MoveObject(Gate[2],Float:X,Float:Y,Float:Z,Float:Speed);//Open the gate
                Open{2} == true;//true for opened
            }
        }
        return 1;
    }
    SendClientMessage(playerid,color,"You're not close enough to any gate.");//Send error message if player isn't near any gate
    return 1;
}

Automatic:
pawn Code:
//On top of your script
new GateTimer[MAX_GATES];//Change MAX_GATES to How much gate you have

forward GateMovement1();
forward GateMovement2();
forward GateMovement3();

public OnFilterScriptInit()
{
    //blahblahblah you create the gate and stuff.........
    GateTimer[0] = SetTimer("GateMovement1",5000,1)//Start the timer
    GateTimer[1] = SetTimer("GateMovement2",5000,1)
    GateTimer[2] = SetTimer("GateMovement3",5000,1)
    return 1;
}

public GateMovement1()
{
    switch(Open{0})//Check whether the gate opened or closed
    {
        case true://If the gate opened
        {
            foreach(Player,i)//For more information about foreach: https://sampforum.blast.hk/showthread.php?tid=92679
            {
                if(IsPlayerInRangeOfPoint(i,10,xxx,yyy,zzz)) return 1;//If someone near the gate, the gate will remain opened
            }
            MoveObject(Gate[0],Float:X,Float:Y,Float:Z,Float:Speed);//Close the gate if there's nobody near the gate
            Open{0} == false;//false for closed
        }
        case false://If the gate closed
        {
            foreach(Player,i)
            {
                if(IsPlayerInRangeOfPoint(i,10,xxx,yyy,zzz))//If someone near the gate, the gate will open
                {
                    MoveObject(Gate[0],Float:X,Float:Y,Float:Z,Float:Speed);//Open the gate
                    Open{0} == true;//true for opened
                    return 1;
                }
            }
        }
    }
    return 1;
}
//Create GateMovement2 and GateMovement3 too by your self just like the code above. Just change "0" to "1" and so on.
PS:
This method is more simple:
pawn Code:
if (IsPlayerInRangeOfPoint(playerid,posRange,whereX,whereY,whereZ))
{
    Open{0} = !Open{0}; // Sets Open variable to the opposite side as it's current side (false = true, true = false).
    switch(Open{0})// Switch depends what is the current state.
    {
        case true : // Gate should be opened
            MoveObject(Gate[0],openX,openY,openZ,moveSpeed ); // Gate[ 0 ] = the object

        default : // Else, gate should be closed
            MoveObject(Gate[0],closeX,closeY,closeZ,moveSpeed ); // Gate[ 0 ] = the object
    }
    return 1;
}

Example FS


If you find anything wrong, please post a reply.
Reply
#2

Good job the best tutorial about making moving gates in the 3 methods
Reply
#3

Nice tutorial. Very usefull.
Reply
#4

This is very usefull.Keep it up
Reply
#5

Good work!

I learnt something more from this!
Reply
#6

Good work .
Learned * ^^.
Reply
#7

Lil' error here =D
pawn Code:
stock IsNearGate(playerid)
{
    for(new a;a<sizeof(GatePos);a++)
    {
        if(IsPlayerInRangeOfPoint(playerid,10,GatePos[a][0],GatePos[a][1],GatePos[a][2])
        {
            OnGate[playerid] = a;
            return 1;
        }
    }
    return 0;
MISSING BRACKET :3

Nice tutorial.
Reply
#8

Glad to know people learn more from this tut.

@Basicz: Oh, my bad.
Reply
#9

Also I think you can do a simpler way on this
pawn Code:
if(IsPlayerInRangeOfPoint(playerid,10,xxx,yyy,zzz)//Gate 1
{
    switch(Open{0})//Check whether the gate opened or closed
    {
        case true://If the gate opened
        {
            MoveObject(Gate[0],Float:X,Float:Y,Float:Z,Float:Speed);//Close the gate
            Open{0} == false;//false for closed
        }
        case false://If the gate closed
        {
            MoveObject(Gate[0],Float:X,Float:Y,Float:Z,Float:Speed);//Open the gate
            Open{0} == true;//true for opened
        }
    }
}
To
pawn Code:
if ( IsPlayerInRangeOfPoint( playerid, posRange, whereX, whereY, whereZ  ) )
{
    Open{ 0 } = ! Open{ 0 }; // Sets Open variable to the opposite side as it's current side ( false = true, true = false ).

    // Why { } ? Because we are using CHAR-ARRAYS, which is limited to ' 255 '.
   
           
    switch ( Open{ 0 } ) // Switch depends what is the current state.
    {
        case true : // Gate should be opened
            MoveObject( Gate[ 0 ], openX, openY, openZ, moveSpeed ); // Gate[ 0 ] = the object

        default : // Else, gate should be closed
            MoveObject( Gate[ 0 ], closeX, closeY, closeZ, moveSpeed ); // Gate[ 0 ] = the object
    }
}
No idea why you are not using char-arrays to the ' Gate ' variable, when you used ' OnGate ' variable to an char-array ( 8bits ) to the size of gate positions.

Plus, char arrays need to use { } not [ ]
pawn Code:
new OnGate[MAX_PLAYERS char];
OnGate[playerid] = a; // Wrong
MoveObject(Gate[OnGate[playerid]], ... ) // Wrong
Reply
#10

Quote:
Originally Posted by Basicz
View Post
Also I think you can do a simpler way on this
pawn Code:
if(IsPlayerInRangeOfPoint(playerid,10,xxx,yyy,zzz)//Gate 1
{
    switch(Open{0})//Check whether the gate opened or closed
    {
        case true://If the gate opened
        {
            MoveObject(Gate[0],Float:X,Float:Y,Float:Z,Float:Speed);//Close the gate
            Open{0} == false;//false for closed
        }
        case false://If the gate closed
        {
            MoveObject(Gate[0],Float:X,Float:Y,Float:Z,Float:Speed);//Open the gate
            Open{0} == true;//true for opened
        }
    }
}
To
pawn Code:
if ( IsPlayerInRangeOfPoint( playerid, posRange, whereX, whereY, whereZ  ) )
{
    Open{ 0 } = ! Open{ 0 }; // Sets Open variable to the opposite side as it's current side ( false = true, true = false ).

    // Why { } ? Because we are using CHAR-ARRAYS, which is limited to ' 255 '.
   
           
    switch ( Open{ 0 } ) // Switch depends what is the current state.
    {
        case true : // Gate should be opened
            MoveObject( Gate[ 0 ], openX, openY, openZ, moveSpeed ); // Gate[ 0 ] = the object

        default : // Else, gate should be closed
            MoveObject( Gate[ 0 ], closeX, closeY, closeZ, moveSpeed ); // Gate[ 0 ] = the object
    }
}
I use the old style one just to make sure people know the use of the bool.
Well I guess I can make a foot-note for it.

Quote:
Originally Posted by Basicz
View Post
No idea why you are not using char-arrays to the ' Gate ' variable, when you used ' OnGate ' variable to an char-array ( 8bits ) to the size of gate positions.

Plus, char arrays need to use { } not [ ]
pawn Code:
new OnGate[MAX_PLAYERS char];
OnGate[playerid] = a; // Wrong
MoveObject(Gate[OnGate[playerid]], ... ) // Wrong
Because we don't know the object id of them. Sometimes people have a huge amount of object and it's possible they have more than 255 object. So I'm not take the risk to suggest them use char-array fot the object id.

Yeah, another miss-typo. Thanks for reminding me.
Reply
#11

pawn Код:
//automatic

//Change the coordinate by yourself

#define FILTERSCRIPT

#include <a_samp>
#include <foreach>//Download this include if you don't have one

#define MAX_GATES 1

new Gate[MAX_GATES],
    bool:Open[MAX_GATES char];

new GateTimer[MAX_GATES];

forward GateMovement1();

public OnFilterScriptInit()
{
    Gate[0] = CreateObject(980,2044.09997559,1313.50000000,12.39999962,0.00000000,0.00000000,2.00000000);
    GateTimer[0] = SetTimer("GateMovement1",5000,1);
    return 1;
}

public GateMovement1()
{
    switch(Open{0})
    {
        case true:
        {
            foreach(Player,i)
            {
                if(IsPlayerInRangeOfPoint(i,10,2044.09997559,1313.50000000,12.39999962)) return 1;
            }
            MoveObject(Gate[0],2044.09997559,1313.50000000,6.39999962,1);
            Open{0} == false;
        }
        case false:
        {
            foreach(Player,i)
            {
                if(IsPlayerInRangeOfPoint(i,10,2044.09997559,1313.50000000,12.39999962))
                {
                    MoveObject(Gate[0],10,2044.09997559,1313.50000000,6.39999962,1);
                    Open{0} == true;
                    return 1;
                }
            }
        }
    }
    return 1;
}
Hmm , its not closing when it get opened ? What the problem ?
Reply
#12

Quote:
Originally Posted by =WoR=Varth
Посмотреть сообщение
This isn't a beginner tutorial.
Yet advanced people are able to create gates themselves.

Also, you explained almost nothing. You just posted some code which will only confuse people.
Reply
#13

I have a little question. I want to make gate(s) to different houses with one command such that only the owner of the house or renter can open it? Is this very hard to script?
Reply
#14

nicex very useful
Reply
#15

Nice Tutorial , I like it
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)