Code Snippets
From SA-MP Wiki
Contents |
[edit]
Numeric Snippets and Functions
[edit]
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
[edit]
String-based Snippets and Functions
| Note: The string-based functions that are listed here (strtok and split) have a far better alternative available: Sscanf. Use of these functions is generally discouraged. |
[edit]
Strtok
Extracts a substring where a character or token is found.
[edit]
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.
[edit]
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; }
[edit]
Example
Here we will do a shouting command.
if(strcmp(cmdtext,"/shout",true)==0) { new tmp[128], //The variable for strtok. string[128], // A Variable where we store a text/number with information. pName[MAX_PLAYER_NAME], //You can use this variable to get the players name later on. 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. GetPlayerName(playerid,pName,sizeof(pName));//This Stores the playerid's name in a variable. format(string,sizeof(string),"%s Shouts: %s !!",pName,tmp); /* In the format up,We used the stored text in the pName variable to show the player's name. Then we used the tmp variable to get the text that the player entered.*/ SendClientMessageToAll(COLOR_WHITE,string); //now we send the formatted string. return 1;//make sure it returns something. }//and close the ending bracket /* Written by iMatt */ /* Optimized by Vince */
[edit]
Split
Works similar to strtok, except it splits up the entire string.
[edit]
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.
[edit]
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; }
[edit]
Example
public OnFilterScriptInit() { new tmp[2][128]; split("Hello World!", tmp, ' '); print(tmp[0]); }
OUTPUTS: Hello
