How to use table contents within table itself?

I have a dictionary where all item data in the game is stored. I wanna be able to use contents from the dictionary within the dictionary itself.

Below is an example:

local Items = {
	
	--Ores
	Copper = {
		["Info"] = {
			Name = "Copper Ore";
		};
	};
	
	--Bars
	CopperBar = {
		["Info"] = {
			Name = "Copper Bar";
		};
		
		["Recipes"] = {
			Recipe1 = {
				Materials = {
					{Items.Copper, 4}; --The first element of this table
				};
				Station = {"crafting station"};
			};
		}

	};

return Items

I want to use Items.Copper but that causes an error.

1 Like

You can define the table and add gradually:

local Items = {}
-- Ores
Items.Copper = {
    ["Info"] = {
        Name = "Copper Ore";
    };
}
-- Bars and recipes
Items.Bars = {
    CopperBar = {
        ["Info"] = {
            Name = "Copper Bar";
        };
        
        ["Recipes"] = {
            Recipe1 = {
                Materials = {
                    {Items.Copper, 4};
                };
                Station = {"crafting station"};
            };
        };
    };
}

Or you could restructure or split the table, for instance, by making Recipes its own table and work with two.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.