SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 21/05/2019, 11:08 PM   #1
KeyOfKey
Big Clucker
 
KeyOfKey's Avatar
 
Join Date: Apr 2019
Posts: 196
Reputation: 8
Default Another problem

It's me again, i managed to fix the label not showing, now i have another problem. This /cvehicle commands inserts correctly the vehicle in database and assigns it an id, everything works until i restart the server.

It starts again from ID 1 when creating a vehicle, even if there's already a vehicle with that id, when it should continue to the next free available id. So this is the result:



The first 4 vehicles are created, then i restarted the server, created another one and as you can see, its id is 1, when it should be 5.

This is the command:

pawn Code:
CMD:cvehicle(playerid, params[])
{
    if(Player[playerid][AdminLevel] < 5) return ErrorMessage(playerid, COLOR_ERROR, 1);

    new car[50], price, vehid, color1, color2, Float:x, Float:y, Float:z, Float:angle, string[300], id;
   
    id = Iter_Free(ServerVehicles);

    if(sscanf(params, "s[50]ddd", car, price, color1, color2)) return SendUsageMessage(playerid, "/cvehicle [vehicle id/name] [price] [color 1 (255 for random)] [color 2 (255 for random)]");

    if(!isnumeric(car))
    {
        vehid = GetVehicleModelIDFromName(car);
    }
    else
    {
        vehid = strval(car);
    }

    if(color1==255) color1=random(256);
    if(color2==255) color2=random(256);
       
    if((vehid < 400 || vehid > 611)) return SendErrorMessage(playerid, "Invalid vehicle ID.");
    if((color1 < 0 || color1 > 255) || color2 < 0 || color2 > 255) return SendErrorMessage(playerid, "Vehicle color must be between -1 and 255.");
    if((price < 1) || (price > 999999)) return SendErrorMessage(playerid, "Invalid price.");

    Iter_Add(ServerVehicles, id);

    GetPlayerPos(playerid, x, y, z);
    GetPlayerFacingAngle(playerid, angle);

    format(VehicleData[id][v_Owner], MAX_PLAYER_NAME, "-");

    VehicleData[id][v_Model] = vehid;

    VehicleData[id][v_Color1] = color1;
    VehicleData[id][v_Color2] = color2;

    SetVehicleNumberPlate(VehicleData[id][v_Create], VehicleData[id][v_Plate]);

    VehicleData[id][v_Price] = price;

    VehicleData[id][v_PosX] = x;
    VehicleData[id][v_PosY] = y;
    VehicleData[id][v_PosZ] = z;
    VehicleData[id][v_PosA] = angle;

    LoadVehicleMod(vehid, id);

    format(string, sizeof(string), "INSERT INTO `vehicles` (`id`, `owner`, `model`, `color1`, `color2`, `price`, `v_PosX`, `v_PosY`, `v_PosZ`, `v_PosA`) VALUES ('%d', '%s', '%d', '%d', '%d', '%d', '%f', '%f', '%f', '%f')", id, VehicleData[id][v_Owner], vehid, color1, color2, price, x, y, z, angle);
    mysql_tquery(g_SQL, string);

    VehicleData[id][v_Create] = CreateVehicle(vehid, x, y, z, angle, color1, color2, -1);

    format(string, sizeof(string), "%s for sale\n Price: %s", VehicleNames[vehid-400], formatInt(price));
    VehicleData[id][v_Label] = Create3DTextLabel(string, 0xFF0000AA, x, y, z, 50.0, 0, 1 );
    Attach3DTextLabelToVehicle(VehicleData[id][v_Label], VehicleData[id][v_Model], -0.7, -1.9, -0.3);

    printf("Creted v_label for vdb_id: %d - vid: %d - string: %s", id, VehicleData[id][v_Model], string);

    SCMEX(playerid, 0xFFFF00FF, "You have created a(n) %s. ID: %d - Price: %s - Color 1: %d - Color 2: %d", VehicleNames[vehid-400], id, formatInt(price), color1, color2);
   
    return 1;
}

