• 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.2 - Simple Magic Effects Function

Lava Titan

Developer
Joined
Jul 25, 2009
Messages
1,529
Solutions
1
Reaction score
85
Location
Portugal
Hey there, I created this simple function, it's nothing special but I guess it may save some lines for some people :p

Some people may find it useless I guess, but like I said it's nothing special, it's just a simple function :p

How to add?


It's very simple, you can start by browsing "server\data\lib\compat\compat.lua" and add this code in last line.

Code:
function Player.sendMagicEffect(self, effect)
    self:getPosition():sendMagicEffect(effect)
    return true
end

How to use?

After you got this added in compat.lua you can use it on any Lua script you want, for example:

Code:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    player:sendMagicEffect(CONST_ME_GIFT_WRAPS)
    return true
end

Instead of using the first one, you can always use this which is more simple and can also be used in addEvent :p

Code:
function sendMagicEffect(playerId, effect)
    local player = Player(playerId)
    if not player then
        return
    end
    player:getPosition():sendMagicEffect(effect)
    return true
end


After you can simply use like:

Code:
addEvent(sendMagicEffect, 1000, CONST_ME_GIFT_WRAPS)


For those who use Sublime Text 3 TFS AutoComplete, here's the code:

Code:
    {
      "trigger": "Player:sendMagicEffect",
      "contents": "${1:player}:sendMagicEffect(${2:effect})"
    },

or

Code:
    {
      "trigger": "sendMagicEffect",
      "contents": "sendMagicEffect(${1:effect})"
    },
 
Last edited by a moderator:
The addEvent function has a dangling pointer.
 
You should always check if the creature exsist, if you going to use addEvent.

Try it yourself, use the addEvent and logout before the event get executed. It will cause error.
 
agree with @Printer when you try use addEvent with no existent "object" causes crash in tfs
 
fixed I guess :p
Nope! The function(s) will not even execute now due to a non-existent userdata object (player is not defined anywhere, and you shouldn't use isPlayer).

When you use addEvent, pass player ID as an argument, and construct the Player userdata object inside the function, e.g:
Code:
local function sendMagicEffect(playerId, effect)
    local player = Player(playerId)
    if not player then
        return
    end

    player:getPosition():sendMagicEffect(effect)
end
 
This is quite useful.

However with Position.sendMagicEffect you can actually use addEvent safely by retrieving the position and passing it as the argument.
Code:
local position = player:getPosition()
addEvent(Position.sendMagicEffect, 1000, position, CONST_ME_MAGIC_RED)
 
Back
Top