Loading Player's Vehicle
#1

Hi there, I've been having a little bit of problem loading my player vehicles...

My LoadPlayerCars code:

pawn Код:
stock LoadPlayerCars(carid)
{
    new query[500];
    format(query,sizeof(query),"SELECT * FROM vehicles WHERE `Vehicle_ID` = %d",carid);
    mysql_query(query);
    mysql_store_result();
    new Results[256];
    while(mysql_fetch_row(query))
    {

        mysql_fetch_field("Vehicle_ID",Results);
        Vehicle[carid][V_ID] = strval(Results);

        mysql_fetch_field("Model_ID",Results);
        Vehicle[carid][Model] = strval(Results);

        mysql_fetch_field("Owner",Vehicle[carid][Owner]);

        mysql_fetch_field("Vehicle_Position", Results);
        sscanf(Results, "ffff", Vehicle[carid][CarPosition][0], Vehicle[carid][CarPosition][1], Vehicle[carid][CarPosition][2],Vehicle[carid][CarPosition][3]);

        mysql_fetch_field("Vehicle_Deal_Position", Results);
        sscanf(Results, "ffff", Vehicle[carid][SpawnDealPosition][0], Vehicle[carid][SpawnDealPosition][1], Vehicle[carid][SpawnDealPosition][2],Vehicle[carid][SpawnDealPosition][3]);

        mysql_fetch_field("Vehicle_Value", Results);
        Vehicle[carid][Vehicle_Price] = strval(Results);

        mysql_fetch_field("Color_ID_1", Results);
        Vehicle[carid][Vehicle_Paint] = strval(Results);

        mysql_fetch_field("Color_ID_2", Results);
        Vehicle[carid][Vehicle_Paint2] = strval(Results);
        Vehicle[carid][SecondV_ID] = CreateVehicle(Vehicle[carid][Model],Vehicle[carid][CarPosition][0], Vehicle[carid][CarPosition][1], Vehicle[carid][CarPosition][2],Vehicle[carid][CarPosition][3],Vehicle[carid][Vehicle_Paint],Vehicle[carid][Vehicle_Paint2],60000);

        printf(" V_ID: %d | SecondV_ID: %d | Owner: %s | i= %d",Vehicle[carid][V_ID],Vehicle[carid][SecondV_ID],Vehicle[carid][Owner],carid);
    }
}
My Current problem is that whenever a player who owns a vehicle disconnects, he'll be the owner of the next player vehicle thats spawned. For example

Player A:
Buys Vehicle
Gets Assigned SecondV_ID(this is the variable used to create the car) 312
Gets Assigned V_ID(the vehicle id stored in the db for reference) 322
Disconnects
SecondV_ID is destroyed(despawned)

Player B:
Spawns with V_ID 325
Vehicle V_ID 325 loads into arrays
Vehicle V_ID 325 spawns with SecondV_ID 312(this is the bug)
Player B's vehicle owner is Player A.(obvious bug)


Thats the best i can explain, if you need clarification on anything please let me know. I really need help on this. It'll be Very grateful of you if you help.

I hope to get a reply fast.
Reply
#2

How is carid determined ?
Reply
#3

Carid is determined by the id that saved when the player buys the vehicle.

So when a car is bought the cariable UserInfo[playerid][Vehicle_ID_1] = to the ID in the database, and to load the cars i use

pawn Код:
LoadPlayerCars(UserInfo[playerid][Vehicle_ID_1];
Reply
#4

bump, i really need help on this
Reply
#5

Please Help.
Reply
#6

Okay I took another look and there is some obvious problems here.

pawn Код:
format(query,sizeof(query),"SELECT * FROM vehicles WHERE `Vehicle_ID` = %d",carid);
This is never going to work as expected in fact you would be limited to 2000 vehicles doing this what you need to do is select by player name or save vehicles owner reference by the players primary integer key in your database so your query is going to be something like.

pawn Код:
format(query,sizeof(query),"SELECT * FROM vehicles WHERE `Owner` = %s", playername);
or

pawn Код:
format(query,sizeof(query),"SELECT * FROM vehicles WHERE `Owner` = %s", PlayerData[playerid][PrimaryKey]);
The player may have multiple vehicles which is perfectly fine you simply would need to load each row from your result.

Another issue is you should create the vehicle first and use that id to store all load data in your array basically instead of directly loading the data in your variable load it into temporary variables create the vehicle then assign that data to the index of the created vehicle.
Reply
#7

Well if i use LoadPlayerCars(playerid) instead of (carid) what am i supposed to use for the [carid] when storing data into my arrays?
Reply
#8

Quote:
Originally Posted by MikeEd
Посмотреть сообщение
Well if i use LoadPlayerCars(playerid) instead of (carid) what am i supposed to use for the [carid] when storing data into my arrays?
You use the id of the created vehicle of course.
Reply
#9

So here's what i should do,

Create temporary variables, store the data received from the mysql database, then after the vehicle is created i use the data from the temp variables and store them into the arrays...?

However i have a small question... you can obviously see how i create my vehicle, i use the SecondV_ID variable but if i want to create vehicle from the temp variables what should i use and wouldn't it keep using the same id over and over again?
Reply
#10

Vehicle id is dynamic it can really be anything depending on how many players are connected and how many vehicles they have
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)