• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

TFS 1.X+ TFS 1.3 current unstable release npc problem

Itutorial

Excellent OT User
Joined
Dec 23, 2014
Messages
2,307
Solutions
68
Reaction score
982
So with a fresh download of TFS 1.3 I get an error in npcs when I talk to them.

Code:
Lua Script Error: [Npc interface]
data/npc/scripts/ulfrik.lua:onThink
NpcScriptInterface::luagetDistanceTo(). Thing not found
stack traceback:
        [C]: in function 'getDistanceTo'
        data/npc/lib/npcsystem/npchandler.lua:567: in function 'isInRange'
        data/npc/lib/npcsystem/npchandler.lua:502: in function 'onThink'
        data/npc/scripts/ulfrik.lua:8: in function <data/npc/scripts/ulfrik.lua:8>

Anyone have any idea?
 
Solution
E
What do you mean a duplicated function?

0epnlvP.png


something like this (not tested)

Lua:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)

function onCreatureAppear(cid)            npcHandler:onCreatureAppear(cid)            end
function onCreatureDisappear(cid)        npcHandler:onCreatureDisappear(cid)            end
function onCreatureSay(cid, type, msg)        npcHandler:onCreatureSay(cid, type, msg)        end
function onThink()                npcHandler:onThink()                    end

local function creatureSayCallback(cid, type, msg)
    if not npcHandler:isFocused(cid) then
        return false
    end...
So with a fresh download of TFS 1.3 I get an error in npcs when I talk to them.

Code:
Lua Script Error: [Npc interface]
data/npc/scripts/ulfrik.lua:onThink
NpcScriptInterface::luagetDistanceTo(). Thing not found
stack traceback:
        [C]: in function 'getDistanceTo'
        data/npc/lib/npcsystem/npchandler.lua:567: in function 'isInRange'
        data/npc/lib/npcsystem/npchandler.lua:502: in function 'onThink'
        data/npc/scripts/ulfrik.lua:8: in function <data/npc/scripts/ulfrik.lua:8>

Anyone have any idea?

post the NPC code data/npc/scripts/ulfrik.lua
 
If it was something minor I would find it.

Lua:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)

function onCreatureAppear(cid)              npcHandler:onCreatureAppear(cid)            end
function onCreatureDisappear(cid)           npcHandler:onCreatureDisappear(cid)         end
function onCreatureSay(cid, type, msg)      npcHandler:onCreatureSay(cid, type, msg)    end
function onThink()                          npcHandler:onThink()                        end

local messageGreet = {"hi", "Hi", "hello", "Hello", "hey", "Hey"}

function onCreatureSay(cid, type, msg)
    local player = Player(cid)
    if not player then return true end

    if not npcHandler:isFocused(cid) and isInArray(messageGreet, msg) then
        npcHandler:addFocus(cid)
        selfSay("Greetings, "..player:getName()..". I have brought you back from the after-life. Your {story} has yet to begin.", cid)
    return true
    end
    
    if msg == "story" then
        selfSay("I do not have time to tell you your destiny. {Head south} out of the mountain and re-learn the ways of this world.", cid)
        npcHandler:releaseFocus(cid)
    end
    
    return true
end


npcHandler:addModule(FocusModule:new())

This is the code causing the error

Lua:
-- Returns true if cid is within the talkRadius of this npc.
    function NpcHandler:isInRange(cid)
        local distance = Player(cid) and getDistanceTo(cid) or -1
        if distance == -1 then
            return false
        end

        return distance <= self.talkRadius
    end

and this is where its called.

Lua:
-- Handles onThink events. If you wish to handle this yourself, please use the CALLBACK_ONTHINK callback.
    function NpcHandler:onThink()
        local callback = self:getCallback(CALLBACK_ONTHINK)
        if callback == nil or callback() then
            if NPCHANDLER_TALKDELAY == TALKDELAY_ONTHINK then
                for cid, talkDelay in pairs(self.talkDelay) do
                    if talkDelay.time and talkDelay.message and os.time() >= talkDelay.time then
                        selfSay(talkDelay.message, cid, talkDelay.publicize and true or false)
                        self.talkDelay[cid] = nil
                    end
                end
            end

            if self:processModuleCallback(CALLBACK_ONTHINK) then
                for pos, focus in pairs(self.focuses) do
                    if focus then
                        if not self:isInRange(focus) then
                            self:onWalkAway(focus)
                        elseif self.talkStart[focus] and (os.time() - self.talkStart[focus]) > self.idleTime then
                            self:unGreet(focus)
                        else
                            self:updateFocus()
                        end
                    end
                end
            end
        end
    end
 
take this as example:

and btw why are you using this check:
if not player then return true end
npcs will only respond to player anyway

also you have a duplicated function in your script: function onCreatureSay

and I recommend you to take a look at ORTS project NPCs, they can be very useful and well made to be used:
 
Last edited by a moderator:
Lua:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)

function onCreatureAppear(cid)            npcHandler:onCreatureAppear(cid)            end
function onCreatureDisappear(cid)        npcHandler:onCreatureDisappear(cid)            end
function onCreatureSay(cid, type, msg)        npcHandler:onCreatureSay(cid, type, msg)        end
function onThink()                npcHandler:onThink()                    end

local function creatureSayCallback(cid, type, msg)
    if not npcHandler:isFocused(cid) then
        return false
    end
    
    local player = Player(cid)
    
    if table.contains(msg, {"hi", "hello", "hey"}) then
        npcHandler:say("Greetings, "..player:getName()..". I have brought you back from the after-life. Your {story} has yet to begin.", cid)
        npcHandler.topic[cid] = 1
    elseif msgcontains(msg, "story") then
        if npcHandler.topic[cid] == 1 then
        npcHandler:say("I do not have time to tell you your destiny. {Head south} out of the mountain and re-learn the ways of this world.", cid)
            npcHandler.topic[cid] = 0
        end
    end
    return true
end

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 
What do you mean a duplicated function?

0epnlvP.png


something like this (not tested)

Lua:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)

function onCreatureAppear(cid)            npcHandler:onCreatureAppear(cid)            end
function onCreatureDisappear(cid)        npcHandler:onCreatureDisappear(cid)            end
function onCreatureSay(cid, type, msg)        npcHandler:onCreatureSay(cid, type, msg)        end
function onThink()                npcHandler:onThink()                    end

local function creatureSayCallback(cid, type, msg)
    if not npcHandler:isFocused(cid) then
        return false
    end
    local player = Player(cid)
    if msgcontains(msg, "story") then
        npcHandler:say("I do not have time to tell you your destiny. {Head south} out of the mountain and re-learn the ways of this world.", cid)
        npcHandler.topic[cid] = 0
    end
    return true
end

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:setMessage(MESSAGE_GREET, "Greetings, |PLAYERNAME|. I have brought you back from the after-life. Your {story} has yet to begin.")
npcHandler:addModule(FocusModule:new())
 
Solution
Back
Top