SA-MP Forums Archive
Multiple vehicles - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Multiple vehicles (/showthread.php?tid=421730)



Multiple vehicles - edzis84 - 10.03.2013

Hello its me again!

Is it possible to make ini file for all vehicles, like

Owner - QWERTY
Car model - 415
Car color - 100
Car locks - 5
Radio - 1

Owner - UYTSDASD
Car model - 416
Car color - 132
Car locks - 7
Radio - 0

So then i can make that one player can have more that one vehicle??

I made script that saves all info about cars in player file but its not good for me.

I know that i already made one topic asking for this but still no one helped me.

If this is not possible then please someone tell me how can i make that player can own like 10 cars.


Re: Multiple vehicles - Bakr - 10.03.2013

Yes, but since you are using files this would be extremely inefficient.

You could make another file with a player's name and have all vehicles they own within that file, that way it is separate from their other data.


Re: Multiple vehicles - Denying - 10.03.2013

Of course that this is possible, also save the vehicle's ID.


Re: Multiple vehicles - Denying - 10.03.2013

Quote:
Originally Posted by Bakr
Посмотреть сообщение
Yes, but since you are using files this would be extremely inefficient.

You could make another file with a player's name and have all vehicles they own within that file, that way it is separate from their other data.
What are you basically saying? I mean what I understand from you is: "It is possible but if you use files it would be hard, I've a suggestion, use files". Could you please explain it a bit more, or with different words?


Re: Multiple vehicles - Bakr - 10.03.2013

Quote:
Originally Posted by Denying
Посмотреть сообщение
What are you basically saying? I mean what I understand from you is: "It is possible but if you use files it would be hard, I've a suggestion, use files". Could you please explain it a bit more, or with different words?
Creating a separate file for each vehicle is going to be very inefficient. For example, if he were to load a player's vehicles upon connections, he would need to loop through each file and see if one belongs to the player. That requires opening each file, reading data from the file, checking that data, and closing the file every time.

My suggestion was to create another file with the player's name. That way, when the player connects, it searches for a file with that user's name, and if it exists, loads the vehicles he owns from that file. It prevents from looping through the files, opening, closing, and reading data from those files as well.


Re: Multiple vehicles - Denying - 10.03.2013

Quote:
Originally Posted by Bakr
Посмотреть сообщение
Creating a separate file for each vehicle is going to be very inefficient. For example, if he were to load a player's vehicles upon connections, he would need to loop through each file and see if one belongs to the player. That requires opening each file, reading data from the file, checking that data, and closing the file every time.

My suggestion was to create another file with the player's name. That way, when the player connects, it searches for a file with that user's name, and if it exists, loads the vehicles he owns from that file. It prevents from looping through the files, opening, closing, and reading data from those files as well.
I see, thank you for making this clear.


Re: Multiple vehicles - edzis84 - 10.03.2013

Maybe someone can show me code how to save and load them if all is in one file?

Because i have no ideas how to make it work.


Re: Multiple vehicles - Bakr - 10.03.2013

This is a rough outline I came up with. This is only an example to follow and shows the loading of the vehicles. You are responsible of creating the writing and saving of them, which shouldn't be very hard, it would be very similar to this. Note this is not tested - not even compiled.
pawn Код:
// OnPlayerConnect
enum E_VEHLOAD
{
    model,
    Float: x,
    Float: y,
    Float: z,
    Float: a
};
new File: hFile, file[MAX_PLAYER_NAME+11], name[MAX_PLAYER_NAME], buffer[128], key[30], bool: loading, tmpData[E_VEHLOAD];
GetPlayerName(playerid, name, MAX_PLAYER_NAME);
format(file, sizeof(file), "Vehs/%s.ini", name);
if(fexist(file))
{
    hFile = fopen(file, io_read);
    while(fread(hFile, buffer))
    {
        if(strfind(buffer, "[VehID_"))
        {
            // found a vehicle, check if there is already one loaded
            // if there is, create that vehicle and zero the memory in the tmpData array
            if(loading == true)
            {
                CreateVehicle(tmpData[model], tmpData[x], tmpData[y], tmpData[z], -1, -1);
                tmpData[model] = 0;
                // etc
            }
            // else, this is the first vehicle found, so set the loading value
            loading = true;
        }
        else
        {
            if(!loading) continue;

            key = iniGetKey(buffer);
            if(!strcmp(key, "VehModel", true))
                tmpData[model] = iniGetValue(buffer);
            else if(!strcmp(key, "VehX", true))
                tmpData[model] = iniGetValue(buffer);
            // etc
        }
    }
    fclose(hFile);
}
iniGetKey and iniGetValue functions can be found around the forums, search for them.

Note (assuming you follow my example closely) that "VehID_" is just the symbol to search for if there is a vehicle to load. At the end, you add number the vehicle is in the file. The example of how the file might help clear what I'm saying:
Код:
[VehID_0]
VehModel=400
VehX=4.00
VehY=49.99
VehZ=59.84
VehA=582.9
[VehID_1]
VehModel=400
VehX=4.00
VehY=49.99
VehZ=59.84
VehA=582.9
This could be done much easier using Y_INI, as it does support tags, but I'm unfamiliar with the system.