Finding Next Available Number -
nmader - 25.03.2013
Hello again guys,
I am working on a dynamic vehicle saving script and have come across somewhat of a situation, In order for the vehicles (and their data) not to get mixed up with another's I need to give it a car number within the .ini file. However, in order to prevent over-lapping data I need to get the next available (not taken by another vehicle's file) number or ID for the vehicle.
I cannot seem to find a function for this so any help will be greatly appreciated!
Thanks,
Nmader
Re: Finding Next Available Number -
Scenario - 25.03.2013
Something like this, maybe?
pawn Код:
for(new i = 0; i < MAX_SAVED_VEHICLES; i++)
{
format(string, sizeof(string), "%d.INI", i);
if(!fexist(string)) return i;
}
EDIT: Made a few efficiency adjustments.
Re: Finding Next Available Number -
nmader - 25.03.2013
I shall test it within the script, looking at the code you provided it does seem to make a little sense to me. Your time was also very efficient, thanks!
Re: Finding Next Available Number -
Scenario - 25.03.2013
You're most welcome!
Basically, the code will loop through the maximum amount of vehicles that the server will save (which can be defined by "MAX_SAVED_VEHICLES"). It formats a quick string so it can take each ID and make a string which you can use to check if the file exists. If the file exists [with the specified ID], it will continue with the loop. If the file [with the specified ID] doesn't exist, it will stop the loop and return the ID that's open.
Re: Finding Next Available Number -
nmader - 25.03.2013
Quote:
Originally Posted by RealCop228
You're most welcome!
Basically, the code will loop through the maximum amount of vehicles that the server will save (which can be defined by "MAX_SAVED_VEHICLES"). It formats a quick string so it can take each ID and make a string which you can use to check if the file exists. If the file exists [with the specified ID], it will continue with the loop. If the file [with the specified ID] doesn't exist, it will stop the loop and return the ID that's open.
|
Thanks for the fantastic explanation, looking at the code once more it seems to make a lot of sense, a lot more than I would have guessed!
Re: Finding Next Available Number -
park4bmx - 25.03.2013
the code is good but there are function that are not needed, which simply means more time to process because of the checks.
pawn Код:
stock GetFreeID()
{
new file[12];
for(new ID = 0; ID < MAX_SAVED_VEHICLES; ID++)
{
format(file, file(string), "%d.INI", ID);
if(!fexist(file)) return ID;
}
}
Re: Finding Next Available Number -
Scenario - 25.03.2013
Quote:
Originally Posted by park4bmx
the code is good but there are function that are not needed, which simply means more time to process because of the checks.
pawn Код:
stock GetFreeID() { new file[12]; for(new ID = 0; ID < MAX_SAVED_VEHICLES; ID++) { format(file, file(string), "%d.INI", ID); if(!fexist(file)) return ID; } }
|
I thought I edited the post, but I must not of hit the submit button before going to my other tab. :P