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

Is There A Way To Condense These Tables?

Asked by 5 years ago

Not much to explain but is there any way to make this more efficient? Each succeeding line is the same but with one added value. There ought to be a way to make this take up less lines and space. Is there?

local tier1Food = {'FrenchBread'}

local tier2Food = {'FrenchBread', 'Muffin'}

local tier3Food = {'FrenchBread', 'Muffin', 'Donut'}

local tier4Food = {'FrenchBread', 'Muffin', 'Donut', 'ChocolateChipCookie'}

local tier5Food = {'FrenchBread', 'Muffin', 'Donut', 'ChocolateChipCookie', 'Bagel'}

local tier6Food = {'FrenchBread', 'Muffin', 'Donut', 'ChocolateChipCookie', 'Bagel', 'Coffee'}

local tier7Food = {'FrenchBread', 'Muffin', 'Donut', 'ChocolateChipCookie', 'Bagel', 'Coffee', 'Muffin'}

local tier8Food = {'FrenchBread', 'Muffin', 'Donut', 'ChocolateChipCookie', 'Bagel', 'Coffee', 'Muffin', 'ApplePie'}

local tier9Food = {'FrenchBread', 'Muffin', 'Donut', 'ChocolateChipCookie', 'Bagel', 'Coffee', 'Muffin', 'ApplePie', 'Croissant'}

local tier10Food = {'FrenchBread', 'Muffin', 'Donut', 'ChocolateChipCookie', 'Bagel', 'Coffee', 'Muffin', 'ApplePie', 'Croissant', 'VanillaCake'}

local tier11Food = {'FrenchBread', 'Muffin', 'Donut', 'ChocolateChipCookie', 'Bagel', 'Coffee', 'Muffin', 'ApplePie', 'Croissant', 'VanillaCake', 'Cupcake'}

local tier12Food = {'FrenchBread', 'Muffin', 'Donut', 'ChocolateChipCookie', 'Bagel', 'Coffee', 'Muffin', 'ApplePie', 'Croissant', 'VanillaCake', 'Cupcake', 'Tea'}

local tier13Food = {'FrenchBread', 'Muffin', 'Donut', 'ChocolateChipCookie', 'Bagel', 'Coffee', 'Muffin', 'ApplePie', 'Croissant', 'VanillaCake', 'Cupcake', 'Tea', 'ChocolateCake'}

local tier14Food = {'FrenchBread', 'Muffin', 'Donut', 'ChocolateChipCookie', 'Bagel', 'Coffee', 'Muffin', 'ApplePie', 'Croissant', 'VanillaCake', 'Cupcake', 'Tea', 'ChocolateCake', 'PumpkinPie'}

local tier15Food = {'FrenchBread', 'Muffin', 'Donut', 'ChocolateChipCookie', 'Bagel', 'Coffee', 'Muffin', 'ApplePie', 'Croissant', 'VanillaCake', 'Cupcake', 'Tea', 'ChocolateCake', 'PumpkinPie', 'BlueberryPie'}

local tier16Food = {'FrenchBread', 'Muffin', 'Donut', 'ChocolateChipCookie', 'Bagel', 'Coffee', 'Muffin', 'ApplePie', 'Croissant', 'VanillaCake', 'Cupcake', 'Tea', 'ChocolateCake', 'PumpkinPie', 'BlueberryPie', 'StrawberryPie'}

0
How are these tables being used? Are you using them to know what types of food(s) that the tiers can specifically access? DeveloperColton 229 — 5y
0
Thank you everyone who submitted answers. I apologize for being unclear about my intentions with the tables but every answer worked as I intended. BunnyFilms1 297 — 5y

4 answers

Log in to vote
0
Answered by 5 years ago

Hi Bunny,

You can simply store all of the foods in one table and make a system that returns the correct table according to the tier you want. Something like this:

local food_system = {

    foods = {'FrenchBread', 'Muffin', 'Donut', 'ChocolateChipCookie', 'Bagel', 'Coffee', 'Muffin', 'ApplePie', 'Croissant', 'VanillaCake', 'Cupcake', 'Tea', 'ChocolateCake', 'PumpkinPie', 'BlueberryPie', 'StrawberryPie'};

    tier_return = function(self, tier)
        local tab_to_return = {};

        for i = 1, tier do
            tab_to_return[i] = self.foods[i];
        end

        return tab_to_return;
    end
}

local tier_5_tab = food_system:tier_return(5);

for _, food in next, tier_5_tab do
    print(food);
end

Well, hope I helped and have a nice day/night.

Thanks,

Best regards,

~~ KingLoneCat

0
Note although this method is straightfoward, you create a new table every time you get a tier. This can eat up memory if you perform a large amount of "tier returns". To resolve this, create a table to store tiers that were already created, and check there first before creating a new one. Check out my answer for an example of this. amanda 1059 — 5y
0
But, this gets the tier table when it's needed. Not create one for each tier. Creating one for each tier can also eat up a lot of memory on a bigger scale. Also, the table I create inside of the function gets over-ridden and why would you need to keep creating a new table everytime you get a tier? Just make a reference to the tier table for later usage. So, you won't have to keep calling it. KingLoneCat 2642 — 5y
Ad
Log in to vote
1
Answered by
amanda 1059 Moderation Voter
5 years ago

