Code Snippets

From SA-MP Wiki

Revision as of 22:40, 24 November 2008; view current revision
←Older revision | Newer revision→
Jump to: navigation, search

Contents

Numeric Snippets and Functions

Speed Distance Time Triangle

This triangle equation calculates the speed, distance or time of an entity.

Speed = Distance / Time
Distance = Time / Speed
Time = Distance / Speed

String-based Snippets and Functions

Strtok

Extracts a substring where a character or token is found.

Parameters

  • string: the string to split up.
  • index: a numeric position in where to search the string to start from.
  • seperator: the character or token to search for.


Source

// Written by DracoBlue.
stock strtok(const string[], &index,seperator=' ')
{
	new length = strlen(string);
	new offset = index;
	new result[MAX_STRING];
	while ((index < length) && (string[index] != seperator) && ((index - offset) < (sizeof(result) - 1)))
	{
		result[index - offset] = string[index];
		index++;
	}
 
	result[index - offset] = EOS;
	if ((index < length) && (string[index] == seperator))
	{
		index++;
	}
	return result;
}

Example

Here we will do a shouting command.

if(strcmp(cmdtext,"/shout",true)==0)
{
   new tmp[256];//The variable for strtok.
   new string[256];// A Variable where we store a text/number with information.
   new pName[MAX_PLAYER_NAME];//You can use this variable to get the players name later on.
   new idx;//Also known as Index,Part of strtok.
   tmp = strtok(cmdtext,idx);//This Is where we define tmp to equal strtok.
   if(!strlen(tmp)) return SendClientMessage(playerid,COLOR_WHITE,"USAGE:/me[action]");
   //this will see if the playerid filled all parameters,if not send him a message showing him how.
   //now continue with the command.
   if(IsPlayerConnected(playerid))//just a simple check to make it run smoother.
   {
      GetPlayerName(playerid,pName,sizeof(pName));//This Stores the playerid's name in a variable.
      format(string,sizeof(string),"%s Shouts: %s !!",pName,cmdtext[7]);
      /* In the format up,We used the stored text in the pName variable to show the player's name.
      cmdtext[7] means how many letters of the command we cut off to get the text of what he said.*/
      //and continue the command.
      SendClientMessageToAll(COLOR_WHITE,string)//now we send the formatted string.
   }// close the Connected Check.
   return 1;//make sure it returns something.
}//and close the ending bracket  
 
/* Written by iMatt */

Split

Works similar to strtok, except it splits up the entire string.

Parameters

  • strsrc: String to split up.
  • strdest: A 2D array; where to store the split up pieces.
  • delimiter: A character or token which identifies the beginning and end of substrings.


Source

// Author unknown. It was probably someone smart like [[User:DracoBlue|DracoBlue]] or [[User:Y_Less|Y_Less]].
stock split(const strsrc[], strdest[][], delimiter)
{
    new i, li;
    new aNum;
    new len;
    while(i <= strlen(strsrc))
    {
        if(strsrc[i] == delimiter || i == strlen(strsrc))
        {
            len = strmid(strdest[aNum], strsrc, li, i, 128);
            strdest[aNum][len] = 0;
            li = i+1;
            aNum++;
        }
        i++;
    }
    return 1;
}

Example

public OnFilterScriptInit()
{
    new tmp[2][128];
    split("Hello World!", tmp, ' ');
    print(tmp[0]);
}
OUTPUTS: Hello 

Miscellaneous

Halt

Halt freezes the script for a defined amount of time in seconds, identical to PHP's sleep function.

Parameters

  • seconds: How many seconds to freeze the script.


Source

// Written by Xalphox.
halt(seconds)
{
	new _newTime[4], _oldTime[4];
	gettime(_oldTime[0], _oldTime[1], _oldTime[2]);
	_oldTime[3] = _oldTime[2] + (_oldTime[1] * 60) + (_oldTime[0] * 600);
 
	while(_newTime[3] != (_oldTime[3] + seconds))
	{
		gettime(_newTime[0], _newTime[1], _newTime[2]);
		_newTime[3] = _newTime[2] + (_newTime[1] * 60) + (_newTime[0] * 600);
	}
}

Example

public OnFilterScriptInit()
{
	print("Sending you a hello message in ten seconds! =D");
	halt(10);
	print("HAI2U!");
}