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

Tables actionsid in lua? Checking condition in lop.

Srync

New Member
Joined
Dec 24, 2017
Messages
9
Reaction score
0
How can I make levers tables? I have 6 levers and I have to set them in the appropriate setting. By what I have to write in the script 6 conditions and make 6 files in actions. Can I somehow go around it, to write less code and be more readable?
 
Solution
Well if you want to trigger an action when levers are in a certain order:
Lua:
local lever_trans = {
    [1945] = 1946,
    [1946] = 1945
}
local levers = {
    {Position(104, 129, 7), 1945}, --lever position, desired id (1945 lever to the left, 1946 lever to the right)
    {Position(104, 130, 7), 1946},
    {Position(104, 131, 7), 1945},
    {Position(106, 129, 7), 1946},
    {Position(106, 130, 7), 1945},
    {Position(106, 131, 7), 1946}
}
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local correct_levers = 0
    item:transform(lever_trans[item:getId()])
    for i = 1, #levers do
        local lever = getTileItemById(levers[i][1], levers[i][2]).uid
        if lever > 0 then
            correct_levers =...
Do you mean that until (IE) 2 are to the left and 4 to the right x action will trigger? or that until they are used in a certain order x action will trigger?
Also remember to tell us which server you're using (TFS 0.x, 1.x, etc)
 
How can I make levers tables? I have 6 levers and I have to set them in the appropriate setting. By what I have to write in the script 6 conditions and make 6 files in actions. Can I somehow go around it, to write less code and be more readable?
It's simple no matter what distribution you are using, if it has actionscripts then you can use item.itemid to index a table.
Lua:
-- this should defined outside of onUse
local levers = {
    -- 1234 would be if it was a lever item id
    [1234] = {--[[ put whatever u want in here]]}
}
-- and this should be defined inside of onUse
if levers[item.itemid] then
    -- do something
end
 
Do you mean that until (IE) 2 are to the left and 4 to the right x action will trigger? or that until they are used in a certain order x action will trigger?
Also remember to tell us which server you're using (TFS 0.x, 1.x, etc)

That's exactly what I mean. I am using otx server 3.

It's simple no matter what distribution you are using, if it has actionscripts then you can use item.itemid to index a table.
Lua:
-- this should defined outside of onUse
local levers = {
    -- 1234 would be if it was a lever item id
    [1234] = {--[[ put whatever u want in here]]}
}
-- and this should be defined inside of onUse
if levers[item.itemid] then
    -- do something
end

I must enter the id of my 6 levers in local leavers?
 
That's exactly what I mean. I am using otx server 3.



I must enter the id of my 6 levers in local leavers?
It really depends on what your trying to do.

I'd use an actionid in actions.xml, for the lever to trigger the script, and use uniqueid's in your table to differentiate the levers from one another.
on map editor, put the aid in actions.xml on the levers, and a different uid on each one.

then you do something like this.. Just like @bayview said.

Lua:
local levers = {
    [11111] = {},
    [22222] = {},
    [33333] = {},
    [44444] = {},
    [55555] = {},
    [66666] = {}
}

function onUse(a,item,c,d,e)
    if levers[item.uid] then
        -- do stuff
    end
    return true
end
 
That's exactly what I mean. I am using otx server 3.



I must enter the id of my 6 levers in local leavers?

Gave you two options lol, so is it in an order like lever 5, then 3, then 6, etc or in a certain combination like 2 to left 4 to the right?
 
I do not know if you understend me well. I have 6 levers, in the map editor there is uniqueid: 1111,2222,3333,4444,5555,6666. In the action file, I add my leavers and in the action folder I have to add 6 files, which change the variables which then I check when I enter the teleport. What the teleport condition looks like: if lever1 == 1 and lever2 == 1 ... And lever6 == 1 then
To something.
End
So I wanted to find out if I could improve it, not to create 6 files in actions and compare 6 variables in the teleport condition. I would like my code to be more readable. Is it possible to put these levers in the table and compare all the loops?
 
I do not know if you understend me well. I have 6 levers, in the map editor there is uniqueid: 1111,2222,3333,4444,5555,6666. In the action file, I add my leavers and in the action folder I have to add 6 files, which change the variables which then I check when I enter the teleport. What the teleport condition looks like: if lever1 == 1 and lever2 == 1 ... And lever6 == 1 then
To something.
End
So I wanted to find out if I could improve it, not to create 6 files in actions and compare 6 variables in the teleport condition. I would like my code to be more readable. Is it possible to put these levers in the table and compare all the loops?
Just post the script or scripts and then people can help you.
 
Well if you want to trigger an action when levers are in a certain order:
Lua:
local lever_trans = {
    [1945] = 1946,
    [1946] = 1945
}
local levers = {
    {Position(104, 129, 7), 1945}, --lever position, desired id (1945 lever to the left, 1946 lever to the right)
    {Position(104, 130, 7), 1946},
    {Position(104, 131, 7), 1945},
    {Position(106, 129, 7), 1946},
    {Position(106, 130, 7), 1945},
    {Position(106, 131, 7), 1946}
}
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local correct_levers = 0
    item:transform(lever_trans[item:getId()])
    for i = 1, #levers do
        local lever = getTileItemById(levers[i][1], levers[i][2]).uid
        if lever > 0 then
            correct_levers = correct_levers + 1
        end
    end
    if correct_levers == #levers then
        print("All levers in correct position")
    end
    return true
end
in this example it'll trigger when the levers 1, 3 and 5 are to the left.
If you want to check when the player steps in a TP or something you could use it like this, same thing lever 1, 3 and 5 to the left (id 1945):
Lua:
local levers = {
    {Position(104, 129, 7), 1945},
    {Position(104, 130, 7), 1946},
    {Position(104, 131, 7), 1945},
    {Position(106, 129, 7), 1946},
    {Position(106, 130, 7), 1945},
    {Position(106, 131, 7), 1946}
}
function onStepIn(creature, item, position, fromPosition)
    local correct_levers = 0
    for i = 1, #levers do
        local lever = getTileItemById(levers[i][1], levers[i][2]).uid
        if lever > 0 then
            correct_levers = correct_levers + 1
        end
    end
    if correct_levers == #levers then
        print("All levers in correct position")
    end
    return true
end
 
Solution
Bogart
Yes, that's what I meant. Thank you, tomorrow I will test the script. I am sorry that I wrote the first post incomprehensibly.
 
Back
Top