Snippet: GetTok in lua

Share your gamemodes and snippets here
Post Reply
rww
Posts: 15
Joined: Fri Jun 15, 2018 6:49 am

Snippet: GetTok in lua

Post by rww » Wed Feb 27, 2019 4:13 pm

GetTok is a function for spilts a string. You can get one word from sentence, by using separating character. This is very usefull function, for example when you do a command with more than one value.

Code (not my, but I fixed small bug):

Code: Select all

function Tokenize( sChar, sInput )
    local tReturn = {}
    for sWord in string.gmatch( sInput, "[^"..sChar.."]+" ) do
        table.insert( tReturn, tonumber(sWord) or sWord )
    end
    return tReturn
end

function GetTok( sInput, iPosition, Separator, iRange )
    local Separator = string.format( '%c', Separator )
    local tTokens = Tokenize( Separator, sInput )
    if iPosition == 0 then
        return sInput
    end
    local iStart, iStop = ( iPosition > 0 ) and iPosition or ( #tTokens + iPosition + 1 )
    if not iRange or iPosition == iRange then
        return tTokens[ iStart ]
    end
    if iRange > 0 then
        iStop = iRange
    elseif iRange == 0 or ( iStart + iRange ) > #tTokens then
        iStop = #tTokens
    else
        iStop = iStart + iRange + 1
    end
    if iStart == iStop then
        return tTokens[ iStart ]
    end
    if iStart > iStop then
        iStart, iStop = iStop, iStart
    end
	if (iStart == iPosition) then
		return table.concat( tTokens, Separator, iStart, iStop )
	end
end

function NumTok(text, C)
    local count, tokens = 0, Tokenize(C, text)
    for token in tokens:gmatch("%Z+") do count = count+1 end
    return count
end
Source: https://codereview.stackexchange.com/qu ... ion-in-lua

ID's for 'Separator' here (ID 32 = space): http://www.asciitable.com

How it works:

Code: Select all

local info = GetTok("/elo test gettok",0,32)
local info2 = GetTok("/elo test gettok",1,32)
local info3 = GetTok("/elo test gettok",2,32)
local info4 = GetTok("/elo test gettok",3,32)

print(" ")
print(info)
print(info2)
print(info3)
print(info4)
print(" ")
Console:

Code: Select all

27/02/2019 17:08:49.567 /elo test gettok
27/02/2019 17:08:49.567 /elo
27/02/2019 17:08:49.567 test
27/02/2019 17:08:49.567 gettok
Example in script:

Code: Select all

function onPlayerCMD(playerid, text)

	local vehid = isPlayerInAnyVehicle(playerid)
	local cmd = GetTok(text,1,32,1)

	if(cmd == "/cmd" or cmd == "/cmds" or cmd == "/komendy") then
--[[
	old first cmd line (for compare)
	if(string.sub(text, 1, 4) == "/cmd" or string.sub(text, 1, 5) == "/cmds" or string.sub(text, 1, 8) == "/komendy") then
]]--
		sendPlayerMsg(playerid,"* List of commands IV-DM:",0xFFFFFFFF)
		sendPlayerMsg(playerid,"* /heal, /kill, /skin, /wep, /telpos, /teles",0xFF00FF80)
		sendPlayerMsg(playerid,"* /fix, /flip, /randcol, /randcol2, /randcol3, /randcol4, /v",0xFF00FF80)
	end
end
registerEvent("onPlayerCMD", "onPlayerCommand")
Image

rww
Posts: 15
Joined: Fri Jun 15, 2018 6:49 am

Re: Snippet: GetTok in lua

Post by rww » Sun Apr 14, 2019 4:21 pm

Script updated, I fixed small bug:

Code: Select all

API Error: Event onPlayerCMD: ...functions/gettok.lua:37: invalid value (nil) at index 2 in table for 'concat'
Image

Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests