[Tutorial] Creating Gangzones manually and saving them
#1

Hello, i wanted to make my first tutorial about creating a gangzone manually and saving it in a txt file, Let's start:

Needed includes:
pawn Code:
#include <a_samp> // Offical one
#include <zcmd> // for easy command writing
#include <sscanf2>
Got them? good , let's switch to next step, We're going to make some player variables to be able to save the Min's and Max's with them , But before let's explain what are gang zones, how are they coordinates, how they can be used,

A gang zone is a squared place, Used to be in the radar / map, It can be an oblong, That's depends on it's coordinates set, we have 4 coordinates here, Includes X Y for both, We will not use Z coordinate be cause the radar is set a 2D Coordinate system, So we're gonna get just X & Y, X(right - left) Y(forward - backward),
In the GTA SA world, X coordinate's value increases when we go right, Y coordinate's value increases when we go forward ,

Both used to be a float,
pawn Code:
// Example: 10.0 , 9.0005 //
Got it? Now let's explain the min and max:
Min = Minimum
Max = Maximum

with creating a gang zone, we're gonna use Min to set the Minimum coordinate of X and Y , With max we're gonna set the max coordinate of the X, Y

A paint shows you it better:


As paint says, both coordinates result their Min point and Max point to create the gangzone.

Let's now start scripting:


We made the player's variables for the points, we'll use your friend's pos to get the max point

pawn Code:
CMD:czone(playerid, params[])
{
       new id, zvar, Float:X, Float:Y, Float:Z, Float:X2, Float:Y2, Float:Z2;
       if(sscanf(params, "us", id, zvar)) return SendClientMessage(playerid, -1, "Error: use /czone [ID] [Zone's variable name]");
       if(!IsPlayerConnected(id) || id == INVALID_PLAYER_ID) return SendClientMessage(playerid, -1, "Error: invalid id or not connected.");
       GetPlayerPos(playerid, X, Y, Z);
       GetPlayerPos(id, X2, Y2, Z2);
       if(X >= X2 || Y >= Y2) return SendClientMessage(playerid, -1, "Error: the max point should be more than min point!");
       
       
       new File:zones = fopen("szones.txt", io_append);
       if(fexist(zones))
       {
             new str[128];
             format(str, sizeof(str), "new %s = GangZoneCreate(%f, %f, %f, %f);", zvar, X, Y, X2, Y2);
             fwrite(zones, str);
             fclose(zones);
       }
       else return SendClientMessage(playerid, -1, "Error: file is not found! Make a TXT File named 'szones' ");
       return 1;
Let's start explaining line by line!
id = id of the player to use him to get the Max point.
zvar = zone's variable name like( new lol = Gangzonecreate etc..)

*We used sscanf to check the parameters after the command text, so we check if he missed some parameter, we'll send him an error message

*We checked if id is not connected or invalid, we'll send the playerid an error message.

*Here we got playerid pos( playerid = Min point) and (id = Max point) , you're gonna ask why we used the Z, be cause we can't leave the Z in getplayerpos code or it will give us a compiling error. but we'll get the x and y only without the Z.

* We checked if the Min X and Y is more than Max X and Max Y, so that will return a bug, so we made that if the Min is more than Max will return an error message, be cause the Max should always be more than Min.


*here we opened a new file called "szones" savedzones, we used the variable 'zones' to define the file in the script.

* here we checked if the file exists in the scriptfiles folder, else it will not save it in any place and will cause a bug.

*Here we created a string with 128 cells and formated it with a code "Gangzonecreate" to save it in the file with info, %f used to define floats in script, %s used for strings.

* and here we used fwrite to write the code inside the file then we closed the file.

*finally we added an error message if the file is not exist.

P.S: X2 & Y2 used to be the max point and X and Y used to be the Min.

To be known:
- It will not create the zone while u are in the game, it will save it in the file and you are gonna copy the code and paste it into your script

- You can use Y_INI for fast saving and loading, i used file functions to increase your knowledge of normal saving.

Links for some useful:
https://sampwiki.blast.hk/wiki/File_Functions
https://sampforum.blast.hk/showthread.php?tid=120356
https://sampforum.blast.hk/showthread.php?tid=91354

Thank you for reading the tutorial, I hope it helped you
Reply
#2

Wow, one whole command and saving with slow, outdated file functions!

Revolutionary.
Reply
#3

Looks like you've just cut a piece of code out of your gamemode..

pawn Code:
#include <sscanf> // for doing some stuff
ok..
Reply
#4

Nice work
Reply
#5

Quote:
Originally Posted by Snipa
View Post
Wow, one whole command and saving with slow, outdated file functions!

Revolutionary.
Code:
- You can use Y_INI for fast saving and loading, i used file functions to increase your knowledge of normal saving.
?
read again pls..

@ Flake: no..
Although this is my first tutorial, i have got no idea about good tutorial making, I wanted to test myself with creating tutorial, i'm not a reputation hunter ..
Reply
#6

Quote:
Originally Posted by Snipa
View Post
Wow, one whole command and saving with slow, outdated file functions!

Revolutionary.
And what? It works...
For what he needs Y_Ini or other?
Just another include.....
Reply
#7

Not bad for a first tutorial, keep it up.
Reply
#8

Not bad.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)