Gettok: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
__NOTOC__  
__NOTOC__  
{{Server client function}}
{{Server client function}}
This function splits a string using the given seperating character and returns one part as a string.
This function splits a string using the given separating character and returns one specified substring.


==Syntax==  
==Syntax==  
Line 9: Line 9:


===Required Arguments===  
===Required Arguments===  
*'''text:''' The string that should be split
*'''text:''' the string that should be split.
*'''tokenNumber:''' Which token should be returned (1 for the first, 2 for the second, ..)
*'''tokenNumber:''' which token should be returned (1 for the first, 2 for the second, and so on).
*'''separatingCharacter:''' The [[ASCII|ASCII number]] representing the character you want to use to separate the tokens
*'''separatingCharacter:''' the [[ASCII|ASCII number]] representing the character you want to use to separate the tokens. You can easily retrieve this by running string.byte on a string containing the separating character.


===Returns===
===Returns===
Returns the token if it exists, ''false'' otherwise.
Returns a [[string]] containing the token if it exists, ''false'' otherwise.


==Example==  
==Example==  
<section name="Server" class="server" show="true">
<section name="Server" class="server" show="true">
This example retrieves the startskin and endskin for spawning a player
This example retrieves the startskin and endskin for spawning a player from a string of two numbers "a,b"
<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.
-- Put the string with the skins in a variable. Normally you would read it from a .map file or something.
local skins = "20,30"
local skins = "20,30"
-- Get the startskin and endskin (20 and 30)
-- Get the startskin and endskin strings ("20" and "30" in this case)
local startskin = gettok ( skins, 1, 44 )
local startskin = gettok ( skins, 1, string.byte(',') )
local endskin = gettok ( skins, 2, 44 )
local endskin = gettok ( skins, 2, string.byte(',') )
-- Get a random skin of the range
-- Get a random skin in the range
local skin = randInt ( tonumber(startskin), tonumber(endskin) )
local skin = randInt ( tonumber(startskin), tonumber(endskin) )
</syntaxhighlight>
</syntaxhighlight>

Revision as of 12:16, 20 October 2007

This function splits a string using the given separating character and returns one specified substring.

Syntax

string gettok ( string text, int tokenNumber, int separatingCharacter )

Required Arguments

  • text: the string that should be split.
  • tokenNumber: which token should be returned (1 for the first, 2 for the second, and so on).
  • separatingCharacter: the ASCII number representing the character you want to use to separate the tokens. You can easily retrieve this by running string.byte on a string containing the separating character.

Returns

Returns a string containing the token if it exists, false otherwise.

Example

Click to collapse [-]
Server

This example retrieves the startskin and endskin for spawning a player from a string of two numbers "a,b"

-- 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 strings ("20" and "30" in this case)
local startskin = gettok ( skins, 1, string.byte(',') )
local endskin = gettok ( skins, 2, string.byte(',') )
-- Get a random skin in the range
local skin = randInt ( tonumber(startskin), tonumber(endskin) )

See Also