Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I get a value from a dictionary when the key is a value in another dictionary in Lua?

Asked by 5 years ago

I am writing some code where I have multiple dictionaries for my data. The reason being, I have multiple core objects and multiple smaller assets and the user must be able to choose a smaller asset and have some function off in the distance run the code with the parent noted.

An example of one of the dictionaries: (I'm working in ROBLOX Lua 5.1 but the syntax for the problem should be identical)

local data = {
    character = workspace.Stores.NPCs.Thom,
    name = "Thom", npcId = 9,
    npcDialog = workspace.Stores.NPCs.Thom.Dialog
}
local items = {
    item1 = {
        model = workspace.Stores.Items.Item1.Main,
        npcName = "Thom",
    }
}

This is my function:

local function function1(item)
    if not items[item] and data[items[item[npcName]]] then return false end
end

As you can see, I try to index the dictionary using a key from another dictionary. Usually this is no problem.

local thisIsAVariable = item[item1[npcName]]

but the method I use above tries to index the data dictionary for data that is in the items dictionary.

Without a ton of local variables and clutter, is there a way to do this? I had an idea to wrap the conflicting dictionary reference in a tostring() function to separate them - would that work?

Thank you.

0
crosspost x stackoverflow AdministratorReece 193 — 5y

Answer this question