[Tutorial] Vehicle Ownership With Mysql & SScanf
#1

- Vehicle Ownership
As there's a lot of people struggling with Mysql, i decided to write an tutorial for you guys.
I assume you guys know how to use mysql, and does have some pawn knowledge.
In this tutorial i'll be using Strickens plugin.

First some define's:

pawn Код:
#define SCRIPT_CARS 100 // You could also #undef MAX_VEHICLES and define it once again.. that's really up to you.
#define DEFAULT_VEHICLE_OWNER "Dealership"
#define DEFAULT_SELL_PRICE 50000
Okay, that probably doesn't make any sense at all right now, but let's go further.

As this is an tutorial about Vehicle Ownership and basic mysql im not showing how to import the db structure.
Here's the db structure anyway:

Код:
CREATE TABLE IF NOT EXISTS `vehicles` (
  `id` int(5) NOT NULL AUTO_INCREMENT,
  `owner` varchar(25) NOT NULL,
  `model` int(5) NOT NULL,
  `price` int(11) NOT NULL,
  `x` float NOT NULL,
  `y` float NOT NULL,
  `z` float NOT NULL,
  `a` float NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
Now let's setup the Variable.

To make this really easy i'm using SScanf.


These two enums keeps track of the loaded positions and how many vehicles that is currently loaded.
pawn Код:
enum loadedInfo
{
    Vehicles = 0,
}
new LoadedInfo[loadedInfo];

enum posInfo
{
    Float: X,
    Float: Y,
    Float: Z,
    Float: R,
}
pawn Код:
enum vInfo // You could really name this anything.. Foo, pop, vehicle, vehicles whatever you like.
{
     Id, // This is needed as SScanf loads the car variables through this Enum, i'll get back to that later.
     Owner[25],
     Model,
     Price,
     Float:Pos[posInfo], // Arrays ftw.
}
new VehicleInfo[SCRIPT_CARS][vInfo]; // Remember this ? the define we just made.. Great ! you'r actually reading.
Okay, now.. to load things from the db, we need an load function, this is really pretty simple.. just follow along.

pawn Код:
stock LoadVehicles() // You might want to make this an public so you could call it on an timer.
{
    new Query[255], id;
    format(Query, sizeof(Query), "SELECT * FROM vehicles"); // Formats the query for "mysql_query"
    mysql_query(Query); // Querys the "Query" Variable.
    mysql_store_result(); // Stores the result from Query
    while(mysql_fetch_row(Query,"|")) // Splits the row
    {
        id = LoadedInfo[Vehicles];
        sscanf(Query, "p<|>e<is[25]iiffff>", VehicleInfo[id]); // Pretty neat ehh? [ ID, OWNER, MODEL, PRICE, POS X, POS Y, POS Z, POS A ]
        new Color1 = random(126); new Color2 = random(126);
        CreateVehicle(VehicleInfo[id][Model],VehicleInfo[id][Pos][X],VehicleInfo[id][Pos][Y],VehicleInfo[id][Pos][Z],VehicleInfo[id][Pos][R],Color1,Color2, 60*10000);
        LoadedInfo[Vehicles] = LoadedInfo[Vehicles] + 1;
    }
}

SScanf Loads the db vars into our enum in one simple line.
"p<|>" = Delimiter. / Split's the string.
"e<>" = Enum. / Uses the enum we made.
"i" = Int. / Loads int's. Model, Price.
"f" = Float. / Loads Float's. Position.
"s[25]" = String. / Loads String's. Owner.

Command's:

pawn Код:
command(createveh, playerid, params[]) // Create vehicle command, not Tested.
{
    new vID, Query[200];
    if(sscanf(params, "i", vID)) return SendClientMessage(playerid, COLOR_ADMIN, "Usage: /createveh [ ModelID ]");
    new Float:pPOS[4];
    GetPlayerPos(playerid, pPOS[0], pPOS[1], pPOS[2]);
    GetPlayerFacingAngle(playerid , pPOS[3]);
    format(Query, sizeof(Query), "INSERT INTO vehicles (id, owner, model, price, x, y, z, a) VALUES (NULL, '%s', %d, %d, %f, %f, %f, %f);",DEFAULT_VEHICLE_OWNER,vID, DEFAULT_VEHICLE_PRICE, pPOS[0], pPOS[1], pPOS[2], pPOS[3]);
    mysql_query(Query);
    format(Query, sizeof(Query), "Created an %d at x: %f y: %f z: %f a: %f);",vID,pPOS[0], pPOS[1], pPOS[2], pPOS[3]);
    SendClientMessage(playerid, COLOR_ADMIN, Query);
    return 1;
}

command(reloadvehicles, playerid, params[]) // Reload's Vehicles
{
    for(new i; i < SCRIPT_CARS; i++)
    {
        if(IsVehicleConnected(i)) DestroyVehicle(i);
    }
    LoadVehicles();
    SendClientMessage(playerid, COLOR_ADMIN, "Vehicle's reloaded");    
}
Others:

pawn Код:
public OnPlayerEnterVehicle(playerid, vehicleid)
{
     if(strmatch(VehicleInfo[vehicleid][Owner], GetName(playerid)))
     {
          return 1;
     }
     else
     {
          // clear animations, cant be bothered searching it up x))
     }
     return 1;
}

stock strmatch(const String1[], const String2[])
{
    if ((strcmp(String1, String2, true, strlen(String2)) == 0) && (strlen(String2) == strlen(String1)))
    {
        return true;
    }
    else
    {
        return false;
    }
}
Okay, That's about it, Good luck.
Questions etc will be answered as far as my knowledge reaches.

I need to to credit a couple of people:

SScanf - ******.
Mysql - icognito.
Making everything possible - the sa-mp team.
Writing this shit - Me.

As you can see this tutorial does not include how to make the commands / saving function needed in an vehicle ownership. if many people request i will do it. But for now, I'll leave it as it stands.

Here's some tutorials to fully "complete" this tutorial:

https://sampforum.blast.hk/showthread.php?tid=91130 - By GTA_Rules ( Restrict cars to one name )
https://sampforum.blast.hk/showthread.php?tid=118379 - By [HiC]TheKiller ( Buyable houses )
https://sampforum.blast.hk/showthread.php?tid=179206 - By Kwarde ( house system (with vehicles) )
Reply
#2

Nice tutorial! hey i need to know this xD make the last part u.u
Reply
#3

Quote:
Originally Posted by gigi1223
Посмотреть сообщение
Nice tutorial! hey i need to know this xD make the last part u.u
Thanks & Yeah, maybe i will
Reply
#4

Nice will be used, not much maybe since not many people use MySQL + sscanf.
Reply
#5

Quote:
Originally Posted by willsuckformoney
Посмотреть сообщение
Nice will be used, not much maybe since not many people use MySQL + sscanf.
Great

Yeah, i hope someday in the future people will start using mysql, its a much more efficient way to load / save.
Reply
#6

Plus lag source? I seen some servers that have a lot of lines in them that uses MySQL they lag up a little
Reply
#7

Quote:
Originally Posted by willsuckformoney
Посмотреть сообщение
Plus lag source? I seen some servers that have a lot of lines in them that uses MySQL they lag up a little
Probably not using localhost, Or reloading 2k cars every 10'th sec
Reply
#8

ZOMFG 8CHARS!
Quote:
Originally Posted by Cameltoe
Посмотреть сообщение
Or reloading 2k cars every 10'th sec
Reply
#9

Updated.
Reply
#10

Код:
C:\Documents and Settings\Piim\Desktop\samp03bsvr_R2_win32\gamemodes\CleanScript(MySQL).pwn(2254) : warning 219: local variable "Query" shadows a variable at a preceding level
C:\Documents and Settings\Piim\Desktop\samp03bsvr_R2_win32\gamemodes\CleanScript(MySQL).pwn(2261) : error 035: argument type mismatch (argument 3)
C:\Documents and Settings\Piim\Desktop\samp03bsvr_R2_win32\gamemodes\CleanScript(MySQL).pwn(2271) : warning 219: local variable "Query" shadows a variable at a preceding level
C:\Documents and Settings\Piim\Desktop\samp03bsvr_R2_win32\gamemodes\CleanScript(MySQL).pwn(2287) : error 017: undefined symbol "IsVehicleConnected"
Pawn compiler 3.2.3664	 	 	Copyright © 1997-2006, ITB CompuPhase


2 Errors.
Help needed
Reply
#11

Hey, thanks for this. I will definitely be learning from this tutorial. xD
Reply
#12

Code:
C:\Documents and Settings\Piim\Desktop\samp03bsvr_R2_win32\gamemodes\CleanScript(MySQL).pwn(2254) : warning 219: local variable "Query" shadows a variable at a preceding level
C:\Documents and Settings\Piim\Desktop\samp03bsvr_R2_win32\gamemodes\CleanScript(MySQL).pwn(2261) : error 035: argument type mismatch (argument 3)
C:\Documents and Settings\Piim\Desktop\samp03bsvr_R2_win32\gamemodes\CleanScript(MySQL).pwn(2271) : warning 219: local variable "Query" shadows a variable at a preceding level
C:\Documents and Settings\Piim\Desktop\samp03bsvr_R2_win32\gamemodes\CleanScript(MySQL).pwn(2287) : error 017: undefined symbol "IsVehicleConnected"
Pawn compiler 3.2.3664	 	 	Copyright © 1997-2006, ITB CompuPhase


2 Errors.
Help needed
I still need help ,please!
Reply
#13

I know these two errors means you're doing "new Query[xxx]" when you don't need to.

Code:
C:\Documents and Settings\Piim\Desktop\samp03bsvr_R2_win32\gamemodes\CleanScript(MySQL).pwn(2254) : warning 219: local variable "Query" shadows a variable at a preceding level
C:\Documents and Settings\Piim\Desktop\samp03bsvr_R2_win32\gamemodes\CleanScript(MySQL).pwn(2271) : warning 219: local variable "Query" shadows a variable at a preceding level
Reply
#14

I realy dont care about warnings
Reply
#15

Quote:
Originally Posted by Siim
View Post
I realy dont care about warnings
Just because it says "warning" not "error" doesn't mean it's not important. It only shows how careless you are and if you aren't willing to remove those warnings, then fix it yourself.
Reply
#16

Quote:
Originally Posted by RealCop228
View Post
Hey, thanks for this. I will definitely be learning from this tutorial. xD
Great Tell me if everything works as it should, compiles fine etc.. i kinda wrote this in the browser.

Quote:
Originally Posted by Siim
View Post
I realy dont care about warnings
Well, you should.
Reply
#17

Okey i got rid of the warning but i still have the errors
Please help.
Reply
#18

Post lines 2261 and 2287.
Reply
#19

if(mysql_fetch_row(Query, "|")) // Splits the row
if(IsVehicleLoaded(i)) DestroyVehicle(i);
Reply
#20

I did it all and when i go ingame and type /createveh 562 my server closes..
There is no reports on the server log.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)