22.06.2015, 08:06
(
Last edited by Dusan01; 22/06/2015 at 10:06 AM.
)
Making and explaining Dynamic Systems
IntroductionToday 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
- Cant cause lag because only has 1 file
- Cannot be manged ingame
- For each edit you need to restart server
- Can be added ingame
- Can be removed ingame
- Can be edite ingame
- Can be manged ingame
- Can be used for lot of things to improve you server and script
- 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
- 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(dFile, sizeof(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(fileid, name[], value[]); //This will forward for callback LoadVehicle
public LoadVehicle(fileid, name[], 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;
}
We will now create two commands witch will be:
- Adding vehicles to server
- Removing vehicles from server
PHP Code:
CMD:createveh(playerid, params[]) //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(playerid, X,Y,Z); //Getting player position and storing i to X,Y,Z
for(new b = 0; b < sizeof(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,color1, color2; //Againg more variables for parameters
if(sscanf(params, "iii", model, color1, color2)) //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(model, X,Y,Z,0, color1, color2, 0); //creating vehicle
PutPlayerInVehicle(playerid, DVehicles[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(playerid, params[]) //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(dFile, sizeof(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;
}
PHP Code:
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
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(vehz, A); //geting vehicle Angle
GetPlayerPos(playerid, X,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;
}
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(0, 1958.3783, 1343.1572, 15.3746, 269.1425, 0, 0, 0, 0, 0, 0);
for(new b = 0; b < sizeof(DVehicles); b++) //loop
{
new gFile[35]; //string
format(gFile, 50, VEHICLE_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;
}
PHP Code:
public OnPlayerConnect(playerid)
{
idoffile[playerid] = -1;
return 1;
}
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 !!!