[Tutorial] Create "Movable" Gates
#1

Introduction:

Hey everyone, today I'll show you and explain how to make "gates" in samp
In case you are wondering what I mean by that, it is a gate that opens or closes when the user interacts
I will show you guys several different ways for different results, you pick whatever you want
In this tutorial I will be using Zeex's include zcmd


Content Table:
  • Gate activated by command
  • Gate activated by key
  • Gate with automatic closing
  • Gate activated by proximity
¤ Gate activated by command:

Well first of all, we will define the position of the gate open - closed, and declare the following variables:
pawn Code:
new GateGrove; //Id to be assigned to the gate
new isOpen[MAX_OBJECTS]; //Variable to check if the object is open

#define XYZ_Open 2464.6001000,-1668.8000500,14.0000000
#define XYZ_Closed 2464.6999500,-1659.3000500,14.0000000
//Note: They are necessary for the tutorial, and I recommend using them since they'll make your life much easier
Once the variables are declared, we will continue to assign the id to the object, while creating it:
pawn Code:
GateGrove = CreateObject(975,2464.6999500,-1659.3000500,14.0000000,0.0000000,0.0000000,88.0000000);
//Make sure that it's either on "OnGameModeInit" or "OnFilterScriptInit" due that it needs to be created when the server starts running
Finally, we create the command "/gate" to open or close the door, depending of it's state:
pawn Code:
command(gate,playerid,params[])
{
    if(IsPlayerInRangeOfPoint(playerid, 15.0, XYZ_Closed)) //If it is in range of the player
    {
        if(isOpen[GateGrove] == 0) //If the variable is equal to 0 (gate is closed)..
        {
            MoveObject(GateGrove, XYZ_Open, 2.00); //Move the gate to the appropriate coords
            isOpen[GateGrove] = 1; //Set the variable to 1 (gate is now open)
        }
        else if(isOpen[GateGrove] == 1) //If the variable is equal to 1 (gate is open)..
        {
            MoveObject(GateGrove, XYZ_Closed, 2.00); //Move the gate to the appropriate coords
            isOpen[GateGrove] = 0; //Set the variable to 0 (gate is now closed)
        }
    }
    return 1;
}
¤ Gate activated by key:

Well first of all, we will define the position of the gate open - closed, and declare the following variables:
pawn Code:
new GateGrove; //Id to be assigned to the gate
new isOpen[MAX_OBJECTS]; //Variable to check if the object is open

#define XYZ_Open 2464.6001000,-1668.8000500,14.0000000
#define XYZ_Closed 2464.6999500,-1659.3000500,14.0000000
//Note: They are necessary for the tutorial, and I recommend using them since they'll make your life much easier
Once the variables are declared, we will continue to assign the id to the object, while creating it:
pawn Code:
GateGrove = CreateObject(975,2464.6999500,-1659.3000500,14.0000000,0.0000000,0.0000000,88.0000000);
//Make sure that it's either on "OnGameModeInit" or "OnFilterScriptInit" due that it needs to be created when the server starts running
Finally, we add the open-close code on callback "OnPlayerKeyStateChange":
pawn Code:
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    if ((newkeys & KEY_YES) && !(oldkeys & KEY_YES)) //If key "Y" is pressed..
    {
        if(IsPlayerInRangeOfPoint(playerid, 15.0, XYZ_Closed)) //Player is in range of gate..
        {
            if(isOpen[GateGrove] == 0) //If the variable is equal to 0 (gate is closed)..
            {
                MoveObject(GateGrove, XYZ_Open, 2.00); //Move the gate to the appropriate coords
                isOpen[GateGrove] = 1; //Set the variable to 1 (gate is now open)
            }
            else if(isOpen[GateGrove] == 1) //If the variable is equal to 1 (gate is open)..
            {
                MoveObject(GateGrove, XYZ_Closed, 2.00); //Move the gate to the appropriate coords
                isOpen[GateGrove] = 0; //Set the variable to 0 (gate is now closed)
            }
        }
    }
    return 1;
}
¤ Gate with automatic closing:

Well first of all, we will define the position of the gate open - closed, and declare the following variables:
pawn Code:
new GateGrove; //Id to be assigned to the gate
new isOpen[MAX_OBJECTS]; //Variable to check if the object is open
new isAutomatic[MAX_OBJECTS]; //Variable to check if the gate is automatic closing type
new isIndpendent[MAX_OBJECTS]; //Variable to check if the gate is triggered by proximity


#define XYZ_Open 2464.6001000,-1668.8000500,14.0000000
#define XYZ_Closed 2464.6999500,-1659.3000500,14.0000000
//Note: They are necessary for the tutorial, and I recommend using them since they'll make your life much easier
Once the variables are declared, we will continue to assign the id to the object, while creating it:
pawn Code:
GateGrove = CreateObject(975,2464.6999500,-1659.3000500,14.0000000,0.0000000,0.0000000,88.0000000);
isAutomatic[GateGrove] = 1; //Set it to automatic
//Make sure that it's either on "OnGameModeInit" or "OnFilterScriptInit" due that it needs to be created when the server starts running
Now we create the command that will only open the gate:
pawn Code:
command(autogate,playerid,params[])
{
    if(IsPlayerInRangeOfPoint(playerid, 15.0, XYZ_Closed)) //If player is in range of gate..
    {
        if(isOpen[GateGrove] == 0) //If the variable is equal to 0 (gate is closed)..
            {
                MoveObject(GateGrove, XYZ_Open, 2.00); //Move the gate to the appropriate coords
                isOpen[GateGrove] = 1; //Set the variable to 1 (gate is now open)
            }
    }
    return 1;
}
Finally, we add the code for callback "OnObjectMoved":
pawn Code:
public OnObjectMoved(objectid)
{
    if(objectid == GateGrove) //If the object that moved was our gate
    {
        if(isAutomatic[GateGrove] == 1 || isIndependent[GateGrove]) //If it is either automatic closing or independent
        {
            MoveObject(GateGrove, XYZ_Closed, 2.00); //Move the gate to the appropriate coords
            isOpen[GateGrove] = 0; //Set the variable to 0 (gate is now closed)
        }
    }
    return 1;
}
¤ Gate activated by proximity:

