Code explanation.
#1

Hello!

I've been trying to create a dynamic system lately but I struggle with saving/loading so I was reading through other people's code to see how it works, I came at a stop when I reached this part tho because I couldn't really understand how/why this code works.

Код:
LoadDealerships()
{
	new File:handle, count;
	new filename[64], line[256], s, key[64];
	for(new i=1; i < MAX_DEALERSHIPS; i++)
	{
		format(filename, sizeof(filename), DEALERSHIP_FILE_PATH "d%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++);
			if(strcmp(key, "Created") == 0) DealershipCreated[i] = strval(line[s]);
			else if(strcmp(key, "Pos") == 0) sscanf(line[s], "p<,>fff", DealershipPos[i][0],
			DealershipPos[i][1], DealershipPos[i][2]);
		}
		fclose(handle);
		if(DealershipCreated[i]) count++;
	}
	printf("  Loaded %d dealerships", count);
}
I know that it loads the dealerships, but I just can't understand this part:

Код:
format(filename, sizeof(filename), DEALERSHIP_FILE_PATH "d%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++);
			if(strcmp(key, "Created") == 0) DealershipCreated[i] = strval(line[s]);
			else if(strcmp(key, "Pos") == 0) sscanf(line[s], "p<,>fff", DealershipPos[i][0],
			DealershipPos[i][1], DealershipPos[i][2]);
		}
		fclose(handle);
		if(DealershipCreated[i]) count++;
	}
I believe that I'll need to know why is that here for me to be able to create something similar in the future.

Thanks!

CODE SOURCE: MadeMan AVS.
Reply
#2

It opens up the file, reads it line-by-line (that's that while loop), delimits it to get dealership coordinates (x/y/z), closes the file, counts the dealerships.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)