23.07.2016, 12:14
Doing it like that might be very slow. Keep in mind that for every other file, you have to open, read and close it. And that MAX_OBJECTS times. Though, it's definitely going to be faster to delete them and check if they exist.
Here's an example:
The example code allows you to keep numbers filled. Example:
> There are three files; 0.ini, 1.ini and 2.ini respectively.
> You remove 1.ini
> You create a new object
> It will save as 1.ini and not as 3.ini
Doing it otherwise may give you trouble when creating objects with the same ID.
Here's an example:
PHP код:
CMD:createobject(playerid, params[]) {
new
objectid;
if(sscanf(params, "d", objectid)) {
return SendClientMessage(playerid, -1, "USAGE: /createobject <id>");
}
new
obj = createObj(objectid);
if(obj == -1) {
// createObj() has returned -1
SendClientMessage(playerid, -1, "ERROR: The server has reached its maximum amount of objects.");
}
return true;
}
createObj(objectid) {
new
tempPath[24];
for(new i = 0; i < MAX_OBJECTS; i ++) {
format(tempPath, sizeof(tempPath), "\\objects\\%d.ini", i);
if(fexist(tempPath)) {
// Object with ID the value of the current 'i' already exists
continue;
}
new INI:file = INI_Open(tempPath);
INI_SetTag(file, "objects");
INI_WriteInt(file, "Object ID", objectid);
INI_Close(file);
return i;
}
return -1;
}
> There are three files; 0.ini, 1.ini and 2.ini respectively.
> You remove 1.ini
> You create a new object
> It will save as 1.ini and not as 3.ini
Doing it otherwise may give you trouble when creating objects with the same ID.