Hello, I made this function for a friend when he was making a talkaction.
This will ease your job as talkaction scripter.
Place this in your global.lua
And then, to use it you do:
x[1] will be "da", x[2] will be "lol" and x[3] will be "ha".
A way to check if they have typed in all the required data(in the following format, for example):
"name, posX posY posZ"
That script has several restrictions and does not allow you to do wrong. I can't say it's 100% bug free, but I do think it is!
Enjoy this function, and feel free to use the script (you could strip off the comments if you wish)
Regards,
Colandus
This will ease your job as talkaction scripter.
Place this in your global.lua
PHP:
function string.trim(str)
-- Function by Colandus
return (str:gsub("^%s*(.-)%s*$", "%1"))
end
function string.explode(str, sep, limit)
-- Function by Colandus
if limit and type(limit) ~= 'number' then
error("string.explode: limit must be a number", 2)
end
if #sep == 0 or #str == 0 then return end
local pos, i, t = 1, 1, {}
for s, e in function() return str:find(sep, pos) end do
table.insert(t, str:sub(pos, s-1):trim())
pos = e + 1
i = i + 1
if limit and i == limit then break end
end
table.insert(t, str:sub(pos):trim())
return t
end
And then, to use it you do:
PHP:
x=string.explode("da, lol, ha", ",")
x[1] will be "da", x[2] will be "lol" and x[3] will be "ha".
A way to check if they have typed in all the required data(in the following format, for example):
"name, posX posY posZ"
PHP:
function tableToPos(t)
-- Function by Colandus
if type(t) == "table" and #t == 3 and tonumber(table.concat(t)) then
return {x=tonumber(t[1]), y=tonumber(t[2]), z=tonumber(t[3])}
end
return FALSE
end
function positionExists(pos)
-- Function by Pedro B. and Colandus
return getTileThingByPos(pos) > -1
end
function warnPlayer(cid, msg)
-- Function by Colandus
doSendMagicEffect(getPlayerPosition(cid), CONST_ME_POFF)
return doPlayerSendCancel(cid, msg)
end
function onSay(cid, words, param)
-- Example script by Colandus
local data = string.explode(param, ",")
if not data then -- if data was nil, then there was no text, lets send a cancel message
return warnPlayer(cid, "Please enter a name and a position.")
end
local name = data[1] -- data[1] should contain the name
if data[2] then -- data[2] should contain the position (not splitted yet)
local t = string.explode(data[2], " ") -- now we split the positions
local pos = tableToPos(t) -- now we make it to a real position (with x, y, z index), inside of the function it check if position is valid...
local player = getPlayerByName(name)
if pos == FALSE then -- not valid, send cancel message.
return warnPlayer(cid, "Please enter a valid position.")
elseif not positionExists(pos) then -- lets see if this position is exists on the map, if there is something on that pos...
return warnPlayer(cid, "Position does not exist!")
elseif player == FALSE then -- position was valid, but player was not online, send cancel message
return warnPlayer(cid, "Player is not online.")
end
doSendMagicEffect(player, CONST_ME_POFF) -- send a poff effect on the previous pos
doTeleportThing(player, pos) -- teleport the player to the new location
doSendMagicEffect(pos, CONST_ME_TELEPORT) -- send a magic effect to the new position.
else -- data[2] was not found, means that he did only write /teleport "name and nothing more...
warnPlayer(cid, "Please enter a position.")
end
return TRUE
end
That script has several restrictions and does not allow you to do wrong. I can't say it's 100% bug free, but I do think it is!
Enjoy this function, and feel free to use the script (you could strip off the comments if you wish)
Regards,
Colandus
Last edited: