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

How to do a user-defined dictionary?

Asked by
unmiss 337 Moderation Voter
8 years ago

Don't even ask me what that title is. I don't know what to call this.

You know how normal dictionaries work, right?

MyDictionary = {

['Setting1'] = "setting1"
}

and on and on.. but how do these 'user-defined' arrays/tables work? How would a script detect them? Let me take an example from the EISS Settings, in which you can define group ranks for certain types of admin and such:

['Ranks'] = {
{Group=124324,Rank=142,Type='Mod'},
{Group=1932348,Rank='Generals',Type='Owner'},
{Group=1284124,Rank=-193,Type='Admin'},
{Group=1399424,Rank=-1,Type='Banned'}
};

That's the example from it. Essentially, this 'Ranks' setting in the Settings has it's own subdictionary/array/whatever. You can keep adding {Group=,Rank=,Type=} forever. Can I get an example to use for this, specifically with a dictionary?

Like the scenario posted, you can keep adding these rank/group admin things into this 'Ranks' section without having to change the script that controls this. How would I refer back to things like 'Rank', 'Group', or 'Type'? Please explain in-depth if you can. This is boggling me. I don't even know the word to search when looking for an answer for this.

I'm taking a wild guess it might have to do with metatables/metamethods.

For the simplest explanation somebody could provide, maybe a thing like this that could add a textbutton? Like this:

['Buttons'] = {
{Name="Textbox1",Font="SourceSans",FontSize=24},
{Name="Textbox2",Font="SourceSansBold",FontSize=36}
}

Maybe with each of these textbuttons defined, it could move it down? Please make this realistic.

1 answer

Log in to vote
5
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
8 years ago

What you're representing is something called Multi-Dimensional Tables. Although, inside of your table you have a Dictionary.

To understand these, you should know how to index them. If you have a Dictionary like so;

local dictionary = {
    Goulstem = 'Cool dude',
    jackctaylor = 'Cooler dude'
}

Then, say you wanted to get the value assigned to the key 'Goulstem'. To do so then there are two ways.


  • 1) Parent - Child Relationship Indexing.

    The Parent - Child Relationship is where you use decimals to get a descending value.

--Define the value, using parent-child relationship
local val = dictionary.Goulstem

print(val) --> Cool dude
  • 2) Dictionary Indexing.

    The Dictionary Inexation is where you use a string to index the table

--Define the value using dictionary indexation.
local val = dictionary['jackctaylor']

print(val) --> Cooler dude

Now, for Multi-Dimensional Tables then it's just tables inside of tables. In your case, you can access them through standard table indexing.

local tab = {
    {Goulstem = 'Awesome'},
    {jackctaylor = 'Awesomer'}
}

So, if you have a dictionary inside of your multi-dimensional table then you can use the methods above.

print(tab[1].Goulstem) --> Awesome

So, in your case.. you pretty much just have to set up a dictionary with all the configurations/properties you want for the gui. Then you can iterate through them and create the gui.

local Buttons = {
    {
        Name = "Textbox1",
        Font = "SourceSans",
        FontSize = 24,
        Size = UDim2.new(1,0,1,0),
        Position = UDim2.new(0,0,0,0)
    },
    {
        Name = "Textbox2",
        Font = "SourceSansBold,
        FontSize = 36
        Size = UDim2.new(.5,0,.5,0),
        Position = UDim2.new(0.25,0,0.25,0)
    }
}

for _,tab in pairs(Buttons) do
    local gui = Instance.new('TextButton',script.Parent)
    for i,v in pairs(tab) do
        gui[i] = v
    end
end

Edit

You may be wondering about the line gui[i] = v. The pairs function returns two values, the index key and the value. The index key being something like 'Position', or 'Size' as it iterates through the dictionary. The value being UDim2.new(). So, if you do gui[i] = v then it's like saying gui['Position'] = UDim2.new().

0
Can you better explain line 21? unmiss 337 — 8y
1
Edited Goulstem 8144 — 8y
Ad

Answer this question