[Tutorial] Vehicle System (Dini)
#1

Yorans Vehicle System Tutorial!


Hi,

I have decided to start and write a tutorial... Why? Because im pretty bored right now :P Ooh well let me explain what we are going to do!

We are going to create a simple but fast (Dini) vehicle system! This system loads the Vehicle's from a .ini file (Please explain this! I no understand! "I will later on in the tutorial!") Well what are we waiting for? Let's GoGoGo!

First we want to include Dini and zcmd(Easier command scripting include). Put this ontop of your script!
PHP код:
#include <zcmd>
#include <dini> 
Well done! Thats one step through! But we are still not their lets keep on going!


Alright now we will define how many vehicles there are allowed to be on the server. (Don't forget to add this under the includes we just defined!)
PHP код:
#undef MAX_VEHICLES
#define MAX_VEHICLES 5 // 5 For now :) 
Another step done! Lets keep on going!

Alright, now we will make a Enum for the saving and storing of the Vehicles!(Explained in the code itself)
PHP код:
enum vInfo
{
     
vModel// Vehicle Model
    
Float:vPosx// Vehicle X Position
    
Float:vPosy// Vehicle Y Position
    
Float:vPosz// Vehicle Z Position
    
Float:vPosa// Vehicle A Position
    
vColor1// Vehicle Color ID 1
    
vColor2// Vehicle Color ID 2
    
vvw// The virtual world of the vehicle
    
vPlate[10], // And last but not least the plate :) Not necessary but why not?
}
// Alright! Not falling asleep? GOOD!
new VehicleInfo[][vInfo] = // How it saves so 540 = vModel - -2937.5 = Float:vPosx - Ect, ect...
{
    {
540,-2937.5,-2927.519.2819268.0262,1,1,0,"GSLS - 0"}, // This is ID 0 and is never used but keep it defined!
    
{540,-2947.5,-2927.519.2819268.0262,1,1,0,"GSLS - 1"}, // ID 1
    
{540,-2957.5,-2927.519.2819268.0262,1,1,0,"GSLS - 2"}, // ID 2
    
{540,-2967.5,-2927.519.2819268.0262,1,1,0,"GSLS - 3"}, // ID 3
    
{540,-2977.5,-2927.519.2819268.0262,1,1,0,"GSLS - 4"// ID 4
}; 
Few! Thats done! Let's get to where the magic really happens!

Under OnGameModeInit we will place this code: (Explained in the script itself)
PHP код:
public OnGameModeInit()
{
    
LoadVehicles(); // When you start/open the server it will Load all the vehicles we've set! (ID 1-4)
    
return 1;

Alright! Lets get to the loading!

OK, Let's make a stock LoadVehicles in this case
PHP код:
stock LoadVehicles()
{
    for(new 
1sizeof(VehicleInfo); i++) // This will loop through all the Vehicle's! Remember when we made the enum? Let me refresh your brain abit and reshow you the code we made : new VehicleInfo[][vInfo] = (This will get/load the vehicles)
    
{
        if( !
dini_ExistsCarFile(i) ) ) // If the file doesn't exist
        
{
            
dini_Create(CarFile(i)); // We will make the file (CarFile = We will script soon)
            
printf("%s created!"CarFile(i)); // This will basiclly print witch ID has been made inside the Server Console
            
new iStr[10]; // String for the plate itself
            
dini_IntSet(CarFile(i), "model"VehicleInfo[i][vModel]); // Here we set the Vehicle Model
            
dini_FloatSet(CarFile(i), "x"VehicleInfo[i][vPosx]); // Here we set the position X of the Vehicle
            
dini_FloatSet(CarFile(i), "y"VehicleInfo[i][vPosy]); // Here we set the position Y of the Vehicle
            
dini_FloatSet(CarFile(i), "z"VehicleInfo[i][vPosz]); // Here we set the position Z of the Vehicle
            
dini_FloatSet(CarFile(i), "a"VehicleInfo[i][vPosa]); // Here we set the position A of the Vehicle
            
dini_IntSet(CarFile(i), "color1"VehicleInfo[i][vColor1]); // Here we set the Color ID 1 of the vehicle
            
dini_IntSet(CarFile(i), "color2"VehicleInfo[i][vColor2]); // Here we set the Color ID 2 of the vehicle
            
dini_Set(CarFile(i), "plate""GSLS-NONREG"); // And last but not least the plate :)
            
CreateVehicle(VehicleInfo[i][vModel],VehicleInfo[i][vPosx], VehicleInfo[i][vPosy],VehicleInfo[i][vPosz],VehicleInfo[i][vPosa], VehicleInfo[i][vColor1],VehicleInfo[i][vColor2], 500000); // Alright! This is the code to create the vehicle. Zelf explainitory really... But the "500000" is basiclly the respawn timer
            
format(iStrsizeof(iStr), "GSLS - %d"i); // We will not format the plate and set it later on
            
SetVehicleNumberPlate(iiStr); // Here we set the plate, Depending on the ID so if I was infront of CAR ID 2 it will show GSLS - 2.
        
}
        else
        {
            
LoadVehicle(i); // If the file does exist it will load all the info from the .ini car file. (We will create the stock for it now.)
        
}
    }
}
// NOTE: Compiling now will give you alot of errors! 
Wow! Can't believe you actually read through everything Still... No stopping! Lets keep on going! GOGOGO

Creating LoadVehicle stock
PHP код:
stock LoadVehicle(i// If the .ini car file does exist it will execute this stock.
{
        
// All these lines will load the .ini CarFile. I explained everything before so this doesn't need explaining I guess :) If you have any questions feel free the tell me via a Reply on this topic.
    
new iStr[10]; 
    
VehicleInfo[i][vModel] = dini_Int(CarFile(i), "model");
    
VehicleInfo[i][vPosx] = dini_Float(CarFile(i), "x");
    
VehicleInfo[i][vPosy] = dini_Float(CarFile(i), "y");
    
VehicleInfo[i][vPosz] = dini_Float(CarFile(i), "z");
    
VehicleInfo[i][vPosa] = dini_Float(CarFile(i), "a");
    
VehicleInfo[i][vColor1] = dini_Int(CarFile(i), "color1");
    
VehicleInfo[i][vColor2] = dini_Int(CarFile(i), "color2");
    
myStrcpy(VehicleInfo[i][vPlate],dini_Get(CarFile(i), "plate"));
    
CreateVehicle(VehicleInfo[i][vModel],VehicleInfo[i][vPosx], VehicleInfo[i][vPosy],VehicleInfo[i][vPosz],VehicleInfo[i][vPosa], VehicleInfo[i][vColor1],VehicleInfo[i][vColor2], 500000);
    
format(iStrsizeof(iStr), "GSLS - %d"i);
    
SetVehicleNumberPlate(iiStr);
    
SetVehicleToRespawn(i); // Respawn's the vehicle itself

Last but not least we will add the CarFile stock!
PHP код:
stock CarFile(vID)
{
    new 
fn[20];
    
format(fnsizeof(fn), "Cars/Car%d.txt"vID);
    return 
fn;

NOTE: You have to make a folder called "Cars" inside the folder "Scriptfiles" for the server to run and load!

Now let's add this stock at the end of script!
PHP код:
stock myStrcpy(dest[], src[])
{
    new 
0;
    while ((
dest[i] = src[i])) i++;

DONE! We have finally finished this system! You can add new things to the enum anytime. For example vLocked,
If you have any questions feel free to PM me or make a Reply to this post.
Credits

Yoran - Tutorial


If this helped you feel free to +REP me! Thanks!
Reply
#2

Who uses DOF2 change:
PHP код:
dini_Create to DOF2_CreateFile
dini_IntSet to DOF2_SetInt
dini_FloatSet to DOF2_SetFloat
dini_Set to DOF2_SetString
dini_Int to DOF2_GetInt
dini_Float to DOF2_GetFloat
dini_Get to DOF2_GetString 
or use converter:
https://sampforum.blast.hk/showthread.php?tid=469486
Reply
#3

use Y_ini or DOF2, dini is outdated...

anyway good tutorial.
Reply
#4

Quote:
Originally Posted by PT
Посмотреть сообщение
use Y_ini or DOF2, dini is outdated...

anyway good tutorial.
Thought of using Y_ini but I do preffer dini even though its outdated ooh well... :P
Reply
#5

Quote:
Originally Posted by yoran765
Посмотреть сообщение
Thought of using Y_ini but I do preffer dini even though its outdated ooh well... :P
DOF2 is faster than the dini ...
Reply
#6

Good tutorial i like it
Reply
#7

Quote:
Originally Posted by yoran765
Посмотреть сообщение
Thought of using Y_ini but I do preffer dini even though its outdated ooh well... :P
Regardless of your personal taste, a tutorial needs to use the best methods. People assume that whatever's in a tutorial is the best method, merely because it is in a tutorial.
Reply
#8

Quote:
Originally Posted by Vince
Посмотреть сообщение
Regardless of your personal taste, a tutorial needs to use the best methods. People assume that whatever's in a tutorial is the best method, merely because it is in a tutorial.
I don't see how or why I can't use my personal taste? I can make tutorials on what and how I want
Reply
#9

You can use bCini, a new include INI of SA-MP!

https://sampforum.blast.hk/showthread.php?tid=469974
Reply
#10

Quote:
Originally Posted by Vince
Посмотреть сообщение
Regardless of your personal taste, a tutorial needs to use the best methods. People assume that whatever's in a tutorial is the best method, merely because it is in a tutorial.
I agree with you 100 percent, even if this was done with YINI I would still feel the same way the best way is either mysql or sqlite for these types of systems for that matter I won't even look at INI for anything anyways

Quote:
Originally Posted by yoran765
Посмотреть сообщение
I don't see how or why I can't use my personal taste? I can make tutorials on what and how I want
You sure can but expect people to rightfully point out the deficiencies of your methodology, it's called criticism you need to learn to handle it if your going to work with any sort of game design. That concept extends from across the spectrum from user modding to professional design a critic is a good developer and good developer is a critic. Don't expect people to say "It sucks" or "This is shit" a good developer will explain exactly as Vince has, they will keep it specific and to the point of what bothers them. You need to learn to absorb the things people don't like then make changes the things that people do like are completely irrelevant what purpose does that serve in terms of improvement?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)