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

Lua [Help] addEvent with Item metatable

drakylucas

Intermediate OT User
Joined
Dec 15, 2015
Messages
236
Solutions
7
Reaction score
121
Hello guys,
I need help, how can I use addEvent with an item in action?

(TFS 1.2)

I need to transform the "target" item from action after ~3 seconds.
for player, I usually create a new instance

PHP:
function event(cid)
  local player = Player(cid)
end
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
     addEvent(event,3000,player:getId())
     return true
end

but how can I do it with "target" param? if I use target:transform(xxxx) in script, it works correctly, but if I use it on addEvent, this fail, and the argument target:getId() just return the ID of the item, so I can't transform it :s
thanks for help.
 
Hello guys,
I need help, how can I use addEvent with an item in action?

(TFS 1.2)

I need to transform the "target" item from action after ~3 seconds.
for player, I usually create a new instance

PHP:
function event(cid)
  local player = Player(cid)
end
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
     addEvent(event,3000,player:getId())
     return true
end

but how can I do it with "target" param? if I use target:transform(xxxx) in script, it works correctly, but if I use it on addEvent, this fail, and the argument target:getId() just return the ID of the item, so I can't transform it :s
thanks for help.

You are using player:getId() and not target:getId(), you have to pass the target to the event method I guess.

EDIT: Can't you just do something like this:

Code:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
     addEvent(target:transform(xxxx), 3000)
    
     return true
end
 
You are using player:getId() and not target:getId(), you have to pass the target to the event method I guess.

EDIT: Can't you just do something like this:

Code:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
     addEvent(target:transform(xxxx), 3000)
   
     return true
end

No because it's not only for transform, I need to check some things after this 3 seconds before transform, so I need to run a function in this addEvent
and the target:getId() return only the id of the item :s

at the moment, I'm using an other way (but I guess it's not correctly, but works)

Example:
Code:
function event(toPosition, targetId)
local target = Tile(toPosition):getItemById(targetId)
if (not target) then return false end
target:transform(2453)
end

onUse(player, item, fromPosition, target, toPosition, isHotkey)
    addEvent(event,3000,toPosition,target:getId())
    return true
end

it's for a small lumberjacking script, like Necronia. (PS: this isn't all the script, I've some verifications before target:transform, like check if the player has some item...)
Is there a better way to do this?
 
Code:
local function transformTo(toPosition, itemId)
    local item = Tile(toPosition):getItemById(itemId)
    if item then
        item:transform(2453)
    end
end

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    addEvent(transformTo, 3000, toPosition, target:getId())
end
 
You are using player:getId() and not target:getId(), you have to pass the target to the event method I guess.

EDIT: Can't you just do something like this:

Code:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
     addEvent(target:transform(xxxx), 3000)
  
     return true
end
so bad idea! SO SO bad idea xD
in many cases if within that 3 seconds target gets removed server may crash.

drakylukas welcome to TFS 1.2 annoying UID section.
on TFS 1.0 something like this used to work:
Code:
itemUID = item:getUniqueId()
addEvent(doTransform, 3000, itemUID, toItemID)
But now every time item is accessed, it has its own uniqueID
What I did was turn off the source restriction where we couldn't set unique ID for items and seems like if you set UID to item, then it stays.
It doesn't solve many of the problems and one day might conflict with the source, but in this case it would help.

The other way I do item transformation is this:
I register item Position where is was last seen and then from that position i find itemID and transform it.
You might want to add AID to item and make it unmoveable then. Or depending on item, you can make a tracker to it. But there are lot of "black holes" in TFS where item location is unknown.
 
But now every time item is accessed, it has its own uniqueID
What I did was turn off the source restriction where we couldn't set unique ID for items and seems like if you set UID to item, then it stays.

Why do you even wanna give the item a custom UID? This, if something sounds like a bad idea for hes problem.
Do not all items already have a UID?

Yes @drakylucas , go with printers method, I am sure he provide you a good solution. I barely don't know how to do LUA anymore haha.
 
I'm using the same method as @Printer ,
so, is this the best way at the moment?

Thanks all :)
Seems like it.

Why do you even wanna give the item a custom UID? This, if something sounds like a bad idea for hes problem.
Do not all items already have a UID?

Yes @drakylucas , go with printers method, I am sure he provide you a good solution. I barely don't know how to do LUA anymore haha.
All items have unique ID, however if its not set. Its GENERATED each time the its asked "what is your UID?" aka item:getUniqueId()

If you give UID to item you can do something like this:
Item(UID)
This will construct the item userdata if it exists regardless where it is. (at least in most cases, don't know will it be created if item is in depots or on offline chars)
 
Seems like it.


All items have unique ID, however if its not set. Its GENERATED each time the its asked "what is your UID?" aka item:getUniqueId()

If you give UID to item you can do something like this:
Item(UID)
This will construct the item userdata if it exists regardless where it is. (at least in most cases, don't know will it be created if item is in depots or on offline chars)

Ok, if you wanna do something with the item itself later? I see. But I cannot see how this would be a solution for him xD
 
Ok, if you wanna do something with the item itself later? I see. But I cannot see how this would be a solution for him xD
I gave 2 solutions. the one what printer posted and the UID one.

What he wants to do is transform item and you can do exactly that with userdata.. you even said it in first part of sentence :D
 
Back
Top