[Tutorial] How to make Semi Automated Gates! (Step by Step!)
#1

Semi Automated Gates
Introduction: Today,I'll be showing you how to make your own simple Semi-Automated Gates System.
This tutorial is very simple and made for newbies with fully explaining to everything we'll be using.

Step 1: First of all, we have to get the closed gate and the opened gate's cords.

To get the cords of each gate we'll be using Map editor application, you can download the program by visiting the official topic of it, here.
Once you have downloaded it follow the instructions to install and use it and when you're done you'll be getting an object with ID 975 (The gate's ID we are going to use in this tutorial).

Once you have added it, would look like this (In this tutorial I'll be Scripting a gate at Idlewood):
Click at the top left of the page and the code will look something like:
Код:
CreateObject(975, 2053.16, -1762.77, 14.25,   0.00, 0.00, 0.00);
Note: I recommended using CreateObject instead of CreateDynamicObject because it have cased to me few problems while opening the gate.

CreateObject: A function in the SAMP's API, used to add an Object to the actual Map of the Game/Server.

Код:
CreateObject(modelid, Float:X, Float:Y, Float:Z, Float:rX, Float:rY, Float:rZ, Float:DrawDistance);

modelid	                The model you want to use.
Float:X	                The X coordinate to create the object at.
Float:Y	                The Y coordinate to create the object at.
Float:Z	                The Z coordinate to create the object at.
Float:rX	        The X rotation of the object.
Float:rY	        The Y rotation of the object.
Float:rZ	        The Z rotation of the object.
Float:DrawDistance	(optional) The distance that San Andreas renders objects. 0.0 will cause objects to render at their default distances. 300.0 is the usable maximum. Usable since 0.3b.
                        Returns	The ID of the object that was created.
That cords were for the gate as closed status, now you'll have to Map an opened gate and get it's cords simply by following the closed gate's steps.

Once you have Mapped it, would look like:
And the code would be something like:
Код:
CreateObject(975, 2047.22, -1762.77, 14.25,   0.00, 0.00, 0.00)
Step 2: In this step we'll be adding the gate to our script.

First, you'll need to find OnGameModeIni();, Once you have found it add the closed gate codes under it and then it would look like:
Код:
public OnGameModeInit(){
   CreateObject(975, 2053.16, -1762.77, 14.25,   0.00, 0.00, 0.00);
}
OnGameModeInit: The OnGameModeInit() Function will be called once the script loads.

Step 3: Now we'll define the variable of the gate that we'll be using.

Find #include <a_samp> in your script and under it add a new variable.
Код:
New gate;
#include <a_samp>: This basically loads the code from pawno/includes/a_samp.inc into your script, so everything it has you can use. One of the things it has is:

Quote:

#include <core>
#include <float>
#include <string>
#include <file>
#include <time>
#include <datagram>
#include <a_players>
#include <a_vehicles>
#include <a_objects>
#include <a_sampdb>

This includes all the other files in that directory so by adding that one line you have access to all the functions in SA:MP.

Now go to the line we have added before under OnGameModeInit() and make the variable "gate" equals it.

Код:
gate=CreateObject(975, 2053.16, -1762.77, 14.25,   0.00, 0.00, 0.00);
Step 4: Now we'll add our commands to open or close the gate.

First you'll need to find OnPlayerCommandText(playerid,cmdtext[])
Under it add this code:
Код:
if(strcmp(cmdtext,"/opengate",true)==0 || strcmp(cmdtext,"/og",true)==0){
      return 1;
}
strcmp: This function compares two strings.
Код:
strcmp(const string1[], const string2[], bool:ignorecase, length)
string1		      The first string to compare.
string2		      The second string to compare.
ignorecase (optional) When set to true, the case doesn't matter - HeLLo is the same as Hello. When false, they're not the same.
length (optional)     When this length is set, the first x chars will be compared - doing "Hello" and "Hell No" with a length of 4 will say it's the same string.

Returns	        -1 if string1 comes before string2
                1 if string1 comes after string2
                0 if the strings are the same (for the matched length).
OnPlayerCommandText(playerid,cmdtext[]): This function will be called everytime the player enters a line to the text box.

Once we have added the base, we'll use IsPlayerInRangeOfPoint function to see if the player is in range of the gate so he won't be able to open it from a long distances.

IsPlayerInRangeOfPoint: Checks if a player is in range of a point.

Код:
IsPlayerInRangeOfPoint(playerid, Float:range, Float:x, Float:y, Float:z)
playerid	        The ID of the player you want to check the point range of.
Float:range	        The furthest distance the player can be to be in range.
Float:x	                The X coordinate of the point to check the range to.
Float:y	                The Y coordinate of the point to check the range to.
Float:z	                The Z coordinate of the point to check the range to.

Returns	1 if the player is in range of the point, otherwise 0.
Here, I'll be using Point (2053.16, -1762.77, 14.25) you can either use the gate's cords or get it using /save.

Код:
if(strcmp(cmdtext,"/opengate",true)==0 || strcmp(cmdtext,"/og",true)==0){
   if (IsPlayerInRangeOfPoint(playerid,30, 2053.16, -1762.77, 14.25)){
   //The player is near the gate.
   }else{
   //The player is not near the gate.
   }
      return 1;
}
If the player is near the gate, we should move the object of the gate to open it using MoveObject function.

MoveObject: Moves an object to a new position with a set speed. Players/vehicles will 'surf' the object as it moves.

Код:
MoveObject(objectid, Float:X, Float:Y, Float:Z, Float:Speed, Float:RotX = -1000.0, Float:RotY = -1000.0, Float:RotZ = -1000.0)
objectid	                The ID of the object to move.
Float:X	                The X coordinate to move the object to.
Float:Y	                The Y coordinate to move the object to.
Float:Z	                The Z coordinate to move the object to.
Float:Speed	        The speed at which to move the object (units per second).
Float:RotX	        The FINAL X rotation (optional).
Float:RotY	        The FINAL Y rotation (optional).
Float:RotZ	        The FINAL Z rotation (optional).

Returns	                The time it will take for the object to move in milliseconds.
Here we'll be using the opened gate's cords.

Код:
if(strcmp(cmdtext,"/opengate",true)==0 || strcmp(cmdtext,"/og",true)==0){
   if (IsPlayerInRangeOfPoint(playerid,30, 2053.16, -1762.77, 14.25)){
   //The player is near the gate.
   MoveObject(gate,2053.16, -1762.77, 14.25,15); // Moves object "gate" to other point
   }else{
   //The player is not near the gate.
   }
      return 1;
}
And now we'll be coding the second part of that command that's if the player is not in range of that point.
We'll send the play error message.

Код:
if(strcmp(cmdtext,"/opengate",true)==0 || strcmp(cmdtext,"/og",true)==0){
   if (IsPlayerInRangeOfPoint(playerid,30, 2053.16, -1762.77, 14.25)){
   //The player is near the gate.
   MoveObject(gate,2053.16, -1762.77, 14.25,15); // Moves object "gate" to other point
   }else{
   //The player is not near the gate.
   SendClientMessage(playerid,RED,"You are not close enough to open the gate.");
   }
      return 1;
}
SendClientMessage: This function can be used to send a message to a certain player with any chosen color. The whole line in the chatbox will be in the set color unless colour embedding is used with 0.3c or later.


Код:
SendClientMessage (playerid, color, const message[])
playerid	        The playerid you would like to display the message for.
color	        	The color it should be in.
const message[]	        The text you would like to display. max len displayable: 144
Returns	                This function doesn't return a specific value
Our final part for this tutorial is the close command.
Same steps as the open command with few changes:

Код:
if(strcmp(cmdtext,"/closegate",true)==0 || strcmp(cmdtext,"/cg",true)==0){
}
Now we'll add the same Point Range Checker part, so the code would look like:
Код:
if(strcmp(cmdtext,"/closegate",true)==0 || strcmp(cmdtext,"/cg",true)==0){
if (IsPlayerInRangeOfPoint(playerid,30,2053.16, -1762.77, 14.25)){
 //The player is near the gate.
}else{
 //The player is not near the gate.
}
}
Now we'll add the MoveObject part, it should move the object "gate" to the closed cords back, and then if the player is not near the gate we'll send the same error message as before.

Код:
if(strcmp(cmdtext,"/closegate",true)==0 || strcmp(cmdtext,"/cg",true)==0){
if (IsPlayerInRangeOfPoint(playerid,30,2053.16, -1762.77, 14.25)){
 //The player is near the gate.
 MoveObject(gate,2053.16, -1762.77, 14.25,15);
}else{
 //The player is not near the gate.
 SendClientMessage(playerid,RED,"You are not close enough to open the gate.");
}
}
Credits:
  • TD (Me).
  • SA:MP API.
This is my first tutorial, if you need any help or spotted a bug feel free to post it below or PM me it.

Thanks for reading my tutorial,
Royal
Reply
#2

Nice.
Reply
#3

Quote:
Originally Posted by Lexi'
Посмотреть сообщение
Nice.
Thanks!
Reply
#4

help me alot lolz
Reply
#5

9.5k + views and only 3 replies? xD
OT: good one! +5 reps..
Reply
#6

Good job.
Reply
#7

Good job for what you exaplined ( From the title ) but an really automatic gate that get opened with player range detection would be better .
Reply
#8

Very well explained. Good job. Rep+'ed
Reply
#9

coooool
Reply
#10

Why bump old topics?

@The author -

Your tutorial is sort of incomplete. What if I keep spamming opengate? It'll just going up and up and up, there's no variable to check if gate is open or closed. Same for closegate.
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)