[Include] The Guff - File related stuff [NEWBIE-FRIENDLY]
#1

The Guff
Newbie friendly file related functions

pawn Код:
stock LoadGObjectsFromFile(const filename[])
stock LoadGAddStaticVehiclesFromFile(const filename[])
stock LoadCreateVehiclesFromFile(const filename[])
What is it?
Well, I've seen a LOT of people asking how to read objects and vehicles from a file. This is exactly why I made this.
This requires SSCANF2 (The latest one) and the latest SAMP include. It's in BETA stage currently and it has only 3 functions. All credits to Jarnu and Lovro for the idea and helping me test it all, This is for you boys.

Code:


pawn Код:
#include <a_samp>
#include <sscanf2>

stock LoadGObjectsFromFile(const filename[])
{
    new File:file_ptr;
    new line[256];
    new Object;
    new Float:SpawnX;
    new Float:SpawnY;
    new Float:SpawnZ;
    new Float:SpawnRX;
    new Float:SpawnRY;
    new Float:SpawnRZ;
    new Float:draw;
    new Objects_loaded;

    file_ptr = fopen(filename,filemode:io_read);
    if(!file_ptr)
    {
        printf("[Guff] Couldn't load objects from %s [Reason : File doesn't exist]", filename);
    }
    else
    {
        Objects_loaded = 0;
        while(fread(file_ptr,line,256))
        {
            sscanf(line,"p<,>iffffffi",SpawnX, SpawnY, SpawnZ, SpawnRX, SpawnRY, SpawnRZ, draw);
            CreateObject(Object, SpawnX, SpawnY, SpawnZ, SpawnRX, SpawnRY, SpawnRZ, draw);
            Objects_loaded++;
        }
    }
    fclose(file_ptr);
    printf("[Guff] Loaded %d objects from: %s",Objects_loaded,filename);
    return Objects_loaded;
}


stock LoadGAddStaticVehiclesFromFile(const filename[])
{
    new File:file_ptr;
    new line[256];
    new modelid, Float:SpawnX, Float:SpawnY, Float:SpawnZ, Float:angle, color1, color2, Cars_Loaded;
    file_ptr = fopen(filename, filemode:io_read);
    if(!file_ptr)
    {
        printf("[Guff] Couldn't load permanent vehicles from %s [Reason : File doesn't exist]", filename);
    }
    else
    {
        Cars_Loaded = 0;
        while(fread(file_ptr, line, 256))
        {
            sscanf(line, "p<,>iiffffff", SpawnX, SpawnY, SpawnZ, angle, color1, color2);
            AddStaticVehicle(modelid, SpawnX, SpawnY, SpawnZ, angle, color1, color2);
            Cars_Loaded++;
        }
    }
    fclose(file_ptr);
    printf("[Guff] Loaded %d vehicles from: %s",Cars_Loaded,filename);
    return Cars_Loaded;
}

stock LoadCreateVehiclesFromFile(const filename[])
{
    new File:file_ptr;
    new line[256];
    new modelid, Float:SpawnX, Float:SpawnY, Float:SpawnZ, Float:angle, color1, color2, respawn, Cars_Loaded;
    file_ptr = fopen(filename, filemode:io_read);
    if(!file_ptr)
    {
        printf("[Guff] Couldn't load temporary vehicles from %s [Reason : File doesn't exist]", filename);
    }
    else
    {
        Cars_Loaded = 0;
        while(fread(file_ptr, line, 256))
        {
            sscanf(line, "p<,>iifffffff", SpawnX, SpawnY, SpawnZ, angle, color1, color2, respawn);
            CreateVehicle(modelid, SpawnX, SpawnY, SpawnZ, angle, color1, color2, respawn);
            Cars_Loaded++;
        }
    }
    fclose(file_ptr);
    printf("[Guff] Loaded %d vehicles from: %s",Cars_Loaded,filename);
    return Cars_Loaded;
}
Download : http://www.mediafire.com/download.php?5b99zaf8xbdbxm1

Test script :


pawn Код:
#include <a_samp>
#include <Guff>
#include <sscanf2>

main()
{
    print("\n----------------------------------");
    print(" Testing Log creator");
    print("----------------------------------\n");
}
public OnGameModeInit()
{
    // Don't use these lines if it's a filterscript
    SetGameModeText("Blank Script");
    AddPlayerClass(0, 1958.3783, 1343.1572, 15.3746, 269.1425, 0, 0, 0, 0, 0, 0);
    LoadGObjectsFromFile("Objects.txt");
    LoadGAddStaticVehiclesFromFile("Vehicles.txt");
    return 1;
}

Simple, But I hope it helps
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)