All pastes #134787 Raw Edit

Untitled

public text v1 · immutable
#134787 ·published 2006-08-16 15:07 UTC
rendered paste body
  if( strcmp(cmdtext, "/savepos", true) == 0 || strcmp(cmdtext, "/s", true) == 0 )
  {
	  if(IsPlayerAdmin(playerid))
	  {
//Step1: Make the string we are going to save
	   	new comment[256];
		comment = strtokspace(cmdtext, idx);  //strtokspace ignores spaces and just collects everything after the command
		new Float:pX,Float:pY,Float:pZ, Float:facing;
		//First we collect the data we want to save
		GetPlayerPos(playerid, pX,pY,pZ);
		GetPlayerFacingAngle(playerid, facing);

		//Then we format all the data into a string so we can write that string to a line.
		new output[256];
		format(output, sizeof(output), "{ %f , %f , %f, %f }, // %s \r\n" , pX, pY, pZ, facing, comment);
		//example line { 666.829406 , 926.550720 , -40.729133, 265.242553 }, // oilleakA
		
		

//Step2: Open the file and save the string to the end of the file, then close it.
		//IMPORTANT BIT START
		if (fexist("savedpositions.txt")) //check if the text file exists, this is incase you dont have a scriptfiles folder
		{
			new File: savefp = fopen("savedpositions.txt", io_append);  //open the file up to append to it. This puts the pointer at the end of the file so you write stuff below what is there at the moment.
			fwrite(savefp,output); //Writes the string we formatted earlier (example of the string: { 666.829406 , 926.550720 , -40.729133, 265.242553 }, // oilleakA
			fclose(savefp);
		} else {
			SendClientMessage(playerid, COLOR_WHITE, "Error 404: File not found");
			return 1;
  		}
		//IMPORTANT BIT END





//Step3: Displays to the player what was saved
		new outputmsg[256];
		format(outputmsg, sizeof(outputmsg), "Position %f , %f , %f, %f Saved // %s \n" , pX, pY, pZ, facing, comment);
		SendClientMessage(playerid, COLOR_WHITE, outputmsg);

	  } else {
		SendClientMessage(playerid, COLOR_WHITE, " You need to be an admin to to this!");
	  }
  return 1;
  }





//strtokspace - place somewhere on its own (very bottom of script is good place)
strtokspace(const string[], &index)
{
	new length = strlen(string);
	while ((index < length) && (string[index] <= ' '))
	{
		index++;
	}

	new offset = index;
	new result[256];
	while ((index < length) && ((index - offset) < (sizeof(result) - 1)))
	{
		result[index - offset] = string[index];
		index++;
	}
	result[index - offset] = EOS;
	return result;
}