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

String to Variant

zxmatzx

Advanced OT User
Joined
Dec 1, 2010
Messages
311
Solutions
27
Reaction score
154
Location
Brazil
GitHub
Mateuso8
Hello,
Im trying to convert a string to a variant that represents a table key

Pseudo-Code, im traveling and don't have access to real code, but its is similar.

I want to access "CONFIG_GERAL.equipsConfigs.armor_slots.slot_1", where the slot number changes and equipament type.

Lua:
--in lib file
CONFIG_GERAL = {
canWalk = true,
intervalAction = 1000,
healPercent = 20,
equipsConfigs = {
    armor_slots = {name = "armor", maxCount = 5, slot_1 = 5, slot_2 = 10, slot_3 = 5, slot_4 = 10, slot_5 = 5},
    helmet_slots = {name = "helmet", maxCount = 3, slot_1 = 5, slot_2 = 10, slot_3 = 5},
    boots_slots = {name = "boots", maxCount = 1, slot_1 = 5}
        },
enabled = true
}

function getSlots(equipType) 
    local baseTable = "CONFIG_GERAL.equipsConfigs."
    
    local tableIndex = -1
    for i = 0, #CONFIG_GERAL.equipsConfigs do
        if CONFIG_GERAL.equipsConfigs[i].name == equipType then
            tableIndex = i
            --break;
        end
    end

    if tableIndex == -1 then
        return 0
    end
    
    local equipMaxCount = CONFIG_GERAL.equipsConfigs[tableIndex].maxCount
    
    local totalValue = 0
    for i = 1, equipMaxCount do
        local tmpVar = Variant(baseTable .. "" .. equipType .. "_slots" .. ".slot_" .. i)
        totalValue = totalValue + tmpVar
    end

    return totalValue
end

Lua:
--In action file, called by onUse function
print(getSlots("armor"))
--expected output 35 (slot_1 = 5 + slot_2 = 10 + slot_3 = 5 + slot_4 = 10 + slot_5 = 5)

print(getSlots("helmet"))
--expected output 20 (slot_1 = 5 + slot_2 = 10 + slot_3 = 5)

print(getSlots("boots"))
--expected output 5 (slot_1 = 5)

I don't know if i explained in best way... Im using TFS 1.2.
Sorry my english, its not my main language.
Thank you.
 
Solution
You can do something like this :p

Lua:
--[[
enum slots_t : uint8_t {
    CONST_SLOT_WHEREEVER = 0,
    CONST_SLOT_HEAD = 1,
    CONST_SLOT_NECKLACE = 2,
    CONST_SLOT_BACKPACK = 3,
    CONST_SLOT_ARMOR = 4,
    CONST_SLOT_RIGHT = 5,
    CONST_SLOT_LEFT = 6,
    CONST_SLOT_LEGS = 7,
    CONST_SLOT_FEET = 8,
    CONST_SLOT_RING = 9,
    CONST_SLOT_AMMO = 10,

    CONST_SLOT_FIRST = CONST_SLOT_HEAD,
    CONST_SLOT_LAST = CONST_SLOT_AMMO,
};
]]

local CONFIG_GERAL = {
    canWalk = true,
    intervalAction = 1000,
    healPercent = 20,
    equipsConfigs = {
        [CONST_SLOT_HEAD] = {
            name = "helmet", 
            slots = {3, 5, 10, 5} 
        },
        [CONST_SLOT_ARMOR] = {
            name = "armor", 
            slots = {5, 10, 5, 10, 5} 
        }...
You can do something like this :p

Lua:
--[[
enum slots_t : uint8_t {
    CONST_SLOT_WHEREEVER = 0,
    CONST_SLOT_HEAD = 1,
    CONST_SLOT_NECKLACE = 2,
    CONST_SLOT_BACKPACK = 3,
    CONST_SLOT_ARMOR = 4,
    CONST_SLOT_RIGHT = 5,
    CONST_SLOT_LEFT = 6,
    CONST_SLOT_LEGS = 7,
    CONST_SLOT_FEET = 8,
    CONST_SLOT_RING = 9,
    CONST_SLOT_AMMO = 10,

    CONST_SLOT_FIRST = CONST_SLOT_HEAD,
    CONST_SLOT_LAST = CONST_SLOT_AMMO,
};
]]

local CONFIG_GERAL = {
    canWalk = true,
    intervalAction = 1000,
    healPercent = 20,
    equipsConfigs = {
        [CONST_SLOT_HEAD] = {
            name = "helmet", 
            slots = {3, 5, 10, 5} 
        },
        [CONST_SLOT_ARMOR] = {
            name = "armor", 
            slots = {5, 10, 5, 10, 5} 
        },
        [CONST_SLOT_FEET] = {
            name = "boots", 
            slots = {5} 
        }
    },
    enabled = true
}
    


function getSlots(equipType) 
    local tableIndex
    for slot = CONST_SLOT_HEAD, CONST_SLOT_AMMO do
        local table = CONFIG_GERAL.equipsConfigs[slot]
        if table and table.name == equipType then
            local totalValue = 0
            for i = 1, #table.slots do
                local tmpVar = table.slots[i]
                totalValue = totalValue + tmpVar
            end
            return totalValue
        end
    end
end

print(getSlots("armor")) -- 35
 
Solution
You can do something like this :p

Lua:
--[[
enum slots_t : uint8_t {
    CONST_SLOT_WHEREEVER = 0,
    CONST_SLOT_HEAD = 1,
    CONST_SLOT_NECKLACE = 2,
    CONST_SLOT_BACKPACK = 3,
    CONST_SLOT_ARMOR = 4,
    CONST_SLOT_RIGHT = 5,
    CONST_SLOT_LEFT = 6,
    CONST_SLOT_LEGS = 7,
    CONST_SLOT_FEET = 8,
    CONST_SLOT_RING = 9,
    CONST_SLOT_AMMO = 10,

    CONST_SLOT_FIRST = CONST_SLOT_HEAD,
    CONST_SLOT_LAST = CONST_SLOT_AMMO,
};
]]

local CONFIG_GERAL = {
    canWalk = true,
    intervalAction = 1000,
    healPercent = 20,
    equipsConfigs = {
        [CONST_SLOT_HEAD] = {
            name = "helmet",
            slots = {3, 5, 10, 5}
        },
        [CONST_SLOT_ARMOR] = {
            name = "armor",
            slots = {5, 10, 5, 10, 5}
        },
        [CONST_SLOT_FEET] = {
            name = "boots",
            slots = {5}
        }
    },
    enabled = true
}
   


function getSlots(equipType)
    local tableIndex
    for slot = CONST_SLOT_HEAD, CONST_SLOT_AMMO do
        local table = CONFIG_GERAL.equipsConfigs[slot]
        if table and table.name == equipType then
            local totalValue = 0
            for i = 1, #table.slots do
                local tmpVar = table.slots[i]
                totalValue = totalValue + tmpVar
            end
            return totalValue
        end
    end
end

print(getSlots("armor")) -- 35
Nice, this will work to me. Thank you.
I will mark as solved, if someone could explain why my code isn't working, and how to use properly Variant functions, i'll be very grateful.
 
Back
Top