Y_INI Saving/loading help -
GeorgeMcReary - 22.07.2016
Hello i am creating a Objects saving system for my server. i was using y_ini. i am saving all objects using one simple call back, SaveObjects()
Now when i thought what to write there, i used it like:
PHP код:
INI_WriteInt(object, "Object ID", ObjInfo[MAX_OBJECTS][OID]);
Am i doing right? i have to use MAX_OBJECTS there or do i have to use loop too in Save Callback?
Its a Small help but Thanks in advance
Re: Y_INI Saving/loading help -
Deadpoop - 22.07.2016
PHP код:
PHP Code:
INI_WriteInt(object, "Object ID", objectid);
Re: Y_INI Saving/loading help -
GeorgeMcReary - 22.07.2016
i didnt get it, like ofc there will not be a single .ini file. so how do i put objectid to which i am storing under ObjInfo[MAX_OBJECTS][OID]. i want to set that [OID] would get saved.
Also how can i make it like in folder Trees, the .ini file get saved with the name of tree ID like "1.ini" or "20.ini", and if i do that, how do i load the all files togather?
Thanks for Fast Reply
Re: Y_INI Saving/loading help -
AndySedeyn - 22.07.2016
The 2D-array ObjInfo will only save whatever value [0ID] is holding for index MAX_OBJECTS.
Let's say MAX_OBJECTS has been defined as 5:
PHP код:
#define MAX_OBJECTS 5
We then assign a value to ObjInfo[MAX_OBJECTS][0ID]:
PHP код:
ObjInfo[MAX_OBJECTS][0ID] = 10;
What the preprocessor will do is replace MAX_OBJECTS with its defined value:
PHP код:
ObjInfo[5][0ID] = 10;
This means that for the index MAX_OBJECTS (5) of the 2D-array ObjInfo, 0ID equals 10.
What you want to do is save the values of 0ID for all indexes of ObjInfo. In that case, you need a loop:
PHP код:
for(new i = 0; i < MAX_OBJECTS; i++) {
INI_WriteInt(object, "Object ID", ObjInfo[i][OID])
}
Fault-checking should be done by you. With fault-checking, I immediately think of non-existing objects or empty indexes.
Re: Y_INI Saving/loading help -
GeorgeMcReary - 23.07.2016
Yeah right. that was i was asking. i have to use loop in order to save it. now i want that each .ini gets saved inside Objects/1.ini or 2.ini etc.. according to objects ID. how can i make it like that. also how will i be able to load it?
Thanks for help.
Re: Y_INI Saving/loading help -
AndySedeyn - 23.07.2016
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:
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;
}
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.