Page 1 of 1

[Solved] Would it be possible to use a for loop here?

Posted: Sat Mar 26, 2016 10:07 pm
by simonsays476
Hello, my current recipe.lua looks like this:

Code: Select all

data:extend({

	--Tree 02C

	{
		type = "recipe",
		name = "facdeco-tree-02-c-pink",
		enabled = "false",
		ingredients = { {"iron-plate", 5}, {"plastic-bar", 5} },
		result = "facdeco-tree-02-c-pink"
	},
        {
                type = "recipe",
                name = "facdeco-tree-02-c-red",
                enabled = "false",
                ingredients = { {"iron-plate", 5}, {"plastic-bar", 5} },
                result = "facdeco-tree-02-c-red"
        },
        {
                type = "recipe",
                name = "facdeco-tree-02-c-orange",
                enabled = "false",
                ingredients = { {"iron-plate", 5}, {"plastic-bar", 5} },
                result = "facdeco-tree-02-c-orange"
        },
        {
                type = "recipe",
                name = "facdeco-tree-02-c-yellow",
                enabled = "false",
                ingredients = { {"iron-plate", 5}, {"plastic-bar", 5} },
                result = "facdeco-tree-02-c-yellow"
        },
        {
                type = "recipe",
                name = "facdeco-tree-02-c-green",
                enabled = "false",
                ingredients = { {"iron-plate", 5}, {"plastic-bar", 5} },
                result = "facdeco-tree-02-c-green"
        },
        {
                type = "recipe",
                name = "facdeco-tree-02-c-lime",
                enabled = "false",
                ingredients = { {"iron-plate", 5}, {"plastic-bar", 5} },
                result = "facdeco-tree-02-c-lime"
        },
        {
                type = "recipe",
                name = "facdeco-tree-02-c-pine",
                enabled = "false",
                ingredients = { {"iron-plate", 5}, {"plastic-bar", 5} },
                result = "facdeco-tree-02-c-pine"
        },
        {
                type = "recipe",
                name = "facdeco-tree-02-c-purple",
                enabled = "false",
                ingredients = { {"iron-plate", 5}, {"plastic-bar", 5} },
                result = "facdeco-tree-02-c-purple"
        },
})
My entity.lua and technology.lua looks similar as well. I was wondering for the future if it would have been possible to use a for loop to simplify all this repetition? Something like:

Code: Select all

for VAR = pink, red, orange, yellow, green, lime, pine, purple; do
        {
		type = "recipe",
		name = "facdeco-tree-02-c-[VAR]",
		enabled = "false",
		ingredients = { {"iron-plate", 5}, {"plastic-bar", 5} },
		result = "facdeco-tree-02-c-[VAR]"
	},
end for
If so could you guys point me to the exact syntax for it? I'm not really a programmer and I'm not quite sure if this is possible. Any help is appreciated.

Re: Would it be possible to use a for loop here?

Posted: Sat Mar 26, 2016 10:32 pm
by prg

Code: Select all

for _, color in pairs({"pink", "red", "orange", "yellow", "green", "lime", "pine", "purple"}) do
    data:extend({
        {
            type = "recipe",
            name = "facdeco-tree-02-c-"..color,
            enabled = "false",
            ingredients = { {"iron-plate", 5}, {"plastic-bar", 5} },
            result = "facdeco-tree-02-c-"..color
        }
    })
end

Re: Would it be possible to use a for loop here?

Posted: Sun Mar 27, 2016 1:44 am
by simonsays476
Thank you.