• 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+ Whats the difference addFocus and RealeseFocus?

roriscrave

Advanced OT User
Joined
Dec 7, 2011
Messages
1,188
Solutions
34
Reaction score
200
Hi, I'm trying to study the npcs a little...
i see in oracle code this part, what is difference of addFocus and RealeseFocus?
scrit npc oracle in github
Lua:
local function onAddFocus(cid)
    town[cid] = 0
    vocation[cid] = 0
    destination[cid] = 0
end

local function onReleaseFocus(cid)
    town[cid] = nil
    vocation[cid] = nil
    destination[cid] = nil
end
 
Solution
onAddFocus is when player say "hi"?
if i remove all this part, the script will work incorrect?
i dont understand why i need to set 0 and nil, i think set only one of this works fine, or i'm wrong?
Lua:
local function onReleaseFocus(cid)
    town[cid] = nil
    vocation[cid] = nil
    destination[cid] = nil
end
The script will work fine regardless I believe, but when you remove a value from a table the memory stays allocated, but it's marked to be freed up so something else can use it next garbage collection cycle.
Otherwise the table will just get bigger and bigger for no reason (this will still barely even use memory, but at the same time why use resources for no reason?)
They''re both callback functions, meaning they get called when an event is triggered (in this case, onAddFocus is called when the NPC adds focus to a player, go figure what onReleaseFocus does). In this case, it just initializes a few values to 0, and removes the cid from each table when focus is released.
Each implementation is different, it all just depends on what you want to do when the focus is added/removed.
 
They''re both callback functions, meaning they get called when an event is triggered (in this case, onAddFocus is called when the NPC adds focus to a player, go figure what onReleaseFocus does). In this case, it just initializes a few values to 0, and removes the cid from each table when focus is released.
Each implementation is different, it all just depends on what you want to do when the focus is added/removed.
onAddFocus is when player say "hi"?
if i remove all this part, the script will work incorrect?
i dont understand why i need to set 0 and nil, i think set only one of this works fine, or i'm wrong?
Lua:
local function onReleaseFocus(cid)
    town[cid] = nil
    vocation[cid] = nil
    destination[cid] = nil
end
 
onAddFocus is when player say "hi"?
if i remove all this part, the script will work incorrect?
i dont understand why i need to set 0 and nil, i think set only one of this works fine, or i'm wrong?
Lua:
local function onReleaseFocus(cid)
    town[cid] = nil
    vocation[cid] = nil
    destination[cid] = nil
end
The script will work fine regardless I believe, but when you remove a value from a table the memory stays allocated, but it's marked to be freed up so something else can use it next garbage collection cycle.
Otherwise the table will just get bigger and bigger for no reason (this will still barely even use memory, but at the same time why use resources for no reason?)
 
Solution
onAddFocus is when player say "hi"?
if i remove all this part, the script will work incorrect?
i dont understand why i need to set 0 and nil, i think set only one of this works fine, or i'm wrong?
Lua:
local function onReleaseFocus(cid)
    town[cid] = nil
    vocation[cid] = nil
    destination[cid] = nil
end
It's unlikely that anything will stop working, but it's there for 2 reasons.

First, is for garbage collection.. (free up available memory)

Second, is in the very unlikely event that you have enough creatures spawning/despawning, that the number eventually loops around, and the npc still have the old data when another player re-uses a creatureid that the npc had stored previously, it could potentially screw up the character / npc dialogue and/or functions, depending on how you have it setup.
 
Back
Top