• 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+ [LUA]AddEvent with function problem [Newbie]

Klank

Althea ¤ A New World Developer
Joined
Feb 21, 2008
Messages
1,080
Reaction score
639
Location
Norway
Hello,
I'm currently trying to learn scripting and im facing an (probably easy) issue.
I know Line 13 causing the error, but i dont know how to fix it yet. I followed the tutorial from this thread; [How-to] Using addEvent() by Evan.
I'm trying to reset a function that was done after X seconds with a "AddEvent".

Appriciate any help.

Code;
1670527146157.png
 
You must do this inside the function that will be executed after the timeout:
1670528199256.png
so you must pass toPosition and 3944 as arguments instead.
1670528345032.png
You cannot pass user data as arguments.
only can be safely passed: numbers, booleans, strings, nil, tables (these should not contain user data obviously)
 
You must do this inside the function that will be executed after the timeout:
View attachment 72202
so you must pass toPosition and 3944 as arguments instead.
View attachment 72203
You cannot pass user data as arguments.
only can be safely passed: numbers, booleans, strings, nil, tables (these should not contain user data obviously)
hmm okey. Can you like show me exactly how it should look like in my script? I tried to add these but didnt work. As title says, sorry, but i'm fairly new to this..
 
toPosition is the position player selected after right click on item with far-use like sign are you sure this toPosition is inserted correctly to the event so it can be parameter in the onUse?
Hey, thanks for the responds. Hmm ok, do you have any examples to show on how to use "toPosition" in event in a function?
I can't get it to work, i've read some basic lua, but i dont understand everything about argument, index etc. You got any tips on a good page to read about it?
 
Hey, thanks for the responds. Hmm ok, do you have any examples to show on how to use "toPosition" in event in a function?
I can't get it to work, i've read some basic lua, but i dont understand everything about argument, index etc. You got any tips on a good page to read about it?
Look carefully at the image that I sent you, obviously your script is badly formed.
If you would be so kind as to pass along the formatted code, I would gladly do the same for you.
1670603044943.png
 
Look carefully at the image that I sent you, obviously your script is badly formed.
If you would be so kind as to pass along the formatted code, I would gladly do the same for you.
View attachment 72213

Sorry i must be blind... I feel i have exact copy of your picture.
Code is in here; Script at Github
With this last "version" of code, im getting this;
1670607534708.png
 
Sorry i must be blind... I feel i have exact copy of your picture.
Code is in here; Script at Github
With this last "version" of code, im getting this;
View attachment 72215
It is not an exact copy, you should go more calmly and pay attention, but don't worry here is the code for you:
Lua:
local stairPosition = Position(736, 453, 10)

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if item.itemid == 3943 then
        item:transform(3944)
        stairPosition:sendMagicEffect(CONST_ME_POFF)
        Game.createItem(4835, 1, stairPosition)
        player:sendTextMessage(MESSAGE_INFO_DESCR, "You've lit the torch and a secret stairwell appears!.")

        addEvent(function(tilePosition, toPosition, torchId)
            Game.createItem(3177, 1, tilePosition)
            local torch = Tile(toPosition):getItemById(torchId)
            if torch then
                torch:transform(3943)
            end
            tilePosition:sendMagicEffect(CONST_ME_POFF)
        end, 2000, stairPosition, toPosition, 3944)
    else
        item:transform(3943)
        local stairWell = Tile(stairPosition):getItemById(4835)
        if stairWell then
            stairWell:transform(3177)
        end
    end
    return true
end
 
It is not an exact copy, you should go more calmly and pay attention, but don't worry here is the code for you:
Lua:
local stairPosition = Position(736, 453, 10)

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if item.itemid == 3943 then
        item:transform(3944)
        stairPosition:sendMagicEffect(CONST_ME_POFF)
        Game.createItem(4835, 1, stairPosition)
        player:sendTextMessage(MESSAGE_INFO_DESCR, "You've lit the torch and a secret stairwell appears!.")

        addEvent(function(tilePosition, toPosition, torchId)
            Game.createItem(3177, 1, tilePosition)
            local torch = Tile(toPosition):getItemById(torchId)
            if torch then
                torch:transform(3943)
            end
            tilePosition:sendMagicEffect(CONST_ME_POFF)
        end, 2000, stairPosition, toPosition, 3944)
    else
        item:transform(3943)
        local stairWell = Tile(stairPosition):getItemById(4835)
        if stairWell then
            stairWell:transform(3177)
        end
    end
    return true
end

Thank you man, this time it didnt crash :D The event works 90%, the tileposition does not revert back to its normal tile (3177). Why? Does this have anything to do about the value is not called back to the function? The "game:createitem" for 3177 doesnt actually go anywhere?
Also another question, you added "target" and "isHotkey" to the main function, why? Are these parameters for TFS described anywhere?
 
