Saving car mods
From SA-MP Wiki
OnVehicleMod
Params: vehicleid - The vehicle id of the vehicle that just been moded. componentid - The component id (used for AddVehicleComponent(vehicleid, componentid) )
Info:
There is no need to add anything like 'AddVehicleComponent(vehicleid, componentid)' under this line. Also, if you would like to save vehicle components... Here's the guide how to save vehicle mods: Firstly, create new values for vehicle mods 1-17.
enum vInfo { mod[17], owner }; new VehicleInfo[200][vInfo]; #define CARMODTYPE_FRONT_BULLBAR 14 #define CARMODTYPE_REAR_BULLBAR 15 #define CARMODTYPE_SIDESKIRT_RIGHT 3 #define CARMODTYPE_SIDESKIRT_LEFT 16
And also the vehicle components divided into groups:
new spoiler[20][0] = { {1000}, {1001}, {1002}, {1003}, {1014}, {1015}, {1016}, {1023}, {1058}, {1060}, {1049}, {1050}, {1138}, {1139}, {1146}, {1147}, {1158}, {1162}, {1163}, {1164} }; new nitro[3][0] = { {1008}, {1009}, {1010} }; new fbumper[23][0] = { {1117}, {1152}, {1153}, {1155}, {1157}, {1160}, {1165}, {1166}, {1169}, {1170}, {1171}, {1172}, {1173}, {1174}, {1175}, {1179}, {1181}, {1182}, {1185}, {1188}, {1189}, {1192}, {1193} }; new rbumper[22][0] = { {1140}, {1141}, {1148}, {1149}, {1150}, {1151}, {1154}, {1156}, {1159}, {1161}, {1167}, {1168}, {1176}, {1177}, {1178}, {1180}, {1183}, {1184}, {1186}, {1187}, {1190}, {1191} }; new exhaust[28][0] = { {1018}, {1019}, {1020}, {1021}, {1022}, {1028}, {1029}, {1037}, {1043}, {1044}, {1045}, {1046}, {1059}, {1064}, {1065}, {1066}, {1089}, {1092}, {1104}, {1105}, {1113}, {1114}, {1126}, {1127}, {1129}, {1132}, {1135}, {1136} }; new bventr[2][0] = { {1142}, {1144} }; new bventl[2][0] = { {1143}, {1145} }; new bscoop[4][0] = { {1004}, {1005}, {1011}, {1012} }; new rscoop[17][0] = { {1006}, {1032}, {1033}, {1035}, {1038}, {1053}, {1054}, {1055}, {1061}, {1067}, {1068}, {1088}, {1091}, {1103}, {1128}, {1130}, {1131} }; new lskirt[21][0] = { {1007}, {1026}, {1031}, {1036}, {1039}, {1042}, {1047}, {1048}, {1056}, {1057}, {1069}, {1070}, {1090}, {1093}, {1106}, {1108}, {1118}, {1119}, {1133}, {1122}, {1134} }; new rskirt[21][0] = { {1017}, {1027}, {1030}, {1040}, {1041}, {1051}, {1052}, {1062}, {1063}, {1071}, {1072}, {1094}, {1095}, {1099}, {1101}, {1102}, {1107}, {1120}, {1121}, {1124}, {1137} }; new hydraulics[1][0] = { {1087} }; new base[1][0] = { {1086} }; new rbbars[4][0] = { {1109}, {1110}, {1123}, {1125} }; new fbbars[2][0] = { {1115}, {1116} }; new wheels[17][0] = { {1025}, {1073}, {1074}, {1075}, {1076}, {1077}, {1078}, {1079}, {1080}, {1081}, {1082}, {1083}, {1084}, {1085}, {1096}, {1097}, {1098} }; new lights[2][0] = { {1013}, {1024} };
Create a public callback "SaveComponent" and don't forget to forward it (forward SaveComponent(vehicleid, componentid);) on top of your script.
public SaveComponent(vehicleid, componentid) { new playerid = GetDriverID(vehicleid); if(GetPlayerState(playerid) == PLAYER_STATE_DRIVER) { if (strcmp(VehicleInfo[vehicleid][owner], PlInfo[playerid][name], false) == 0) { for(new s=0; s<20; s++) { if(componentid == spoiler[s][0]) { VehicleInfo[vehicleid][mod][0] = componentid; } } for(new s=0; s<4; s++) { if(componentid == bscoop[s][0]) { VehicleInfo[vehicleid][mod][1] = componentid; } } for(new s=0; s<17; s++) { if(componentid == rscoop[s][0]) { VehicleInfo[vehicleid][mod][2] = componentid; } } for(new s=0; s<21; s++) { if(componentid == rskirt[s][0]) { VehicleInfo[vehicleid][mod][3] = componentid; } } for(new s=0; s<21; s++) { if(componentid == lskirt[s][0]) { VehicleInfo[vehicleid][mod][16] = componentid; } } for(new s=0; s<2; s++) { if(componentid == lights[s][0]) { VehicleInfo[vehicleid][mod][4] = componentid; } } for(new s=0; s<3; s++) { if(componentid == nitro[s][0]) { VehicleInfo[vehicleid][mod][5] = componentid; } } for(new s=0; s<28; s++) { if(componentid == exhaust[s][0]) { VehicleInfo[vehicleid][mod][6] = componentid; } } for(new s=0; s<17; s++) { if(componentid == wheels[s][0]) { VehicleInfo[vehicleid][mod][7] = componentid; } } for(new s=0; s<1; s++) { if(componentid == base[s][0]) { VehicleInfo[vehicleid][mod][8] = componentid; } } for(new s=0; s<1; s++) { if(componentid == hydraulics[s][0]) { VehicleInfo[vehicleid][mod][9] = componentid; } } for(new s=0; s<23; s++) { if(componentid == fbumper[s][0]) { VehicleInfo[vehicleid][mod][10] = componentid; } } for(new s=0; s<22; s++) { if(componentid == rbumper[s][0]) { VehicleInfo[vehicleid][mod][11] = componentid; } } for(new s=0; s<2; s++) { if(componentid == bventr[s][0]) { VehicleInfo[vehicleid][mod][12] = componentid; } } for(new s=0; s<2; s++) { if(componentid == bventl[s][0]) { VehicleInfo[vehicleid][mod][13] = componentid; } } for(new s=0; s<2; s++) { if(componentid == fbbars[s][0]) { VehicleInfo[vehicleid][mod][15] = componentid; } } for(new s=0; s<4; s++) { if(componentid == rbbars[s][0]) { VehicleInfo[vehicleid][mod][14] = componentid; } } return 1; } } return 0; }
Also don't forget to tell your script when to SaveComponent:
public OnVehicleMod(vehicleid, componentid) { SaveComponent(vehicleid, componentid); return 1; }
If you want your vehicle to spawn with the saved mods:
public OnVehicleSpawn(vehicleid) { ModVehicle(vehicleid); return 1; }
Don't forget to add the function "ModVehicle".
stock ModVehicle(vehicleid) { if(vehicleid <= 0 || vehicleid >= MAX_VEHICLES) return; for(new i = 0; i < 17; ++i) { if(VehicleInfo[vehicleid][mod][i] != 0) { AddVehicleComponent(vehicleid, VehicleInfo[vehicleid][mod][i]); } } }
All you need now is the GetDriverID(vehicleid) function. Here it is:
stock GetDriverID(vehicleid) { for(new i = 0; i < MAX_PLAYERS; i++) { if(GetPlayerVehicleID(i) == vehicleid && GetPlayerState(i) == PLAYER_STATE_DRIVER) return i; } return -1; }
Don't forget to add forward for each and every callback we entered.
OLD: Wiki info by Ignas1337 out of work of tAxI.
Improved by Kar - This code has been altered for better viewing and fixed Uranus bumper mix up.