Gettok: Difference between revisions
Jump to navigation
Jump to search
mNo edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
__NOTOC__ | __NOTOC__ | ||
<!-- Describe in plain english what this function does. Don't go into details, just give an overview --> | <!-- Describe in plain english what this function does. Don't go into details, just give an overview --> | ||
This | This function splits a string using the given seperating character and returns one part as a string. | ||
==Syntax== | ==Syntax== | ||
<!-- NOTE: don't use 'special' names for variable names, e.g. you shouldn't be writing things like 'player player, vehicle vehicle', instead write something like 'player thePlayer, vehicle vehicleToGetInto'. This is less confusing and prevents the syntax highlighting being odd --> | <!-- NOTE: don't use 'special' names for variable names, e.g. you shouldn't be writing things like 'player player, vehicle vehicle', instead write something like 'player thePlayer, vehicle vehicleToGetInto'. This is less confusing and prevents the syntax highlighting being odd --> | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
string gettok ( string text, int tokennumber, int | string gettok ( string text, int tokennumber, int seperatingCharacter ) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
===Required Arguments=== | ===Required Arguments=== | ||
<!-- List each argument one per line. This should be the argument's name as in the argument list above, NOT the argument's data type --> | <!-- List each argument one per line. This should be the argument's name as in the argument list above, NOT the argument's data type --> | ||
*''' | *'''text:''' The string that should be split | ||
*'''tokennumber:''' Which token should be returned (1 for the first, 2 for the second, ..) | |||
*'''seperatingCharacter:''' The ASCII number of the character that seperates the strings (for example 44 for ',') | |||
*''' | |||
*''' | |||
===Returns=== | ===Returns=== | ||
<!-- Make this descriptive. Explain what cases will return false. If you're unsure, add a tag to it so we can check --> | <!-- Make this descriptive. Explain what cases will return false. If you're unsure, add a tag to it so we can check --> | ||
Returns '' | Returns ''string'' with the token if it exists, ''false'' otherwise. | ||
==Example== | ==Example== | ||
<!-- Explain what the example is in a single sentance --> | <!-- Explain what the example is in a single sentance --> | ||
This example | This example retrieves the startskin and endskin for spawning a player | ||
<!-- Add the code below, an emphasis should be on making it clear, not optimized. You could provide two versions if you wish, one clear and well commented, the other optimized --> | <!-- Add the code below, an emphasis should be on making it clear, not optimized. You could provide two versions if you wish, one clear and well commented, the other optimized --> | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
-- | -- Put the string with the skins in a variable. Normally you would read it from a .map file or something. | ||
local skins = "20,30" | |||
-- | -- Get the startskin and endskin (20 and 30) | ||
local startskin = gettok ( skins, 1, 44 ) | |||
local endskin = gettok ( skins, 2, 44 ) | |||
-- Get a random skin of the range | |||
local skin = randInt(tonumber(startskin),tonumber(endskin)) | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Revision as of 14:12, 4 July 2007
This function splits a string using the given seperating character and returns one part as a string.
Syntax
string gettok ( string text, int tokennumber, int seperatingCharacter )
Required Arguments
- text: The string that should be split
- tokennumber: Which token should be returned (1 for the first, 2 for the second, ..)
- seperatingCharacter: The ASCII number of the character that seperates the strings (for example 44 for ',')
Returns
Returns string with the token if it exists, false otherwise.
Example
This example retrieves the startskin and endskin for spawning a player
-- Put the string with the skins in a variable. Normally you would read it from a .map file or something. local skins = "20,30" -- Get the startskin and endskin (20 and 30) local startskin = gettok ( skins, 1, 44 ) local endskin = gettok ( skins, 2, 44 ) -- Get a random skin of the range local skin = randInt(tonumber(startskin),tonumber(endskin))