Car mod saving help with mysql
#1

Is there a way to easily save all the carmods with Like | inbetween every value and read it with sscanf?
Like 12|10|1010|

And then read it from database with mysql. Anyone knows how to do that?
Reply
#2

pawn Код:
// Saving:
new
    CarMods_Info[71];

for (new s; s != 14; ++s)
{
    format(CarMods_Info, sizeof (CarMods_Info), "%s%d,", CarMods_Info, GetVehicleComponentInSlot(vehicleid, s));
}
pawn Код:
// Loading:
new
    a[14];

sscanf(string_from_mysql, "p<,>a<i>[14]", a);
Each slot stores the componentid was saved in a array.
Reply
#3

So each a[0] a[1] Is a different component?
Reply
#4

Yes. Each slot represents the type of the component: https://sampwiki.blast.hk/wiki/Componentslots
Reply
#5

Okay i got this on replacing the vehicle:
pawn Код:
House_ReplaceVehicle(HouseID, CarSlot, playerid)
{
    // Setup local variables
    new vid, cModel, Float:cx, Float:cy, Float:cz, Float:crot, Col1, Col2, Float:Health, cFuel, _CarID, cComponents[14];
    new panels, doors, lights, tires;

    // Get the data from the already existing vehicle that was parked before
    vid = AHouseData[HouseID][VehicleIDs][CarSlot];
    _CarID = AVehicleData[vid][SaveID];
    cModel = AVehicleData[vid][CarModel];
    cFuel = AVehicleData[vid][FuelData];
    Col1 = AVehicleData[vid][Color1];
    Col2 = AVehicleData[vid][Color2];
    cx = AVehicleData[vid][SpawnX];
    for (new i; i < 14; i++)
        cComponents[i] = AVehicleData[vid][Components][i];
    cy = AVehicleData[vid][SpawnY];
    cz = AVehicleData[vid][SpawnZ];
    crot = AVehicleData[vid][SpawnRot];
    GetVehicleHealth(vid, Health);
    GetVehicleDamageStatus(vid, panels, doors, lights, tires);

    // Delete the vehicle and clear the data
    Vehicle_Delete(vid);

    // Create a new vehicle in the same carslot
    vid = House_AddVehicle(HouseID, cModel, Float:cx, Float:cy, Float:cz, Float:crot, Col1, Col2, _CarID, playerid, cComponents);
    // Update the fuel of the vehicle to the previous setting
    AVehicleData[vid][FuelData] = cFuel;
    // Update the health to what it was before and update the bodywork
    SetVehicleHealth(vid, Health);
    UpdateVehicleDamageStatus(vid, panels, doors, lights, tires);

    return vid;
}
Now how to do the same thing on the loading part? i got this so far:
pawn Код:
forward OnPlayerHousesVehiclesLoad(playerid, houseid_);
public OnPlayerHousesVehiclesLoad(playerid, houseid_)
{
    printf( "OnPlayerHousesVehiclesLoad::houseid_ = %d", houseid_ );
    new rows, fields;
    cache_get_data( rows, fields, Handle );

    if( rows )
    {
        print("There are rows - Vehicles");
        new
        idx = 0,
        cmodel2, Float: cx2, Float: cy2, Float: cz2, Float: crot2, ccol1, ccol2, _carid, mods[71];
        while( rows > idx )
        {
            _carid =    cache_get_row_int(idx, 1);
            cmodel2 =   cache_get_row_int( idx, 3 );
            cx2 =       cache_get_row_float( idx, 4 );
            cy2 =       cache_get_row_float( idx, 5 );
            cz2 =       cache_get_row_float( idx, 6 );
            crot2 =     cache_get_row_float( idx, 7 );
            ccol1 =     cache_get_row_int( idx, 8 );
            ccol2 =     cache_get_row_int( idx, 9 );
            //Modding
            cache_get_row( 0, 14, mods, Handle, 71 );
            printf("ccol1: %i, ccol2: %i", ccol1, ccol2);
           
            printf("House_AddVehicle(%d,%d,%f,%f,%f,%f,%d,%d, %d);", houseid_, cmodel2, cx2, cy2, cz2, crot2, ccol1, ccol2, _carid);
            House_AddVehicle(houseid_, cmodel2, cx2, cy2, cz2, crot2, ccol1, ccol2, _carid, playerid, mods);
            print("Car loaded");

            idx++;
        }
    }
    return 1;
}
mods is the string now how to use mods in the House_Addvehicle?
Reply
#6