Regardless of your purpose, you should only use one table, containing all the foods in order.

From there, it depends on what you want to do.

If you absolutely need a table for each tier, then examine my function below called getTier. It returns whichever tier you want, given a number in range.

Since I am not sure what you are doing, I cannot necessarily give you more than that without shooting in the dark.

What the function does, is either gets the existing tierTable, or creates it based off the number you put into the function.

local Food = {'FrenchBread', 'Muffin', 'Donut', 'ChocolateChipCookie', 'Bagel', 'Coffee', 'Muffin', 'ApplePie', 'Croissant', 'VanillaCake', 'Cupcake', 'Tea', 'ChocolateCake', 'PumpkinPie', 'BlueberryPie', 'StrawberryPie'}

local tierTables = {}

local function getTier(num)
    if num > 0 and num <= #Food then
        if tierTables[num] then
            return tieirTables[num]
        else
            tierTables[num] = {}
            for i = 1, num do
                tierTables[num][i] = Food[i]
            end
            return tierTables[num]
        end
    end
end

--Example usage, say I just wanted tier6
local tier6 = getTier(6)

If you have any questions let me know.

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

yes, but this is all you can do

local foods = {
 tier1Food = {'FrenchBread'}; -- use " ; " when putting tables in tables
tier2Food = {'FrenchBread', 'Muffin'};
tier3Food = {'FrenchBread', 'Muffin', 'Donut'};{'FrenchBread', 'Muffin', 'Donut', 'ChocolateChipCookie'};
tier5Food = {'FrenchBread', 'Muffin', 'Donut', 'ChocolateChipCookie', 'Bagel'};
tier6Food = {'FrenchBread', 'Muffin', 'Donut', 'ChocolateChipCookie', 'Bagel', 'Coffee'};
tier7Food = {'FrenchBread', 'Muffin', 'Donut', 'ChocolateChipCookie', 'Bagel', 'Coffee', 'Muffin'};
tier8Food = {'FrenchBread', 'Muffin', 'Donut', 'ChocolateChipCookie', 'Bagel', 'Coffee', 'Muffin', 'ApplePie'};
tier9Food = {'FrenchBread', 'Muffin', 'Donut', 'ChocolateChipCookie', 'Bagel', 'Coffee', 'Muffin', 'ApplePie', 'Croissant'};
tier10Food = {'FrenchBread', 'Muffin', 'Donut', 'ChocolateChipCookie', 'Bagel', 'Coffee', 'Muffin', 'ApplePie', 'Croissant', 'VanillaCake'};
tier11Food = {'FrenchBread', 'Muffin', 'Donut', 'ChocolateChipCookie', 'Bagel', 'Coffee', 'Muffin', 'ApplePie', 'Croissant', 'VanillaCake', 'Cupcake'};
tier12Food = {'FrenchBread', 'Muffin', 'Donut', 'ChocolateChipCookie', 'Bagel', 'Coffee', 'Muffin', 'ApplePie', 'Croissant', 'VanillaCake', 'Cupcake', 'Tea'};
tier13Food = {'FrenchBread', 'Muffin', 'Donut', 'ChocolateChipCookie', 'Bagel', 'Coffee', 'Muffin', 'ApplePie', 'Croissant', 'VanillaCake', 'Cupcake', 'Tea', 'ChocolateCake'};
tier14Food = {'FrenchBread', 'Muffin', 'Donut', 'ChocolateChipCookie', 'Bagel', 'Coffee', 'Muffin', 'ApplePie', 'Croissant', 'VanillaCake', 'Cupcake', 'Tea', 'ChocolateCake', 'PumpkinPie'};
tier15Food = {'FrenchBread', 'Muffin', 'Donut', 'ChocolateChipCookie', 'Bagel', 'Coffee', 'Muffin', 'ApplePie', 'Croissant', 'VanillaCake', 'Cupcake', 'Tea', 'ChocolateCake', 'PumpkinPie', 'BlueberryPie'};
tier16Food = {'FrenchBread', 'Muffin', 'Donut', 'ChocolateChipCookie', 'Bagel', 'Coffee', 'Muffin', 'ApplePie', 'Croissant', 'VanillaCake', 'Cupcake', 'Tea', 'ChocolateCake', 'PumpkinPie', 'BlueberryPie', 'StrawberryPie'} -- " ; " not needed on last line

}
-- to access these do

--foods.tier1Food
1
Uh, how is this "condensing" his tables? I'm pretty sure he wanted it so you don't need a table for each tier. DeveloperColton 229 — 5y
0
What he wanted was instead of a billion tables was to have one. Dog2puppy 168 — 5y
Log in to vote
0
Answered by
Dog2puppy 168
5 years ago

I didn’t read through it all, but I’m just assuming there’s another item for each rank. This was typed on an iPad, so it might not work.

local food = { "food", "food2", "etc"}
function GetAvaliableFood(rank)
     local foodAvaliable = {}
     for i=1, rank do
          table.insert(foodAvaliable, food[i])
     end
     return foodAvaliable
end

Answer this question