Well first of all, we will define the position of the gate open - closed, and declare the following variables:
pawn Code:
new GateGrove; //Id to be assigned to the gate
new isOpen[MAX_OBJECTS]; //Variable to check if the object is open
new isAutomatic[MAX_OBJECTS]; //Variable to check if the gate is automatic closing type
new isIndpendent[MAX_OBJECTS]; //Variable to check if the gate is triggered by proximity

#define XYZ_Open 2464.6001000,-1668.8000500,14.0000000
#define XYZ_Closed 2464.6999500,-1659.3000500,14.0000000
//Note: They are necessary for the tutorial, and I recommend using them since they'll make your life much easier
Once the variables are declared, we will continue to assign the id to the object, while creating it:
pawn Code:
GateGrove = CreateObject(975,2464.6999500,-1659.3000500,14.0000000,0.0000000,0.0000000,88.0000000);
isIndependent[GateGrove] = 1; //Set it to independent
for(new i; i < MAX_PLAYERS; i++) //Loop for every player
    {
        SetTimerEx("NearGate", 2000, true, "i", i); //Check every 2 seconds if player is near a gate
    }
//Make sure that it's either on "OnGameModeInit" or "OnFilterScriptInit" due that it needs to be created when the server starts running
Now we create the public and the forward for the checking function:
pawn Code:
forward NearGate(playerid);
public NearGate(playerid)
{
    if(IsPlayerInRangeOfPoint(playerid, 15.0, XYZ_Closed)) //If player is in range of gate..
    {
        if(isIndependent[GateGrove] == 1) // If it is independent..
        {
            if(isOpen[GateGrove] == 0) //If the variable is equal to 0 (gate is closed)..
            {
                MoveObject(GateGrove, XYZ_Open, 2.00); //Move the gate to the appropriate coords
                isOpen[GateGrove] = 1; //Set the variable to 1 (gate is now open)
            }
        }
    }
    return 1;
}
Finally, we add the code for callback "OnObjectMoved":
pawn Code:
public OnObjectMoved(objectid)
{
    if(objectid == GateGrove) //If the object that moved was our gate
    {
        if(isAutomatic[GateGrove] == 1 || isIndependent[GateGrove]) //If it is either automatic closing or independent
        {
            MoveObject(GateGrove, XYZ_Closed, 2.00); //Move the gate to the appropriate coords
            isOpen[GateGrove] = 0; //Set the variable to 0 (gate is now closed)
        }
    }
    return 1;
}
Reply
#2

Epilogue:

Hey everyone, I showed you guys how to create gates in the post above
In case you are wondering what is in this epilogue, is some variations you can do to the gates above
I'll show you different ways to do it for different results, you just pick the one you want
In this tutorial I will be using Zeex's include zcmd


Content Table:
  • Exclusive to a faction/team
  • Exclusive to any vehicle
  • Exclusive to a specific vehicle
  • Exclusive to a specific score
¤ Exclusive to a faction/team:

Well, what you want to do here is to add the following line:
pawn Code:
// Command depending on the type of gate
if(IsPlayerInRangeOfPoint(playerid, 15.0, XYZ_Closed)) //If player is in range of the gate..
    {
        if(PlayerInfo[playerid][pFaccion] == x) //If player belongs to faction "x" - Change for your team/faction variable -..
        {
            //Rest of code
        }
    }
return 1;
}
¤ Exclusive to any vehicle:

Well, what you want to do here is to add the following line:
pawn Code:
// Command depending on the type of gate
if(IsPlayerInRangeOfPoint(playerid, 15.0, XYZ_Closed)) //If player is in range of the gate..
    {
        if(IsPlayerInAnyVehicle(playerid)) //If player is in any vehicle..
        {
            //Rest of code
        }
    }
return 1;
}
¤ Exclusive to a specific vehicle:

Well, what you want to do here is to add the following line:
pawn Code:
// Command depending on the type of gate
if(IsPlayerInRangeOfPoint(playerid, 15.0, XYZ_Closed)) //If player is in range of the gate..
    {
        if(IsPlayerInVehicle(playerid, x)) //If the player is in vehicle "x" - Change x for the vehicle id -..
        {
            //Rest of code
        }
    }
return 1;
}
¤ Exclusive to a specific score:

Well, what you want to do here is to add the following line:
pawn Code:
// Command depending on the type of gate
if(IsPlayerInRangeOfPoint(playerid, 15.0, XYZ_Closed)) //Si esta en rango de la puerta..
    {
        if(GetPlayerScore(playerid) >= x) //If player has a bigger or equal value of "x" score - Change x for the score -..
        {
            //Rest of code
        }
    }
return 1;
}
Reply
#3

I have not read all of it, I skipped through it. However, by looks it's a great tutorial and I'm sure that people will find it useful. Keep up!
Reply
#4

Quote:
Originally Posted by AlonzoTorres
View Post
I have not read all of it, I skipped through it. However, by looks it's a great tutorial and I'm sure that people will find it useful. Keep up!
Thank you very much!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)