• 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!
  • 2026 staff recruitment is open! Check it out and consider applying!

TFS 1.X+ TFS 1.4.2 AddEvent Argument #3 is unsafe

lukatxd

Well-Known Member
Joined
Dec 9, 2010
Messages
135
Solutions
1
Reaction score
58
I know it may seem like a duplicate, but from the threads I found in this forum, they're not pointing to the same argument, and I tried their solutions beforehand.

I was trying to make a crossbow that shoots faster than normal without changing tfs sources, then I found the addEvent but I couldnt make it work there, so i decided to make a spell simulate that same effect.
Basically an exori frigo machine gun

current code looks like this:
LUA:
function onCastSpell(creature, variant)
    addEvent(shoot, 0, creature,variant)
    addEvent(shoot, 200, creature,variant)
    addEvent(shoot, 400, creature,variant)
    addEvent(shoot, 600, creature,variant)
    return true;
end

function shoot(creature,variant)
    combat:execute(creature, variant)
    return true
end

But I am getting the message:
Lua Script Error: [Spell Interface]
data/spells/scripts/attack/ice_strike.lua:onCastSpell
LuaScriptInterface::luaAddEvent(). Argument #3 is unsafe
stack traceback:
[C]: in function 'addEvent'
data/spells/scripts/attack/ice_strike.lua:17: in function <data/spells/scripts/attack/ice_strike.lua:14>

I already tried this, but the spell dont shoot anymore

I then went to see what was inside the "variant" argument by simply printing the keys in the table and I saw that it had a type and pos fields.
So I tried separating the arguments to rebuild the variant in the "shoot" method. Similar to this link from the forum.

That way the spell still shoots, but the warnings dont stop, they just change the target and the culprit is "variant.type". I'm kinda lost at this point.
 
Solution
It's because you using addEvent with creature check but imagine you logout.. Result: addEvent can't find the creature = Crash
For simple things like this you can actually use AI (ChatGpt) :D He will explain you everything why it's unsafe

Try this instead

LUA:
function onCastSpell(creature, variant)
    local cid = creature:getId()
    for i = 1, 4 do
        addEvent(shoot, (i - 1) * 200, cid, variant)
    end
    return true
end

function shoot(cid, variant)
    local creature = Creature(cid)
    if not creature then
        return
    end

    combat:execute(creature, variant)
end
It's because you using addEvent with creature check but imagine you logout.. Result: addEvent can't find the creature = Crash
For simple things like this you can actually use AI (ChatGpt) :D He will explain you everything why it's unsafe

Try this instead

LUA:
function onCastSpell(creature, variant)
    local cid = creature:getId()
    for i = 1, 4 do
        addEvent(shoot, (i - 1) * 200, cid, variant)
    end
    return true
end

function shoot(cid, variant)
    local creature = Creature(cid)
    if not creature then
        return
    end

    combat:execute(creature, variant)
end
 
Solution
It's because you using addEvent with creature check but imagine you logout.. Result: addEvent can't find the creature = Crash
For simple things like this you can actually use AI (ChatGpt) :D He will explain you everything why it's unsafe

Try this instead

LUA:
function onCastSpell(creature, variant)
    local cid = creature:getId()
    for i = 1, 4 do
        addEvent(shoot, (i - 1) * 200, cid, variant)
    end
    return true
end

function shoot(cid, variant)
    local creature = Creature(cid)
    if not creature then
        return
    end

    combat:execute(creature, variant)
end

I've been a programmer for a few years already, and every time I decide to mess around with OTs I feel like I never knew anything at all. Like I get the idea that you have to check for creature, but WHY would it warn me that VARIANT is unsafe if I needed to check for the creature? omg dude

Thank you so much, it is working flawlessly

--- omg i get it now, lua is not 0 based
 
I've been a programmer for a few years already, and every time I decide to mess around with OTs I feel like I never knew anything at all. Like I get the idea that you have to check for creature, but WHY would it warn me that VARIANT is unsafe if I needed to check for the creature? omg dude

Thank you so much, it is working flawlessly

--- omg i get it now, lua is not 0 based

Hahaha I know that feeling :D Don't worry you will get fast into it :D
 
afaik this should do the trick


LUA:
function onCastSpell(creature, variant)
    addEvent(shoot, 0, creature,variant)
    addEvent(shoot, 200, creature,variant)
    addEvent(shoot, 400, creature,variant)
    addEvent(shoot, 600, creature,variant)
    return true;
end

function shoot(creature,variant)
    combat:execute(creature.uid, variant)
    return true
end
 
afaik this should do the trick


LUA:
function onCastSpell(creature, variant)
    addEvent(shoot, 0, creature,variant)
    addEvent(shoot, 200, creature,variant)
    addEvent(shoot, 400, creature,variant)
    addEvent(shoot, 600, creature,variant)
    return true;
end

function shoot(creature,variant)
    combat:execute(creature.uid, variant)
    return true
end
No, it definitely won't. I guess you didn't read the previous replies where someone has already explained and solved the issue. What you have suggested is bad and may (most probably) result in a crash at some point during runtime.
 
Back
Top