Gettok: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Undo revision 55324 by Pirulax (talk))
 
(20 intermediate revisions by 11 users not shown)
Line 1: Line 1:
__NOTOC__
__NOTOC__  
==Description==
{{Server client function}}
This function splits a string into sub-strings. You specify a character that will act as a separating character; this will determine where to split the sub-strings.  
This function splits a string using the given separating character and returns one specified substring.


For example, you could split up the string "This is a sentence", using spaces as separators. This would return "This","is","a","sentence". Each sub-string is assigned an ID number ascending from 1. In this example, ID 2 would return "is".
==Syntax==
<syntaxhighlight lang="lua">
string gettok ( string text, int tokenNumber, string / int separatingCharacter )
</syntaxhighlight>


This function is commonly used to check the first word against a list of known commands. If the command is found, all subsequent words will be used as arguments.  
===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|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.


==Syntax==
===Returns===
<syntaxhighlight lang="lua">string Gettok ( string, index, separatingchar )</syntaxhighlight>
Returns a [[string]] containing the token if it exists, ''false'' otherwise.


==Example==
==Example==  
<syntaxhighlight lang="lua">function onPlayerChat ( player, chat )
<section name="Server" class="server" show="true">
if (gettok (chat, 1, " ")) == "!createhydra")
This example retrieves the startskin and endskin for spawning a player from a string of two numbers "a,b"
  x, y, z = getPlayerPosition ( player )
<syntaxhighlight lang="lua">
  createVehicle ( 520, x + 5, y, z )
-- Put the string with the skins in a variable. Normally you would read it from a .map file or something.
  outputChatBox ( "You got a hydra", player )
local skins = "20,30"
end
-- Get the startskin and endskin strings ("20" and "30" in this case)
end</syntaxhighlight>
local startskin = gettok ( skins, 1, ',')
local endskin = gettok ( skins, 2, ',' )
-- Get a random skin in the range
local skin = math.random ( tonumber(startskin), tonumber(endskin) )
</syntaxhighlight>
</section>
 
==See Also==
{{Utility functions}}

Latest revision as of 12:48, 20 February 2019

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

Syntax

string gettok ( string text, int tokenNumber, string / 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, ',')
local endskin = gettok ( skins, 2, ',' )
-- Get a random skin in the range
local skin = math.random ( tonumber(startskin), tonumber(endskin) )

See Also