Loading strings issue.
#1

Hello, so I am creating a faction system where the faction rank-names are dynamic, you set their strings in-game and they save in the faction's file. The saving works fine and the string gets written in the file properly, the problem is loading it as currently the code I used to load it is not working.

Here's the array I have to save the rank names.

Код:
new FactionARanks[MAX_FACTIONS][13][32];
Here's how I save the ranks..

Код:
SaveFaction(factionid)
{
	new filename[64], line[256];
	format(filename, sizeof(filename), FACTION_FILE_PATH "f%d.ini", factionid);
	new File:handle = fopen(filename, io_write);
        for(new j = 0; j < 13; j++)
	{
		format(line, sizeof(line), "FactionSkins[%d]=%d\r\n",  j+1 ,FacInfo[factionid][FactionSkins][j]);     fwrite(handle, line);
  		format(line, sizeof(line), "FactionARanks[%d]=%s\r\n", j+1 ,FactionARanks[factionid][j]); fwrite(handle, line);
	}
	fclose(handle);
}
I have other stuff and everything saves fine, but here's how I load it and that's where the problem occurs.

Код:
LoadFactions()
{
	new File:handle, count;
	new filename[64], line[256], s, key[64];
	for(new i=0; i < MAX_FACTIONS; i++)
	{
		format(filename, sizeof(filename), FACTION_FILE_PATH "f%d.ini", i);
		if(!fexist(filename)) continue;
		handle = fopen(filename, io_read);
		while(fread(handle, line))
		{
			StripNL(line);
			s = strfind(line, "=");
			if(!line[0] || s < 1) continue;
			strmid(key, line, 0, s++);
			for(new j = 0; j < 13; j++)
			{
 				if(strcmp(key, "FactionSkins") == 0) FacInfo[i][FactionSkins][j] = strval(line[s]);
 				else if(strcmp(key, "FactionARanks") == 0) sscanf(line[s], "s[32]", FactionARanks[i][j]);
			}
		}
		fclose(handle);
		if(FacIDTaken[i]) count++;
	}
	printf("  Loaded %d factions", count);
}
I have other variables that load aswell under that function and they load fine, those strings are the problem tho.

Help would be greatly appreciated.

PS: Just to clarify, the strings are saved properly and they are written in their files, the problem is with LOADING them into the game after restarting. Other variables under the same loading/saving functions work fine.
Reply
#2

pawn Код:
StripNL(line);
What does this function do for loading purpose ?
Reply
#3

Quote:
Originally Posted by Gammix
Посмотреть сообщение
pawn Код:
StripNL(line);
What does this function do for loading purpose ?
Its the same as PHP rtrim, it returns a string with whitespace stripped from the end of str.

That's not the problem here tho, it might be something to do with the loop.
Reply
#4

Still need help with this guys.
Reply
#5

Still need help, seriously tho I need to find the issue.

Код:
			for(new j = 0; j < 13; j++)
			{
 				if(strcmp(key, "FactionSkins") == 0) FacInfo[i][FactionSkins][j] = strval(line[s]);
 				else if(strcmp(key, "FactionARanks") == 0) sscanf(line[s], "s[32]", FactionARanks[i][j]);
I am pretty sure its this
Код:
else if(strcmp(key, "FactionARanks") == 0) sscanf(line[s], "s[32]", FactionARanks[i][j]);
but I am not sure how is it supposed to be written.
Reply
#6

There's a lot of unnecessary things you are doing.

Here's how I would do it (loading part):
pawn Код:
// ** INCLUDES

#include <a_samp>

// ** MAIN

main()
{
    print("Loaded \"get_values_in_file.amx\".");

    new File:file, line[128], length_whitespace, length_equal, var[3], count;
    file = fopen("get_values_in_file.ini", io_read);

    while(fread(file, line))
    {
        length_whitespace = strfind(line, "\r\n", false);

        if(length_whitespace != -1)
        {
            strdel(line, length_whitespace, (length_whitespace + 4));
        }

        length_equal = strfind(line, "=", false);

        if(length_equal != -1)
        {
            strdel(line, 0, (length_equal + 2));
        }

        var[count] = strval(line);

        printf("var[0] = %d", var[count]);

        count ++;
    }

    fclose(file);
}

// ** CALLBACKS

public OnGameModeInit()
{
    return 1;
}

public OnGameModeExit()
{
    return 1;
}
get_values_in_file.ini
pawn Код:
var[0] = 10
var[1] = 20
var[2] = 50
Output
pawn Код:
var[0] = 10
var[0] = 20
var[0] = 50
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)