24.08.2011, 10:17
(
Last edited by =WoR=Varth; 09/09/2011 at 07:19 PM.
)
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
If you want to create more than one gate, create multi-dimensional variable (which is faster) instead multiple variable.
Now we can create the gate.
You can do any different method f.e:
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:
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:
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
Automatic:
PS:
This method is more simple:
Example FS
If you find anything wrong, please post a reply.
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*/
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;
}
//--------------------------
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;
}
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;
}
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.
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.