TFS 1.2 Get Spec...2

lazarus321

Member
Joined
May 8, 2017
Messages
78
Reaction score
6
I have another similar question in this post, if this function creates an area (considering from me) how would I exclude myself from it? ex. I have 3 monsters 1 player and I in the area of magic, I would like me to exclude more than another player and the monsters..

 

StreamSide

Joseluis Gonzalez
Support Team
Joined
Aug 31, 2007
Messages
3,164
Reaction score
721
Location
Arica - Chile
the function will return a table, you can actually print the table and there are a few on this forum. So if you have a table you can use any table function to locate yourself or anything on it
You can do something like
first check at this: [any TFS] print_r, improved print (https://otland.net/threads/any-tfs-print_r-improved-print.244958/)
with this function you can print all the things inside the table
So let use the same code (a lil bit modified and with the print_r function), but inside a talkaction to make it usable:
Lua:
function print_r ( t ) 
    local print_r_cache={}
    local function sub_print_r(t,indent)
        if (print_r_cache[tostring(t)]) then
            print(indent.."*"..tostring(t))
        else
            print_r_cache[tostring(t)]=true
            if (type(t)=="table") then
                for pos,val in pairs(t) do
                    if (type(val)=="table") then
                        print(indent.."["..pos.."] => "..tostring(t).." {")
                        sub_print_r(val,indent..string.rep(" ",string.len(pos)+8))
                        print(indent..string.rep(" ",string.len(pos)+6).."}")
                    else
                        print(indent.."["..pos.."] => "..tostring(val))
                    end
                end
            else
                print(indent..tostring(t))
            end
        end
    end
    sub_print_r(t,"  ")
end

function onSay(player, words, param)
    local spectators = Game.getSpectators(player:getPosition(), false, false, 5, 5, 5, 5)
    if spectators and #spectators > 0 then
        for i = 1, #spectators do
            local spectator = spectators[i]
            spectator:say(spectator:getName(), TALKTYPE_SAY)
        end
        print_r(spectators)
    end
    return false
end

This will do two things, make us (us: the players/monsters/npcs around) to say our name and print the values inside the table


and

As you can check, there are 2 values, my userdata and the npc userdata
With this information, I can play with the functions of the server and the lua functions (check this)
So lets play:
As you see, the script will check for everything inside and then make it say his name, what if I want only players to say his name?
Lua:
function onSay(player, words, param)
    local spectators = Game.getSpectators(player:getPosition(), false, false, 5, 5, 5, 5)
    if spectators and #spectators > 0 then
        for i = 1, #spectators do
            local spectator = spectators[i]
            if spectator:isPlayer() then
                spectator:say(spectator:getName(), TALKTYPE_SAY)
            end
        end
    end
    return false
end
As you can see now I only say my name.
So what if I want to ban some players/npcs from this function? Like if your name is Test, Tester or Testerinho your name wont appear.
Lets see:
Lua:
function onSay(player, words, param)
    local spectators = Game.getSpectators(player:getPosition(), false, false, 5, 5, 5, 5)
    local names = {
        "Test", "Tester", "Testinho"
    }
    if spectators and #spectators > 0 then
        for i = 1, #spectators do
            local spectator = spectators[i]
            if not table.contains(spectator:getName(), names) then
                spectator:say(spectator:getName(), TALKTYPE_SAY)
            end
        end
    end
    return false
end

Ok, now lets remove the caster itself, but from the table, then we can iterate again the table, and lets check what it prints.
Lua:
function onSay(player, words, param)
    local spectators = Game.getSpectators(player:getPosition(), false, false, 5, 5, 5, 5)
    if spectators and #spectators > 0 then
        for i = 1, #spectators do
            local spectator = spectators[i]
            if spectator:getName() == player:getName() then
                spectators[i] = nil
            end
        end
        for i = 1, #spectators do
            local spectator = spectators[i]
            spectator:say(spectator:getName(), TALKTYPE_SAY)
        end
    end
    return false
end

Only my friendly npc will say his name (and anyone close enough), but not me.
Lets check the table again!
Lua:
function onSay(player, words, param)
    local spectators = Game.getSpectators(player:getPosition(), false, false, 5, 5, 5, 5)
    if spectators and #spectators > 0 then
        for i = 1, #spectators do
            local spectator = spectators[i]
            if spectator:getName() == player:getName() then
                spectators[i] = nil
            end
        end
        for i = 1, #spectators do
            local spectator = spectators[i]
            spectator:say(spectator:getName(), TALKTYPE_SAY)
        end
        print_r(spectators)
    end
    return false
end

Hope it helps.
 
OP
L

lazarus321

Member
Joined
May 8, 2017
Messages
78
Reaction score
6
helped a lot, very well explained thanks again. Like+
 
Top