Gettok: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
(Undo revision 55324 by Pirulax (talk))
 
(14 intermediate revisions by 9 users not shown)
Line 1: Line 1:
__NOTOC__  
__NOTOC__  
<!-- Describe in plain english what this function does. Don't go into details, just give an overview -->
{{Server client function}}
This fake function is for use with blah & blah and does blahblahblabhalbhl
This function splits a string using the given separating character and returns one specified substring.


==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 -->
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
returnType gettok ( string text, int tokennumber, int seperating character )
string gettok ( string text, int tokenNumber, string / int separatingCharacter )
</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 -->
*'''text:''' the string that should be split.
*'''argumentName:''' description
*'''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.
<!-- Only include this section below if there are optional arguments -->
===Optional Arguments===
{{OptionalArg}}
*'''argumentName2:''' description
*'''argumentName3:''' description


===Returns===
===Returns===
<!-- Make this descriptive. Explain what cases will return false. If you're unsure, add a tag to it so we can check -->
Returns a [[string]] containing the token if it exists, ''false'' otherwise.
Returns ''true'' if blah, ''false'' otherwise.


==Example==  
==Example==  
<!-- Explain what the example is in a single sentance -->
<section name="Server" class="server" show="true">
This example does...
This example retrieves the startskin and endskin for spawning a player from a string of two numbers "a,b"
<!-- 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">
--This line does...
-- Put the string with the skins in a variable. Normally you would read it from a .map file or something.
blabhalbalhb --abababa
local skins = "20,30"
--This line does this...
-- Get the startskin and endskin strings ("20" and "30" in this case)
mooo
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>
</syntaxhighlight>
</section>


==See Also==
==See Also==
<!-- Change FunctionArea to the area that this function is in on the main function list page, e.g. Server, Player, Vehicle etc -->
{{Utility functions}}
{{FunctionArea_functions}}
[[Category:Incomplete]]

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