• 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!

GlobalEvent Teleport summon to Master [TFS 1.0]

Printer

if Printer then print("LUA") end
Senator
Premium User
Joined
Dec 27, 2009
Messages
5,782
Solutions
31
Reaction score
2,284
Location
Sweden?
Hello,
Requested: http://otland.net/threads/tfs-1-0-summons-system.211885/

Code:
<globalevent name="SummonTeleport" interval="1000" script="summon_teleport.lua"/>

Code:
local distFromMaster = 7

function onThink(interval)
    for _, player in ipairs(Game.getPlayers()) do
        local playerPos = player:getPosition()
        if not Tile(playerPos):hasFlag(TILESTATE_PROTECTIONZONE) then
            local summons = player:getSummons()
            if #summons ~= 0 then
                for i = 1, #summons do
                    local summon = summons[i]
                    local summonPos = summon:getPosition()
                    if summonPos.z ~= playerPos.z or summonPos:getDistance(playerPos) > distFromMaster then
                        summon:teleportTo(playerPos)
                        summon:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
                    end
                end
            end
        end
    end
    return true
end
 
why iterate trough every player online? Is better to check directly on summons imho
Yeah I didnt even look at the script, I was just asking why better on sources.

I was writting something similar today in lua and it's better to just get summon master and its position xD
 
why iterate trough every player online? Is better to check directly on summons imho

It's better do this on the source... But we can use too the monster onThink (by lua). It's a simple code, like this:

Lua:
local xDistance = 5
local yDistance = 7

function onThink(self, interval)
    if self:isSummon() then -- By Drazyn
        local masterPos = self:getMaster():getPosition()
        local summonPos = self:getPosition()
        local teleport = false
       
        if math.abs(summonPos.x - masterPos.x) > xDistance then
            teleport = true
        elseif math.abs(summonPos.y - masterPos.y) > yDistance then
            teleport = true
        elseif (summonPos.z ~= masterPos.z) then
            teleport = true
        end
       
        if teleport then
            self:teleportTo(masterPos)
        end
    end
end
 
Well I was checking the distance with creature:getFollowCreature(), if summon doesnt have anyone to follow then it means that he lost his master.
 
It's better do this on the source... But we can use too the monster onThink (by lua). It's a simple code, like this:

Lua:
local xDistance = 5
local yDistance = 7

function onThink(self, interval)
    if self:isSummon() then -- By Drazyn
        local masterPos = self:getMaster():getPosition()
        local summonPos = self:getPosition()
        local teleport = false
      
        if math.abs(summonPos.x - masterPos.x) > xDistance then
            teleport = true
        elseif math.abs(summonPos.y - masterPos.y) > yDistance then
            teleport = true
        elseif (summonPos.z ~= masterPos.z) then
            teleport = true
        end
      
        if teleport then
            self:teleportTo(masterPos)
        end
    end
end
you'd still have to register the event to the summon once it's created
 
Well I was checking the distance with creature:getFollowCreature(), if summon doesnt have anyone to follow then it means that he lost his master.
pretty good idea, also what about if you are on pz?
 
you'd still have to register the event to the summon once it's created

Not exactly, you have to use the script on monster XML, using this tag on the first line of the Monster XML:

script="global.lua"

<monster name="Dragon Lord" nameDescription="a dragon lord" race="blood" experience="2100" script="global.lua" speed="200">

And have to add the script into the folder scripts (data/monster/scripts/global.lua).
 
Not exactly, you have to use the script on monster XML, using this tag on the first line of the Monster XML:

script="global.lua"

<monster name="Dragon Lord" nameDescription="a dragon lord" race="blood" experience="2100" script="global.lua" speed="200">

And have to add the script into the folder scripts (data/monster/scripts/global.lua).
that's even worse to do, because then you're registering the script to every single monster even if it's not a summon.
you'd have events executing on things that aren't summons for no reason in that case.
 
that's even worse to do, because then you're registering the script to every single monster even if it's not a summon.
you'd have events executing on things that aren't summons for no reason in that case.

I don't think so... Because, if we do by src, we will use the same functions (but in c++), it's course if we use on the onThink monster.
 
I was thinking, and if we can register this onThink direct on the summon, will be better, but i don't know how... Do you have any idea?
when you summon it in the spell, use creature:registerEvent(eventName)
it should be in creaturescripts folder instead of monsters
 
when you summon it in the spell, use creature:registerEvent(eventName)
it should be in creaturescripts folder instead of monsters
It's true ... I was thinking that onThink did not have the metatable creature, but when I checked it I found it ... That way it gets a lot better.
 
onThink doesn't belong to any metatables, it's an interface function.
 
Back
Top