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

Lua [TFS 1.3] help with a table

E

Evil Puncker

Guest
hi everyone, I'm trying to make the machete script more user friendly but I miss the knowledge to do so:

Lua:
local wildGrowth = { 1499, 11099 }
local jungleGrass = {
    [2782] = 2781,
    [3985] = 3984,
    [19433] = 19431
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local targetId = target.itemid
    if table.contains(jungleGrass, targetId) then
        target:transform(jungleGrass[target:getId()])
        target:decay()
        return true
    end

    if table.contains(wildGrowth, targetId) then
        toPosition:sendMagicEffect(CONST_ME_POFF)
        target:remove()
        return true
    end
    return destroyItem(player, target, toPosition)
end

what i'm missing right now is to make the first if works, it should check if target ID is the first value of the jungleGrass table, and then transform it to the second value
 
Solution
hey this is too complicated to me 🤣 this is what I have and working right now:

Lua:
local wildGrowth = { 1499, 11099 }
local jungleGrass = {
    [2782] = 2781,
    [3985] = 3984,
    [19433] = 19431
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local targetId = target.itemid
    if table.contains(wildGrowth, targetId) then
        toPosition:sendMagicEffect(CONST_ME_POFF)
        target:remove()
        return true
    end

    local grass = jungleGrass[targetId]
    if not grass then
        return destroyItem(player, target, toPosition)
    else
        target:transform(grass)
        target:decay()
        return true
    end
    return true
end

but I think it has too many returns in it, doesn't?
The...
You just need to rewrite that if statement to if jungleGrass[target:getId()] then
You should also check if the target is actually an item before using item methods on it, otherwise you'll get errors for targeting creatures.
The reason why what you wrote doesn't work is because table.contains compares by values, not keys. If you ever need to check if a key is in a table, literally all you have to do is index it. If the result of that is anything but nil, the key exists and has a corresponding value.
 
You just need to rewrite that if statement to if jungleGrass[target:getId()] then
You should also check if the target is actually an item before using item methods on it, otherwise you'll get errors for targeting creatures.
The reason why what you wrote doesn't work is because table.contains compares by values, not keys. If you ever need to check if a key is in a table, literally all you have to do is index it. If the result of that is anything but nil, the key exists and has a corresponding value.
okay it works but it looks ugly, what am I doing wrong now?

Lua:
local wildGrowth = { 1499, 11099 }
local jungleGrass = {
    [2782] = 2781,
    [3985] = 3984,
    [19433] = 19431
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local targetId = target.itemid
    local grass = jungleGrass[target.itemid]
    if not grass then
        return false
    end
    if jungleGrass[target:getId()] then
        target:transform(jungleGrass[target:getId()])
        target:decay()
        return true
    end

    if table.contains(wildGrowth, targetId) then
        toPosition:sendMagicEffect(CONST_ME_POFF)
        target:remove()
        return true
    end
    return destroyItem(player, target, toPosition)
end
 
You already created a variable for it, use it. You keep re-indexing the table instead of using your variable which makes it look ugly.
 
You also have a bug, it looks like the code would return false if you use it on a wildGrowth, since you have that line 11 check.

What Delusion is saying, is that you are declaring variables that you could be using instead of calling the ids every time

Code:
local wildGrowth = { 1499, 11099 }
local jungleGrass = {
    [2782] = 2781,
    [3985] = 3984,
    [19433] = 19431
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    --Getting the targetId
    local targetId = target.itemid
    -- First checking if its a wildGrowth
    if table.contains(wildGrowth, targetId) then
        toPosition:sendMagicEffect(CONST_ME_POFF)
        target:remove()
        return true
    end

    local grass = jungleGrass[targetId]
    if not grass then
        return false
    else
        target:transform(grass)
        target:decay()
        return true
    end
    
    --this condition will never get triggered, because of the if-else not grass
    return destroyItem(player, target, toPosition)
end
 
You also have a bug, it looks like the code would return false if you use it on a wildGrowth, since you have that line 11 check.

What Delusion is saying, is that you are declaring variables that you could be using instead of calling the ids every time

Code:
local wildGrowth = { 1499, 11099 }
local jungleGrass = {
    [2782] = 2781,
    [3985] = 3984,
    [19433] = 19431
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    --Getting the targetId
    local targetId = target.itemid
    -- First checking if its a wildGrowth
    if table.contains(wildGrowth, targetId) then
        toPosition:sendMagicEffect(CONST_ME_POFF)
        target:remove()
        return true
    end

    local grass = jungleGrass[targetId]
    if not grass then
        return false
    else
        target:transform(grass)
        target:decay()
        return true
    end
   
    --this condition will never get triggered, because of the if-else not grass
    return destroyItem(player, target, toPosition)
end

so shouldn't we move the return destroy item to line 20?
 
so shouldn't we move the return destroy item to line 20?
I have no idea what the original script used to do, if it’s supposed to call destroyItem if nothing from the table(either wildgrowth or grass)is found, removing the “if not grass” condition will be enough
 
I have no idea what the original script used to do, if it’s supposed to call destroyItem if nothing from the table(either wildgrowth or grass)is found, removing the “if not grass” condition will be enough
yes that is what it should call, but I was told by delusion to add the if not grass check otherwise the script would throw an error if used in a creature
 
Ah, then you should add an “if target is not an item then return false”. Adding “if not grass” would prevent all the items that aren’t in that array to reach that last return. The “if not an item” check should be the in first few lines of the function to prevent useless ifs
 
Ah, then you should add an “if target is not an item then return false”. Adding “if not grass” would prevent all the items that aren’t in that array to reach that last return. The “if not an item” check should be the in first few lines of the function to prevent useless ifs
hey this is too complicated to me 🤣 this is what I have and working right now:

Lua:
local wildGrowth = { 1499, 11099 }
local jungleGrass = {
    [2782] = 2781,
    [3985] = 3984,
    [19433] = 19431
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local targetId = target.itemid
    if table.contains(wildGrowth, targetId) then
        toPosition:sendMagicEffect(CONST_ME_POFF)
        target:remove()
        return true
    end

    local grass = jungleGrass[targetId]
    if not grass then
        return destroyItem(player, target, toPosition)
    else
        target:transform(grass)
        target:decay()
        return true
    end
    return true
end

but I think it has too many returns in it, doesn't?
 
hey this is too complicated to me 🤣 this is what I have and working right now:

Lua:
local wildGrowth = { 1499, 11099 }
local jungleGrass = {
    [2782] = 2781,
    [3985] = 3984,
    [19433] = 19431
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local targetId = target.itemid
    if table.contains(wildGrowth, targetId) then
        toPosition:sendMagicEffect(CONST_ME_POFF)
        target:remove()
        return true
    end

    local grass = jungleGrass[targetId]
    if not grass then
        return destroyItem(player, target, toPosition)
    else
        target:transform(grass)
        target:decay()
        return true
    end
    return true
end

but I think it has too many returns in it, doesn't?
The last return would never be reached, since there is an if-else that returns on both code blocks

Lua:
local wildGrowth = { 1499, 11099 }
local jungleGrass = {
    [2782] = 2781,
    [3985] = 3984,
    [19433] = 19431
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local targetId = target.itemid
    -- checking if there is an itemid, I haven't coded for OT in a while, so I am not sure if there is a better way to check if its an item
    if not targetId then
        return true
    end
   
    --If the item is in the wildGrowth array, we remove it and stop the code here (return true)
    if table.contains(wildGrowth, targetId) then
        toPosition:sendMagicEffect(CONST_ME_POFF)
        target:remove()
        return true
    end

    local grass = jungleGrass[targetId]
    --If the item is in the grass array, we transform it to its next state and stop the code here (return true)
    if grass then
        target:transform(grass)
        target:decay()
        return true
    end
    --If none of the previous conditions were met, we call destroyItem ( I don't know what that function does though)
    return destroyItem(player, target, toPosition)
end
 
Solution
Back
Top