Finding Next Available Number
#1

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
Reply
#2

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.
Reply
#3

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!
Reply
#4

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.
Reply
#5

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!
Reply
#6

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;
      }
}
Reply
#7

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
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)