Help with vehicle mods
#1

Hello, I'm trying to add car modifications. I have made these basic system to test it after to implement in the gamemode (MySQL based).

Код:
// ModTest will be the string in MySQL stored and VehicleToImplement will be the MODEL to show the components.
new ModTest[100] = "1000,1004,1006,1007,1013,0,1018,1078,0,1087,0,0,0,1143";
new VehicleToImplement = 496;

// This is called from OnGameModeInit because I create the cars from MySQL. 'idx' is the id of the car in MySQL.
CreateVehicleTest(idx)
{
    Car[idx][VehicleID] = CreateVehicle(Car[idx][Model], Car[idx][pX], Car[idx][pY], Car[idx][pZ], Car[idx][pA], Car[idx][Color1], Car[idx][Color2], -1);
    SetVehicleNumberPlate(Car[idx][VehicleID], VehiclePlate(idx));
    SetVehicleHealth(Car[idx][VehicleID], Car[idx][Health]);
    UpdateVehicleDamageStatus(Vehicle[idx][VehicleID], 0, 0, 0, 0);
    if(Car[idx][Model] == VehicleToImplement)
    {
        printf("SQL: %i", Car[idx][ID]);
        new a[14];
        sscanf(ModTest, "p<,>a<i>[14]", a);
        for (new s; s < 14; ++s)
	{
		if (a[s] > 0)
		{
			AddVehicleComponent(Car[idx][VehicleID], a[s]);
			printf("Component: %i", a[s]);
		}
	}
	printf("-------------- END");
    }
}
Debug:
Код:
[14:03:54] SQL: 69
[14:03:54] Component: 1000
[14:03:54] Component: 1004
[14:03:54] Component: 1006
[14:03:54] Component: 1007
[14:03:54] Component: 1013
[14:03:54] Component: 1018
[14:03:54] Component: 1078
[14:03:54] Component: 1087
[14:03:54] Component: 1143
[14:03:54] -------------- END
Problem: Only loads the first five.

Код:
1000,  -> Loads
1004,  -> Loads
1006,  -> Loads
1007,  -> Loads
1013,  -> Loads
0,       -> Stops and is not applying the rest of components
1018,
1078,
0,
1087,
0,
0,
0,
1143
I know AddVehicleComponent 0 will crash, but I have an if statement to check this:
Код:
if (a[s] > 0) // <- Here, if component is > 0 will apply the component ant get printed.
		{
			AddVehicleComponent(Car[idx][VehicleID], a[s]);
			printf("Component: %i", a[s]);
		}
But then, when I respawn the vehicle, all components gets added, I've added in the OnVehicSpawn function the same process when CreateVehicleTest();

Anyone can help me?
Reply
#2

Why do you save 14 numbers as one string in a Database?
You could either make an adjacent list as new table, or add 14 fields to the existing table. Then you can directly apply them when queried for and don't have to split up the string.

Just saying, seems unneccessary to me. But it should work either way.

Код:
1000,  -> Loads
1004,  -> Loads
1006,  -> Loads
1007,  -> Loads
1013,  -> Loads
0,       -> Stops and is not applying the rest of components
1018,
1078,
0,
1087,
0,
0,
0,
1143
Regarding this, does it not add the remaining components or does it fail to extract them from the string?

You should at first add a check to the return value of the sscanf function, then you know if sscanf failed to extract the values, or if the values are correct but they don't apply as they should.
Reply
#3

I think sscanf it's a simpliest way to save 14 columns in mysql.


Код:
if (a[s] > 0)
{
	AddVehicleComponent(Car[idx][VehicleID], a[s]);
	printf("Component: %i", a[s]);
}

// The printf results:
[14:03:54] Component: 1000
[14:03:54] Component: 1004
[14:03:54] Component: 1006
[14:03:54] Component: 1007
[14:03:54] Component: 1013
[14:03:54] Component: 1018
[14:03:54] Component: 1078
[14:03:54] Component: 1087
[14:03:54] Component: 1143

// As you can see, printf prints all the components to install, but the string contains zeros. When the for arrives to a 0, no component is added, logical because the slot is empty but then, the rest of the components installed are not applied.

/*

1000,  -> Load, printed and installed.
1004,  -> Load, printed and installed.
1006,  -> Load, printed and installed.
1007,  -> Load, printed and installed.
1013,  -> Load, printed and installed.
0,       -> Load, not printed and not installed because is 0 (empty).
1018,  -> Load, printed and NOT installed
1078,  -> Load, printed and NOT installed
0,       -> Load, not printed and not installed because is 0 (empty).
1087,  -> Load, printed and NOT installed
0,       -> Load, not printed and not installed because is 0 (empty).
0,       -> Load, not printed and not installed because is 0 (empty).
0,       -> Load, not printed and not installed because is 0 (empty).
1143   -> Load, printed and NOT installed

*/
If I respawn the vehicle, all mods are applied and I'm using the same function.
The vehicles first are created OnGameModeInit(); because I have to load them from MySQL.
Reply
#4

Quote:
Originally Posted by HidroDF
Посмотреть сообщение
I think sscanf it's a simpliest way to save 14 columns in mysql.


Код:
if (a[s] > 0)
{
	AddVehicleComponent(Car[idx][VehicleID], a[s]);
	printf("Component: %i", a[s]);
}

// The printf results:
[14:03:54] Component: 1000
[14:03:54] Component: 1004
[14:03:54] Component: 1006
[14:03:54] Component: 1007
[14:03:54] Component: 1013
[14:03:54] Component: 1018
[14:03:54] Component: 1078
[14:03:54] Component: 1087
[14:03:54] Component: 1143

// As you can see, printf prints all the components to install, but the string contains zeros. When the for arrives to a 0, no component is added, logical because the slot is empty but then, the rest of the components installed are not applied.

/*

1000,  -> Load, printed and installed.
1004,  -> Load, printed and installed.
1006,  -> Load, printed and installed.
1007,  -> Load, printed and installed.
1013,  -> Load, printed and installed.
0,       -> Load, not printed and not installed because is 0 (empty).
1018,  -> Load, printed and NOT installed
1078,  -> Load, printed and NOT installed
0,       -> Load, not printed and not installed because is 0 (empty).
1087,  -> Load, printed and NOT installed
0,       -> Load, not printed and not installed because is 0 (empty).
0,       -> Load, not printed and not installed because is 0 (empty).
0,       -> Load, not printed and not installed because is 0 (empty).
1143   -> Load, printed and NOT installed

*/
If I respawn the vehicle, all mods are applied and I'm using the same function.
The vehicles first are created OnGameModeInit(); because I have to load them from MySQL.
I've understood how you want to do it, but I don't see any error in the code. Since the IDs are actually applied, I think it's not related to your code.

You could try saving the components in PAWN for the vehicle(s) (when creating the vehicle - get the IDs from MySQL and save them to an array - or do all this only in OnVehicleSpawn), then adding them only in OnVehicleSpawn. That adds a little delay between creating the vehicle and adding the components.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)