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

How would I have multiple dictionary indexes have the same value?

Asked by 3 years ago

The title explains it all

this:

local Dictionary = {
    ["value"], ["val"] = 1 
}

does not work

any help is appreciated

2 answers

Log in to vote
1
Answered by 3 years ago

If you mean something like this:

local dict = nil
local Tab = {}
dict = {
    ["a"] = 10,
    ["TabTest"] = Tab,
    ["b"] = function() return dict['a'] end,
    ["c"] = function() return dict["a"],dict["b"] end,
    ["CopyOfTabInDict"] = function() return dict["TabTest"] end
}
print(dict['b'](),dict['c'](),Tab,dict["TabTest"],dict["CopyOfTabInDict"]())

TabTest is Tab, CopyOfTabInDict returns TabTest with the exact same memory address.

Really the best thing other than that is to manually loop or index outside the dictionary like so:

local dict = {}
dict.a = "hi"
dict.b = dict.a
print(dict.b)

or using a loop which would work too.

if you are aiming for something like this within a dictionary:

local a,b,c,d = 1,1,1,1

then you could make a function for it like this:

local dict = {}
local function CreateMultiple(dictionary,value,...)
    local Variables = {...} 
    for i, v in pairs(Variables) do
       dictionary[v] = value
    end
end

CreateMultiple(dict,10,"a","b","c","d")
print(dict.a,dict.b,dict.c,dict.d,dict.e) -- dict.e is nil, the rest is 10

You could try to do stuff with metatables however, it might be hard.

0
Thank you! The second option has worked the best for me seanieplays 88 — 3y
0
Thank you! The second option has worked the best for me seanieplays 88 — 3y
0
Of course, you are welcome, if you have any questions at all, let me know C: greatneil80 2647 — 3y
Ad
Log in to vote
1
Answered by
Speedmask 661 Moderation Voter
3 years ago
Edited 3 years ago

in lua you cannot assign two things to the same value at once. best you can do is value, val = 1, 1. this applies to variables and dictionary keys, haven’t tested if the aforementioned method works for dicts though. either define them separately, or if you have a large amount, use a loop to fill them.

Answer this question