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

A better way to force dictionaries to go in order?

Asked by
Azarth 3141 Moderation Voter Community Moderator
10 years ago

Is there a better way I could make my dictionary go in order without having to refer to another table?

player_commands = {

    settings = {
        button_size = UDim2.new(0, 95,0, 30);
        max_col = 4;
        dir = options.players_commands;
        padding = 2;
        order = {

            'Kick';
            'Kill';
            'Explode';
            'Burn';
            'Load';
            'Tp_to_me';
            'Tp_to';
        }
    };

    commands = {

        Kick = function()

        end;

        Kill = function()

        end;

        Explode = function()

        end;

        Burn = function()

        end;

        Load = function()

        end;

        Tp_to_me = function()

        end;

        Tp_to = function()

        end;

    }




}

do
    local col       = 0 
    local row       = 0
    local count     = 0 

    for a,b in pairs(player_commands.settings.order) do
        local v = player_commands.commands[b]
        -- Have to use this to go in order
        local button = create("TextButton")
        {
            Text = b;
            Name = b;
            BackgroundTransparency = (count % 2 == 0 and .3 or .5);
            BorderSizePixel = 0;
            Size = player_commands.settings.button_size;
            Font = "Legacy";
            FontSize = "Size12";
            TextColor3 = Color3.new(1,1,1);
            Position = UDim2.new(
                0,5 + ( (player_commands.settings.button_size.X.Offset * col ) + (col * player_commands.settings.padding)) , 
                0,5 + ( (player_commands.settings.button_size.Y.Offset * row ) + (row *  player_commands.settings.padding))); 
            Parent = player_commands.settings.dir

        }
        row = (col + 1 == player_commands.settings.max_col and row + 1 or row)
        col = (col + 1 == player_commands.settings.max_col and 0 or col + 1)
        count = count + 1
    end 
end
0
The reason it's not going in a specific order is because the commands table does not have integer keys. You could modify the script so that each key/value pair is in its own table within the commands table. That way each command has an integer key, which allows it to go in the correct order. 2eggnog 981 — 10y
0
I know, that's why ipairs is useless in this case. I was just wondering if there was something else I didn't know about. Azarth 3141 — 10y
0
Well, Lua stores non-integer keys in a hash table, so I doubt it's possible to preserve the order. 2eggnog 981 — 10y

1 answer

Log in to vote
2
Answered by
jakedies 315 Trusted Moderation Voter Administrator Community Moderator
10 years ago

As 2eggnog said, sadly it's not possible. But you can do something like:

commands = {
{"Kick"; function() end};
{"Kill"; function() end};

And so on to preserve order, but you will not be able to check if a command exists directly by key.

Ad

Answer this question