You should know the vehicleid so I guess you have to return the vehicleid with House_AddVehicle or somehow pass it as variable by reference.

pawn Код:
new
    a[14],
    house_vehicleid;

cache_get_row(0, 14, mods, Handle, 71);
sscanf(mods, "p<,>a<i>[14]", a);

// House_AddVehicle here.. get the vehicleid somehow.. and store it to "house_vehicleid"

for (new s; s != 14; ++s)
{
    if (a[s]) AddVehicleComponent(house_vehicleid, a[s]);
}
Reply
#7

I got this right now:
pawn Код:
House_AddVehicle(HouseID, cModel, Float:cx, Float:cy, Float:cz, Float:crot, Col1, Col2, CarID, playerid, cComponents[])
{
    new vid, CarSlot, pName[MAX_PLAYER_NAME], string[126], modelid;//, string2[128];
    GetPlayerName(playerid, pName, sizeof(pName));
    CarSlot = House_GetFreeCarSlot(HouseID);
    printf("House_AddVehicle::CarSlot = %d", CarSlot);
    if (CarSlot != -1)
    {
        vid = Vehicle_Create(cModel, cx, cy, cz, crot, Col2, Col1, 600);
        printf("House_AddVehicle::vid= %d", vid);
        AHouseData[HouseID][VehicleIDs][CarSlot] = vid;
        printf("House_AddVehicle::AHouseData[%d][VehicleIDs][%d]= %d", HouseID, CarSlot, AHouseData[HouseID][VehicleIDs][CarSlot]);
        AVehicleData[vid][CarModel] = cModel;
        AVehicleData[AHouseData[HouseID][VehicleIDs][CarSlot]][SaveID] = CarID;
        // Save the spawn-data of the vehicle
        AVehicleData[vid][SpawnX] = cx;
        AVehicleData[vid][SpawnY] = cy;
        AVehicleData[vid][SpawnZ] = cz;
        AVehicleData[vid][SpawnRot] = crot;
        AVehicleData[vid][Color1] = Col2;
        AVehicleData[vid][Color2] = Col1;
        for (new s; s != 14; ++s)
        {
            if (cComponents[s]) AddVehicleComponent(vid, cComponents[s]);
        }
        // Apply the given parameters to the vehicle
        AVehicleData[vid][FuelData] = MaxFuel;
        AVehicleData[vid][Owned] = true;
        ChangeVehicleColor(vid, AVehicleData[vid][Color1], AVehicleData[vid][Color2]);
        format(AVehicleData[vid][Owner], 24, pName);
        AVehicleData[vid][BelongsToHouse] = HouseID;
        modelid = GetVehicleModel(vid);
        format(string, sizeof(string), "%s's %s", pName, VehicleNames2[modelid-400]);
        VehText[vid] = Create3DTextLabel(string,0xFFFFFFFF,0,0,0,100.0,0);
        Attach3DTextLabelToVehicle(VehText[vid], vid, 0,0,0);
        print("Vehicle loaded");
    }
    else
    {
        print ("No free carslot");// No free carslot was found, return 0
        return 0;
    }

    // Exit the function and return the vehicle-id
    return vid;
}
And the loading
pawn Код:
public OnPlayerHousesVehiclesLoad(playerid, houseid_)
{
    printf( "OnPlayerHousesVehiclesLoad::houseid_ = %d", houseid_ );
    new rows, fields;
    cache_get_data( rows, fields, Handle );

    if( rows )
    {
        print("There are rows - Vehicles");
        new
        idx = 0,
        cmodel2, Float: cx2, Float: cy2, Float: cz2, Float: crot2, ccol1, ccol2, _carid, mods[71], a[14];
        while( rows > idx )
        {
            _carid =    cache_get_row_int(idx, 1);
            cmodel2 =   cache_get_row_int( idx, 3 );
            cx2 =       cache_get_row_float( idx, 4 );
            cy2 =       cache_get_row_float( idx, 5 );
            cz2 =       cache_get_row_float( idx, 6 );
            crot2 =     cache_get_row_float( idx, 7 );
            ccol1 =     cache_get_row_int( idx, 8 );
            ccol2 =     cache_get_row_int( idx, 9 );
            //Modding
            cache_get_row( 0, 14, mods, Handle, 71 );
            sscanf(mods, "p<,>a<i>[14]", a);
           
            printf("ccol1: %i, ccol2: %i", ccol1, ccol2);
           
            printf("House_AddVehicle(%d,%d,%f,%f,%f,%f,%d,%d, %d);", houseid_, cmodel2, cx2, cy2, cz2, crot2, ccol1, ccol2, _carid);
            House_AddVehicle(houseid_, cmodel2, cx2, cy2, cz2, crot2, ccol1, ccol2, _carid, playerid, a);
            print("Car loaded");

            idx++;
        }
    }
    return 1;
}
But its not loading the mods. Any idea why?
Reply
#8