I don't have PRIMARY or AUTO_INCREMENT for "id" row since i use Iter_Add to assign an id. Also "id" row isn't saved in the vehicle enum. (It's just created in database). This is the function i use to load vehicles when server starts.

pawn Code:
function LoadVehicles()
{
    new rows = cache_num_rows();

    new id, idx, str[32];

    if(rows)
    {
        while(idx < rows)
        {
            cache_get_value_name(idx, "owner", VehicleData[id][v_Owner], MAX_PLAYER_NAME);

            cache_get_value_name_int(idx, "model", VehicleData[id][v_Model]);
            cache_get_value_name_int(idx, "lock", VehicleData[id][v_Lock]);

            cache_get_value_name_int(idx, "color1", VehicleData[id][v_Color1]);
            cache_get_value_name_int(idx, "color2", VehicleData[id][v_Color2]);

            cache_get_value_name(idx, "plate", VehicleData[id][v_Plate], 16);

            cache_get_value_name_int(idx, "paintjob", VehicleData[id][v_Paintjob]);

            for(new mods = 0; mods < 14; mods++)
            {
                format(str, sizeof(str), "mods%d", mods+1);
                cache_get_value_name_int(idx, str, VehicleData[id][v_Mods][mods]);
            }

            cache_get_value_name_int(idx, "price", VehicleData[id][v_Price]);

            cache_get_value_name_int(idx, "owned", VehicleData[id][v_Owned]);

            cache_get_value_name_int(idx, "playerprice", VehicleData[id][v_PlayerPrice]);

            cache_get_value_float(idx, "v_PosX", VehicleData[id][v_PosX]);
            cache_get_value_float(idx, "v_PosY", VehicleData[id][v_PosY]);
            cache_get_value_float(idx, "v_PosZ", VehicleData[id][v_PosZ]);
            cache_get_value_float(idx, "v_PosA", VehicleData[id][v_PosA]);

            cache_get_value_name_int(idx, "ownerid", VehicleData[id][v_OwnerID]);

            cache_get_value_float(idx, "v_Mileage", VehicleData[id][v_Mileage]);

            VehicleData[id][v_Create] = CreateVehicle(VehicleData[id][v_Model], VehicleData[id][v_PosX], VehicleData[id][v_PosY], VehicleData[id][v_PosZ], VehicleData[id][v_PosA], VehicleData[id][v_Color1], VehicleData[id][v_Color2], -1);

            Iter_Add(ServerVehicles, id);


            idx++;
        }
    }
    return 1;
}

Thank you.
KeyOfKey is offline   Reply With Quote
Old 22/05/2019, 12:01 AM   #2
Plastikmensch
Big Clucker
 
Join Date: Aug 2016
Posts: 171
Reputation: 8
Default Re: Another problem

I am not that familiar with YSI and sorry if it doesn't work, but judging from the include
Code:
VehicleData[id][v_Create] = CreateVehicle(VehicleData[id][v_Model], VehicleData[id][v_PosX], VehicleData[id][v_PosY], VehicleData[id][v_PosZ], VehicleData[id][v_PosA], VehicleData[id][v_Color1], VehicleData[id][v_Color2], -1);

            Iter_Add(ServerVehicles, id);
Should be
Code:
VehicleData[idx][v_Create] = CreateVehicle(VehicleData[id][v_Model], VehicleData[id][v_PosX], VehicleData[id][v_PosY], VehicleData[id][v_PosZ], VehicleData[id][v_PosA], VehicleData[id][v_Color1], VehicleData[id][v_Color2], -1);

            Iter_Add(ServerVehicles, idx);
Or in short: replace id with idx or increase id for every row (same thing).
Plastikmensch is offline   Reply With Quote
Old 22/05/2019, 04:17 AM   #3
KeyOfKey
Big Clucker
 
KeyOfKey's Avatar
 
Join Date: Apr 2019
Posts: 196
Reputation: 8
Default Re: Another problem

Ok umh i did what you said, i replaced id with idx in LoadVehicles. Now this is the situation:



ID 1 and 2 are the vehicles created before start, i restarted the server, and as you can see, server resumed from last created vehicle (id 2, so i have two different vehicles with same ID), and the 3rd i created after some seconds.

It should resume from last AVAILABLE id (so if last vehicle db id before restart is 2, resume from 3 after restart). What's wrong?
KeyOfKey is offline   Reply With Quote
Old 22/05/2019, 04:34 AM   #4
Threshold
High-roller
 
Threshold's Avatar
 
Join Date: Nov 2012
Location: Australia
Posts: 3,096
Reputation: 521
Default Re: Another problem

You need to give your 'id' column a 'Unique' constraint. You can do this by either making the 'id' a primary key or unique.

https://www.w3schools.com/sql/sql_unique.asp

Upon doing this, every time you insert into the table, the row will be assigned with a unique ID. So you don't have to create the ID yourself.

EDIT: Lol derp, skipped the first post in this thread apparently?

Are you re-assigning values to your ServerVehicles Iter when the server starts again? You should load the values from the database and assign them before creating more vehicles.

EDIT #2:
Also, you never assign a value to 'id' in your LoadVehicles function..
__________________

Kar's Cops and Robbers 2019
Threshold is offline   Reply With Quote
Old 22/05/2019, 05:14 AM   #5
KeyOfKey
Big Clucker
 
KeyOfKey's Avatar
 
Join Date: Apr 2019
Posts: 196
Reputation: 8
Default Re: Another problem

I managed to fix it. I removed the iterator and added some checks, also added the vehicle_DBID to the LoadVehicles functions. Thanks.
KeyOfKey is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Password Hash Problem & MYSQL Saving Problem Faqahat Scripting Help 3 18/04/2016 11:54 AM
[SOLVED] 3 problem's! (1 problem still unsolved, but stopped solving.) Mechscape Help Archive 9 24/12/2009 08:47 PM


All times are GMT. The time now is 12:39 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.