SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help > Tutorials

Reply
 
Thread Tools Display Modes
Old 25/07/2012, 07:26 PM   #1
TD
Little Clucker
 
Join Date: Jul 2012
Posts: 6
Reputation: 15
Default How to make Semi Automated Gates! (Step by Step!)

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:
Code:
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.

Code:
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:
Code:
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:
Code:
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.
Code:
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.

Code:
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:
Code:
if(strcmp(cmdtext,"/opengate",true)==0 || strcmp(cmdtext,"/og",true)==0){
      return 1;
}
strcmp: This function compares two strings.
Code:
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.

Code:
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.

Code:
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.

Code:
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.

Code:
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.

Code:
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.


Code:
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:

Code:
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:
Code:
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.

Code:
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
TD is offline   Reply With Quote
Old 25/07/2012, 08:19 PM   #2
Glint
Guest
 
Posts: n/a
Default Re: How to make Semi Automated Gates! (Step by Step!)

Nice.
  Reply With Quote
Old 27/07/2012, 10:41 AM   #3
TD
Little Clucker
 
Join Date: Jul 2012
Posts: 6
Reputation: 15
Default Re: How to make Semi Automated Gates! (Step by Step!)

Quote:
Originally Posted by Lexi' View Post
Nice.
Thanks!
TD is offline   Reply With Quote
Old 03/12/2012, 04:03 PM   #4
Devilxz97
Gangsta
 
Devilxz97's Avatar
 
Join Date: Dec 2011
Location: Malaysia
Posts: 748
Reputation: 37
Default Re: How to make Semi Automated Gates! (Step by Step!)

help me alot lolz
__________________


- Skype Name: ejam.blacklistcrew -
- ******** Acc: http://www.********.com/DevConnection -

Selling Gamemode [DRIFT/FREEROAM/ROLEPLAY] for $9.99
Devilxz97 is offline   Reply With Quote
Old 03/12/2012, 04:26 PM   #5
XtremeR
Gangsta
 
XtremeR's Avatar
 
Join Date: Aug 2012
Location: Australia
Posts: 867
Reputation: 565
Default Re: How to make Semi Automated Gates! (Step by Step!)

9.5k + views and only 3 replies? xD
OT: good one! +5 reps..
__________________
XtremeR is offline   Reply With Quote
Old 03/12/2012, 04:30 PM   #6
gtakillerIV
Gangsta
 
gtakillerIV's Avatar
 
Join Date: Sep 2011
Location: UAE, Sharjah.
Posts: 985
Reputation: 201
Default Re: How to make Semi Automated Gates! (Step by Step!)

Good job.
gtakillerIV is offline   Reply With Quote
Old 14/01/2013, 07:16 PM   #7
Private200
High-roller
 
Private200's Avatar
 
Join Date: May 2012
Posts: 1,176
Reputation: 211
Default Re: How to make Semi Automated Gates! (Step by Step!)

Good job for what you exaplined ( From the title ) but an really automatic gate that get opened with player range detection would be better .
Private200 is offline   Reply With Quote
Old 14/01/2013, 08:45 PM   #8
seanny
High-roller
 
seanny's Avatar
 
Join Date: Oct 2010
Posts: 1,429
Reputation: 41
Default Re: How to make Semi Automated Gates! (Step by Step!)

Very well explained. Good job. Rep+'ed
__________________
I'm no longer part of the SA-MP scene. I will not respond to any support requests.
Developer @ Outlaw Games Studio
https://discord.gg/BR2FCTm
seanny is offline   Reply With Quote
Old 21/02/2013, 05:04 PM   #9
LavaHDProkiller
Little Clucker
 
Join Date: Jan 2013
Posts: 42
Reputation: 16
Default Re: How to make Semi Automated Gates! (Step by Step!)

coooool
__________________
Join cops and robbers server LSRCR join.

ip: 217.23.8.199:7777
LavaHDProkiller is offline   Reply With Quote
Old 22/02/2013, 07:05 AM   #10
CreativityLacker
Big Clucker
 
CreativityLacker's Avatar
 
Join Date: Oct 2012
Location: The chocolate factory
Posts: 134
Reputation: 25
Default Re: How to make Semi Automated Gates! (Step by Step!)

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.
__________________
Scripter at Resurrection
91.121.153.202:7778 or resur.cod5samp.com:7778
CreativityLacker is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Tutorial] semi-automatic gates [TUT for newbies] LeOsk Tutorials 19 01/01/2016 11:32 PM


All times are GMT. The time now is 02:03 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.