[Tutorial] Making And Explaining Dynamic Systems
#1

Making and explaining Dynamic Systems
Introduction
Today i'm gonna to release that is, i think pretty useful for those who want to understand how does work dynamic system of loading, saving and creating and how to make it and use it. On my ******** profile i get lot of messages how to make dynamic system for something. Lot of people thinks that is hard but in fact, it's just way easier than you thought.
This whole tutorial is made with comments and i think even beginners should understand it!
This is my first tutorial so its not so great, but I'll try to improve it as much as possible

Things that you need
- SA-MP 0.3.7 windows server - download it and extract it to your desktop
- sscanf plugin - Download it and extract the files in your server's directory.
- YSI Files - You will only need folder witch is located in pawno/include/ and its called YSI download it and extract it in that path
- zcmd - Download it and place it in /pawno/includes/


difference between a dynamic and static system
Static:
  • How you add it in script and file witch system is saved, u cannot edit it ingame, for each edit you will need to restart server and has only 1 file in witch is all saved
Positive side of static system:
  • Cant cause lag because only has 1 file
Negative side of static system:
  • Cannot be manged ingame
  • For each edit you need to restart server
Dynamic:
  • Can be added ingame
  • Can be removed ingame
  • Can be edite ingame
Positive side of dynamic system:
  • Can be manged ingame
  • Can be used for lot of things to improve you server and script
Negative side of dynamic system:
  • Can cause server lag while trying to save lot of files
  • If you dont use it properly can cause server crashes, lags...


Let's start scripting!
For example i will make dynamic system of creating vehicle ingame and saving it to files and loading it
!!! You need to create folder named Vehicles in folder Scriptfiles !!!

- This codes add at top of script:
PHP Code:
//First, of course we need to include these files first
#include <a_samp> //Without this, we won't be able to use all samp functions/callbacks
#include <zcm> //Without this, we won't be able to create commands
#include <sscanf2> //Without this, we won't be able to use creating commands
#include <YSI\y_ini>  //Without this, we won't be able to save and load files 
PHP Code:
new idoffile[MAX_PLAYERS]; //this will be used later in commands for saving Angle of vehicle and saving it
#define VEHICLE_FOLDER "Vehicles/Vehicle_%d.ini" //This will be path were will be saved files of vehicles
//Now let's create an enumerator that holds vehicle's information
enum vfInfo //We name our enumerator as vfInfo. You can name it however you want.
{
    
dID,      // This will be used to save ID of vehicle
    
dModel,   //This will be used to save model of vehicle
    
Float:dX//This will be used to save X cordinate of vehicle
    
Float:dY//This will be used to save Y cordinate of vehicle
    
Float:dZ//This will be used to save Z cordinate of vehicle
    
Float:dA//This will be used to save Angle of vehicle
    
dColor1,  //This will be used to save Vehicle color1
    
dColor2,  //This will be used to save Vehicle color2
};
new 
DVehicles[MAX_VEHICLES][vfInfo]; //Variable that stores enumerator above 
- Now we will make callbacks for saving and loading files:
  • Add this at bottom of script