It works for me, in fact I just added a variable to save the ID of the addEvent and cancel it if someone decides to turn off the torch before it does it automatically, this is because it is no longer necessary to execute the function.
Lua:
local stairPosition = Position(736, 453, 10)
local eventId = nil

function action.onUse(player, item, fromPos, target, toPos, isHotkey)
    if item:getId() == 3943 then
        item:transform(3944)
        stairPosition:sendMagicEffect(CONST_ME_POFF)
        Game.createItem(4835, 1, stairPosition)
        player:sendTextMessage(MESSAGE_INFO_DESCR, "You've lit the torch and a secret stairwell appears!.")
        eventId = addEvent(function(stairPosition, toPos, torchId)
            Game.createItem(3177, 1, stairPosition)
            local torch = Tile(toPos):getItemById(torchId)
            if torch then
                torch:transform(3943)
            end
            stairPosition:sendMagicEffect(CONST_ME_POFF)
        end, 2000, stairPosition, toPos, 3944)
    else
        stopEvent(eventId)
        item:transform(3943)
        local stairWell = Tile(stairPosition):getItemById(4835)
        if stairWell then
            stairWell:transform(3177)
        end
    end
    return true
end
start.gif


Regarding your question about arguments:
The arguments are sent from C++ HERE

isHotkey is true whenever the action comes from a hotkey action. for example F1 or another key.
This is necessary for example when you use something in your inventory, I think you can't use something on the map with hotkeys.
 
It works for me, in fact I just added a variable to save the ID of the addEvent and cancel it if someone decides to turn off the torch before it does it automatically, this is because it is no longer necessary to execute the function.
Lua:
local stairPosition = Position(736, 453, 10)
local eventId = nil

function action.onUse(player, item, fromPos, target, toPos, isHotkey)
    if item:getId() == 3943 then
        item:transform(3944)
        stairPosition:sendMagicEffect(CONST_ME_POFF)
        Game.createItem(4835, 1, stairPosition)
        player:sendTextMessage(MESSAGE_INFO_DESCR, "You've lit the torch and a secret stairwell appears!.")
        eventId = addEvent(function(stairPosition, toPos, torchId)
            Game.createItem(3177, 1, stairPosition)
            local torch = Tile(toPos):getItemById(torchId)
            if torch then
                torch:transform(3943)
            end
            stairPosition:sendMagicEffect(CONST_ME_POFF)
        end, 2000, stairPosition, toPos, 3944)
    else
        stopEvent(eventId)
        item:transform(3943)
        local stairWell = Tile(stairPosition):getItemById(4835)
        if stairWell then
            stairWell:transform(3177)
        end
    end
    return true
end
View attachment 72217


Regarding your question about arguments:
The arguments are sent from C++ HERE

isHotkey is true whenever the action comes from a hotkey action. for example F1 or another key.
This is necessary for example when you use something in your inventory, I think you can't use something on the map with hotkeys.
Thats weird.. i see the poff and everything, but the actual tile does not go back to 3177, it stays at 4835..
Thx for the other info, but i was more intrested in when and how i should use the different parameters within the function.
 
parameter 1: player that triggers
parameter 2: triggered item.
parameter 3: position of the triggered item.
parameter 4: item that triggered the event. (optional)
parameter 5: position of the item that triggered the event. (optional)
parameter 6: True/False if this action comes from a fast access key.

It doesn't matter what name you give to the arguments, only the order matters.
if you want to call the first argument "item", it doesn't mean this is the item, it will still be the player because it is the first argument on the stack. and the first argument we already know is the player.

sss.gif
 
parameter 1: player that triggers
parameter 2: triggered item.
parameter 3: position of the triggered item.
parameter 4: item that triggered the event. (optional)
parameter 5: position of the item that triggered the event. (optional)
parameter 6: True/False if this action comes from a fast access key.

It doesn't matter what name you give to the arguments, only the order matters.
if you want to call the first argument "item", it doesn't mean this is the item, it will still be the player because it is the first argument on the stack. and the first argument we already know is the player.

View attachment 72222
Thank you , that explain a lot! Exactly what i was wondering on.
You still have no idea on why yours work and mine not? Regarding the tile being removed/added?
 
Update:
Somehow the revert of the tile worked as expected if i inserted an "if" statement extra in the event, similar to the "else" statement Instead of using "game.createitem()".
Dont ask me why, but it worked.
 
Update:
Somehow the revert of the tile worked as expected if i inserted an "if" statement extra in the event, similar to the "else" statement Instead of using "game.createitem()".
Dont ask me why, but it worked.
Well, I don't know what engine you're using exactly, but if you don't use the latest version of TFS, it wouldn't be strange at all for nonsense things to happen. I'm glad you found another alternative.

It is likely that in your version of the engine the function transform on the grounds does not work correctly.
however Game.createItem will replace the current floor if you create a new one in the same location. so the trick works.
In my tests, the script works because I use TFS 1.5 and transform works well for grounds.
 
Back
Top