stock LoadStaticVehiclesFromFile(const filename[])
{
new File:file_ptr;
new line[256];
new var_from_line[64];
new vehicletype;
new Float:vSpawnX;
new Float:vSpawnY;
new Float:vSpawnZ;
new Float:SpawnRot;
new Color1, Color2;
new index;
new vehicles_loaded;
file_ptr = fopen(filename,filemode:io_read);
if(!file_ptr) return 0;
vehicles_loaded = 0;
while(fread(file_ptr,line,256) > 0)
{
index = 0;
// Read type
index = token_by_delim(line,var_from_line,',',index);
if(index == (-1)) continue;
vehicletype = strval(var_from_line);
if(vehicletype < 400 || vehicletype > 611) continue;
// Read X, Y, Z, Rotation
index = token_by_delim(line,var_from_line,',',index+1);
if(index == (-1)) continue;
vSpawnX = floatstr(var_from_line);
index = token_by_delim(line,var_from_line,',',index+1);
if(index == (-1)) continue;
vSpawnY = floatstr(var_from_line);
index = token_by_delim(line,var_from_line,',',index+1);
if(index == (-1)) continue;
vSpawnZ = floatstr(var_from_line);
index = token_by_delim(line,var_from_line,',',index+1);
if(index == (-1)) continue;
SpawnRot = floatstr(var_from_line);
// Read Color1, Color2
index = token_by_delim(line,var_from_line,',',index+1);
if(index == (-1)) continue;
Color1 = strval(var_from_line);
index = token_by_delim(line,var_from_line,';',index+1);
Color2 = strval(var_from_line);
AddStaticVehicleEx(vehicletype,vSpawnX,vSpawnY,vSpawnZ,SpawnRot,Color1,Color2,180); // respawn 3 minutes
vehicles_loaded++;
}
fclose(file_ptr);
printf("Loaded %d vehicles from: %s",vehicles_loaded,filename);
return vehicles_loaded;
}
stock token_by_delim(const string[], return_str[], delim, start_index)
{
new x=0;
while(string[start_index] != EOS && string[start_index] != delim) {
return_str[x] = string[start_index];
x++;
start_index++;
}
return_str[x] = EOS;
if(string[start_index] == EOS) start_index = (-1);
return start_index;
}
#include <loadvehicles>
//Possible Vehicle Loads
LoadStaticVehiclesFromFile("vehicles/bone.txt");
LoadStaticVehiclesFromFile("vehicles/flint.txt");
LoadStaticVehiclesFromFile("vehicles/ls_airport.txt");
LoadStaticVehiclesFromFile("vehicles/ls_gen_inner.txt");
LoadStaticVehiclesFromFile("vehicles/ls_gen_outer.txt");
LoadStaticVehiclesFromFile("vehicles/ls_law.txt");
LoadStaticVehiclesFromFile("vehicles/lv_airport.txt");
LoadStaticVehiclesFromFile("vehicles/lv_gen.txt");
LoadStaticVehiclesFromFile("vehicles/lv_law.txt");
LoadStaticVehiclesFromFile("vehicles/pilots.txt");
LoadStaticVehiclesFromFile("vehicles/red_county.txt");
LoadStaticVehiclesFromFile("vehicles/sf_airport.txt");
LoadStaticVehiclesFromFile("vehicles/sf_gen.txt");
LoadStaticVehiclesFromFile("vehicles/sf_law.txt");
LoadStaticVehiclesFromFile("vehicles/sf_train.txt");
LoadStaticVehiclesFromFile("vehicles/tierra.txt");
LoadStaticVehiclesFromFile("vehicles/trains.txt");
LoadStaticVehiclesFromFile("vehicles/trains_platform.txt");
LoadStaticVehiclesFromFile("vehicles/whetstone.txt");
It's not bad, but in the future, it would help to explain what some of the things do (like what's going on in the LoadStaticVehiclesFromFile function) to avoid more confusion.
|
LoadStaticVehiclesFromFile something used in the Grand Larceny game mode. The stock which I provided above makes LoadStaticVehiclesFromFile usable outside of Grand Larceny.
|
Such explanations should be in the tutorial, not somewhere hidden in the replies.
"[...] Replace this [...] Add this [...] Copy this [...]" it's just instruction after instruction without making the reasoning clear to the reader. This is a forum for developers and most developers would also like to know WHY you use the code that you use and what it exactly does. Of course, more experienced developers can derive that from reading the code, but I don't see any of them reading this kind of tutorials. I don't want to discourage you. Your will to contribute to the community is greatly appreciated! |
It's a function not a "stock".
Have a look here: https://sampforum.blast.hk/showthread.php?tid=570635 |
LoadStaticVehiclesFromFile(const filename[]) { static line[145], model, Float: x, Float: y, Float: z, Float: r, color1, color2, len, count = 0 ; new File: handle = fopen(filename, io_read); if (!handle) return 0; while ((len = fread(handle, line))) { line[len-2] = 0; print(line); if (sscanf(line, "P<(),>{s[17]}iffffii", model, x, y, z, r, color1, color2)) continue; if (!(399 < model < 612)) continue; AddStaticVehicleEx(model, x, y, z, r, color1, color2, 180); // Respawns in 3 minutes (180 seconds) count++; } fclose(handle); printf("Loaded %i vehicles from: %s", count, filename); return count; }