PHP Code:
//now lets add callback for saving and loading
forward SaveVehicle(fileid); //This will forward for callback SaveVehicle
public SaveVehicle(fileid)
{
    new 
dFile[128]; //This is string witch will search for file
    
format(dFilesizeof(dFile),VEHICLE_FOLDER,fileid); //Formating string
    
new INI:File INI_Open(dFile); //Opening file
    
INI_WriteInt(File,"Model",DVehicles[fileid][dModel]); //Saving Model to file from variable dModel
    
INI_WriteFloat(File,"X",DVehicles[fileid][dX]);  //Saving Cordinate X to file from variable dX
    
INI_WriteFloat(File,"Y",DVehicles[fileid][dY]);  //Saving Cordinate Y to file from variable dY
    
INI_WriteFloat(File,"Z",DVehicles[fileid][dZ]);  //Saving Cordinate Z to file from variable dZ
    
INI_WriteFloat(File,"A",DVehicles[fileid][dA]);  //Saving Angle to file from variable dA
    
INI_WriteInt(File,"Color1",DVehicles[fileid][dColor1]); //Saving Color1 to file from variable dColor1
    
INI_WriteInt(File,"Color2",DVehicles[fileid][dColor2]); //Saving Color2 to file from variable dColor2
    
INI_Close(File);
    return 
1;
}
forward LoadVehicle(fileidname[], value[]); //This will forward for callback LoadVehicle
public LoadVehicle(fileidname[], value[])
{
    
INI_Int("Model",DVehicles[fileid][dModel]); //This will reard Model from file and store it to dModel
    
INI_Float("X",DVehicles[fileid][dX]); //This will reard Cordinate X from file and store it to dX
    
INI_Float("Y",DVehicles[fileid][dY]); //This will reard Cordinate Y from file and store it to dY
    
INI_Float("Z",DVehicles[fileid][dZ]); //This will reard Cordinate Z from file and store it to dZ
    
INI_Float("A",DVehicles[fileid][dA]); //This will reard Angle from file and store it to dA
    
INI_Int("Color1",DVehicles[fileid][dColor1]); //This will reard Color1 from file and store it to dColor1
    
INI_Int("Color2",DVehicles[fileid][dColor2]); //This will reard Color1 from file and store it to dColor1
    
return 1;

Commands
We will now create two commands witch will be:
  • Adding vehicles to server
  • Removing vehicles from server
- Add this commands at bottom of script
PHP Code:
CMD:createveh(playeridparams[]) //command for create vehicle
{
    new 
fileid 0//variable for file counting, witch file is not created
    
new Float:X,Float:Y,Float:Z//Varables for position
    
GetPlayerPos(playeridX,Y,Z); //Getting player position and storing i to X,Y,Z
    
for(new 0sizeof(DVehicles); b++) //this is loop
    
{
        if(
DVehicles[b][dModel] != 0//checking witch files are free (not created)
        
{
            
fileid b+1//Saving file ID to fileid
        
}
    }
    if(
fileid MAX_VEHICLES) return SendClientMessage(playerid, -1"MAX_VEHICLES reached, u cannot create more vehicles"); //if MAX_VEHICLE reach's you will not be in able to create more vehicles
    
new model,color1color2//Againg more variables for parameters
    
if(sscanf(params"iii"modelcolor1color2)) //In this part we will use sscanf, this is checking are all parameters filled to script can create vehicle
    
{
        
SendClientMessage(playerid, -1"/createveh [Model] [Color1] [Color2]");
        return 
1;
    }
    
DVehicles[fileid][dModel] = model//this will save parameters witch we filled in to variables
    
DVehicles[fileid][dX] = X//this will save parameters witch we filled in to variables
    
DVehicles[fileid][dY] = Y//this will save parameters witch we filled in to variables
    
DVehicles[fileid][dZ] = Z//this will save parameters witch we filled in to variables
    
DVehicles[fileid][dColor1] = color1//this will save parameters witch we filled in to variables
    
DVehicles[fileid][dColor2] = color2//this will save parameters witch we filled in to variables
    
DVehicles[fileid][dID] = CreateVehicle(modelX,Y,Z,0color1color20); //creating vehicle
    
PutPlayerInVehicle(playeridDVehicles[fileid][dID], 0); //Putting player in vehicle witch player created
    
idoffile[playerid] = fileid;
    
SendClientMessage(playerid, -1"You have created vehicle, now select angle in witch direction you want and press 'Y' to finish and save file.");
    return 
1;

PHP Code:
CMD:destroyveh(playeridparams[]) //command for destroy vehilce
{
    new 
fileid//filedd for sscanf
    
if(sscanf(params"i"fileid)) //In this part we will use sscanf, this is checking are all parameters filled to script can create vehicle
    
{
        
SendClientMessage(playerid, -1"/destroyveh [FILE ID]");
        return 
1;
    }
    new 
dFile[128]; //This is string witch will search for file
    
format(dFilesizeof(dFile),VEHICLE_FOLDER,fileid); //Formating string
    
if(!fexist(dFile)) return SendClientMessage(playerid, -1"That file does not exist!");//checking does file exist witch is in format
    
fremove(dFile); //Removing file
    
DestroyVehicle(DVehicles[fileid][dID]); //Destroying vehicle
    
DVehicles[fileid][dModel] = 0//Setting variables to default
    
DVehicles[fileid][dX] = 0.0//Setting variables to default
    
DVehicles[fileid][dY] = 0.0//Setting variables to default
    
DVehicles[fileid][dZ] = 0.0//Setting variables to default
    
DVehicles[fileid][dA] = 0.0//Setting variables to default
    
DVehicles[fileid][dColor1] = 0//Setting variables to default
    
DVehicles[fileid][dColor2] = 0//Setting variables to default
    
SendClientMessage(playerid, -1"You have been destroyed vehicle");
    return 
1;

Now, to we can finish creating vehicle, like u saw in command createveh, we will need to use OnPlayerKeyStateChange
PHP Code:
public OnPlayerKeyStateChange(playeridnewkeysoldkeys)
{
if(
newkeys KEY_YES//if player pres KEY_YES that is 'Y'
{
        if(
idoffile[playerid] > -1//if id of file is bigger then -1
        
{
        new 
fileid idoffile[playerid]; //geting id of file
        
new vehz GetPlayerVehicleID(playerid); //geting player vehicle id
        
new Float:X,Float:Y,Float:Z,Float:A//variables for pos of vehicle
        
GetVehicleZAngle(vehzA); //geting vehicle Angle
        
GetPlayerPos(playeridX,Y,Z); //Getting player position and storing i to X,Y,Z
        
DVehicles[fileid][dX] = X//setting new cordinate X for vehicle
        
DVehicles[fileid][dY] = Y//setting new cordinate Y for vehicle
        
DVehicles[fileid][dZ] = Z//setting new cordinate Z for vehicle
        
DVehicles[fileid][dA] = A//setting Angle for vehicle
        
DestroyVehicle(vehz); //Destronying vehicle
        
CreateVehicle(DVehicles[fileid][dModel], DVehicles[fileid][dX],DVehicles[fileid][dY],DVehicles[fileid][dZ],DVehicles[fileid][dA], DVehicles[fileid][dColor1], DVehicles[fileid][dColor2], 0); //creating new vehicle with saved variables
        
SaveVehicle(fileid); //Now saving vehicle to file
        
idoffile[playerid] = -1//seting player variable to -1
        
RemovePlayerFromVehicle(playerid); //Removing player from vehicle
        
SendClientMessage(playerid,-1,"U have cretated vehicle, congratz!");
        return 
1;
        }
}
return 
1;

Loading files and finishing
Now we will add loading loop in Public OnGameModeInit
PHP Code:
public OnGameModeInit()
{
    
// Don't use these lines if it's a filterscript
    
SetGameModeText("Blank Script");
    
AddPlayerClass(01958.37831343.157215.3746269.1425000000);
    for(new 
0sizeof(DVehicles); b++) //loop
    
{
        new 
gFile[35]; //string
        
format(gFile50VEHICLE_FOLDER ,b); //formating string
        
if(fexist(gFile)) //checking does file exist witch is in format
        
{
               
INI_ParseFile(gFile"LoadVehicle", .bExtra true, .extra b); //Y_INI function to load from files
             
if(DVehicles[b][dModel] != 0//if model is everytning expect 0
             
{
             
DVehicles[b][dID] = CreateVehicle(DVehicles[b][dModel], DVehicles[b][dX],DVehicles[b][dY],DVehicles[b][dZ],DVehicles[b][dA], DVehicles[b][dColor1], DVehicles[b][dColor2], 0); //creating vehicle
             
}
        }
    }
    return 
1;

We will need to set player variable 'idoffile' to -1 when player connects:
PHP Code:
public OnPlayerConnect(playerid)
{
    
idoffile[playerid] = -1;
    return 
1;

N O T E
This tutorial is just made to try teaching you how dynamic systems works and how should you create it. Sure there is defects in commands and other things because this is just example.
I'm sorry for bad english i hope you will understand it.



Download and other stuff
Download links: Special thanks to:
  • - ****** for his sscanf and YSI files
  • - ZEEX for his zcmd
!!! THANKS FOR WATCHING MY TUTORIAL !!!
Reply
#2

This should ve renamed to [Tutorial] Vehicle system
Reply
#3

Quote:
Originally Posted by AlonzoTorres
View Post
This should ve renamed to [Tutorial] Vehicle system
Why? with this system u can make house system, bizz...
In this tutorial i just explained how does work dynamic systems and how u should use it, with this u can make whatever you want another dynamic system...
Reply
#4

Quote:
Originally Posted by Dusan01
View Post
Why? with this system u can make house system, bizz...
In this tutorial i just explained how does work dynamic systems and how u should use it, with this u can make whatever you want another dynamic system...
Well you should at least explain the difference between a dynamic and static system. The positive and negative sides of static/dynamic systems. And explain how you can apply the same kind of system for multiple purposes. Otherwise it's fine.
Reply
#5

Quote:
Originally Posted by AlonzoTorres
View Post
Well you should at least explain the difference between a dynamic and static system. The positive and negative sides of static/dynamic systems. And explain how you can apply the same kind of system for multiple purposes. Otherwise it's fine.
Ahh thanks, i totally forgot about that, i will edit tut, thanks
Reply
#6

Quote:
Originally Posted by Dusan01
View Post
Ahh thanks, i totally forgot about that, i will edit tut, thanks
Better.
Reply
#7

One typo...
pawn Code:
#include <zcmd>
use [pawn] tag pls... other then that i like ur tut... +rep.
Reply
#8

INI sucks for saving/loading data use sqlite/mysql.
Reply
#9

Quote:
Originally Posted by Pottus
View Post
INI sucks for saving/loading data use sqlite/mysql.
cmon dude, lot of people dont know how to use sqlite/mysql so if i would do tutorial and they dont use it in they GMode they will need to translate it to INI for what u need to edit loading/saving(2min) and they dont know it...
Reply
#10

Quote:
Originally Posted by [ND]xXZeusXx.
View Post
One typo...
pawn Code:
#include <zcmd>
use [pawn] tag pls... other then that i like ur tut... +rep.
Thanks, i will
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)