Debug it to both functions and see what it prints.
Reply
#9

When i do this :
pawn Код:
for(new i; i < 14; i++)
                printf("%i", a[i]);
at OnPlayerHousesVehiclesLoad it just gives out 14 zero's and nothing else. No mods whatsoever. So it already goes wrong there. Also when i tried printing the mods string it just sends NULL to console so somehow the string is empty?
Reply
#10

Also i got this in my mysql error log:
Код:
[14:44:41] [WARNING] CMySQLResult::GetRowData - invalid row ('0') or field index ('14')
[14:49:50] [ERROR] CMySQLQuery::Execute[()] - (error #1064) You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Mods WHERE ID = 0' at line 1
[14:50:33] [WARNING] cache_get_data - no active cache
[14:50:33] [WARNING] CMySQLResult::GetRowData - invalid row ('0') or field index ('14')
[14:58:57] [WARNING] cache_get_data - no active cache
[14:58:57] [WARNING] CMySQLResult::GetRowData - invalid row ('0') or field index ('14')
[14:59:40] [WARNING] CMySQLHandle::Create - connection already exists
[14:59:40] [WARNING] cache_get_data - no active cache
[14:59:40] [WARNING] CMySQLResult::GetRowData - invalid row ('0') or field index ('14')
[15:01:49] [WARNING] CMySQLHandle::Create - connection already exists
[15:01:49] [WARNING] cache_get_data - no active cache
[15:01:49] [WARNING] CMySQLResult::GetRowData - invalid row ('0') or field index ('14')
[15:03:03] [WARNING] CMySQLHandle::Create - connection already exists
[15:03:03] [WARNING] cache_get_data - no active cache
[15:03:04] [WARNING] CMySQLResult::GetRowData - invalid row ('0') or field index ('14')
[15:03:54] [WARNING] CMySQLHandle::Create - connection already exists
[15:03:54] [WARNING] cache_get_data - no active cache
[15:03:55] [WARNING] CMySQLResult::GetRowData - invalid row ('0') or field index ('14')
[15:04:55] [WARNING] CMySQLHandle::Create - connection already exists
[15:04:55] [WARNING] cache_get_data - no active cache
[15:04:56] [WARNING] CMySQLResult::GetRowData - invalid row ('0') or field index ('14')
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)