Structuring Item and Crafting Station Dictionaries

My experience will contain a multitude of different items players can obtain and craft. Most, if not all are considered crafting materials that you may need to combine with each other at crafting stations to make other items.

This is inspired by TigerCode’s 2011 Roblox game, Christmas Rush, where you are able to grab crafting materials and go to different stations that are used to craft certain items.

I want to achieve something like this, but I’m not exactly sure how to structure everything where it can be a breeze to add more items and stations in the future.

This is one of my most recent drafts.

--[[
This Item module is mostly used for the client when looking into their inventories
and hovering over items for info, but it is also referred to by the Station module
to confirm an item exists.
]]
local item = {
	["UnpaintedSmallPart"] = {
		Name = "Unpainted Small Part",
	},
	["UnpaintedMediumPart"] = {
		Name = "Unpainted Medium Part",
	},
	["SmallPart"] = {
		Name = "Small Part",
	},
	["MediumPart"] = {
		Name = "Medium Part",
	},
}

return item
local station = {
	["GlueStation"] = {
		-- All the things this station can craft
		
		["UnpaintedMediumPart"] = {
			-- All the materials required to craft this object
			"UnpaintedSmallPart",
			"UnpaintedSmallPart"
		},
		["MediumPart"] = {
			"SmallPart",
			"SmallPart"
		},
	},
	
	["PaintStation"] = {
		["SmallPart"] = {
			"UnpaintedSmallPart"
		},
		["MediumPart"] = {
			"UnpaintedMediumPart"
		},
	},
}

return station

Items will have many different states it can be in, such as unpainted and painted as seen above. I’m planning on adding more states such as glowy, sparkly, refined, etc. Though as you can imagine, this will abhorrently fill up as time goes on.

I’ve been stuck on this for a while and my brain can’t seem to comprehend and come up with how I could make this more efficient and not mind numbing. Help appreciated!

1 Like

Maybe like this:

local Recipes = {
	[1] = {
		["Stations"] = {"PaintStation"}, --Which stations can it be made at
		["Materials"] = {{"UnpaintedFragment", 3}, "PaintBrush"}, --Which items are required
		["Results"] = {"PaintedBrick"}, --Which items are produced
	},
}

function ReplaceRepeatItems(Table)
  --Replace {{"item", 3}} with {"item", "item", "item"}
  for i2, Item in ipairs(Table) do
    if (type(Item) == "table") then
      --Remove the sub table ({"item", count})
      table.remove(Table, i2)
      --Replace with item to repeat
      repeat_item = Item[1]
      times = Item[2]
      for i3 = 1, times do
        table.insert(Table, i2, repeat_item)
      end
    end
  end
end
for i, Recipe in ipairs(Recipes) do
  ReplaceRepeatItems(Recipe["Materials"])
  ReplaceRepeatItems(Recipe["Results"])
end

return Recipes

Maybe something like this?

local item = {
	["SmallPart"] = {
		Name = "Small Part",
		States = {"Unpainted"}
	},
	["MediumPart"] = {
		Name = "Medium Part",
		States = {"Unpainted"}
	},
}

return item

and then you run this code server side on the table

for index, values in pairs(item) do
	if not values.States then
		continue
	end

	if #values.States == 0 then
		continue
	end

	for _, state in pairs(values.States) do
		local newItemIndex = state .. index
		newItemIndex = newItemIndex:gsub(" ","") --remove all spaces
		local stateValue = {
			Name = state .. " " .. values.Name
		}

		item[newItemIndex] = stateValue
	end

	values.States = nil
end

For this example the item table would be converted to:

{
	["UnpaintedSmallPart"] = {
		Name = "Unpainted Small Part",
	},
	["UnpaintedMediumPart"] = {
		Name = "Unpainted Medium Part",
	},
	["SmallPart"] = {
		Name = "Small Part",
	},
	["MediumPart"] = {
		Name = "Medium Part",
	},
}

With this you can have many states without cluttering up code, such as:

local item = {
	["SmallPart"] = {
		Name = "Small Part",
		States = {"Shiny", "Rocky", "Unpainted", "Painted With Green"}
	}
}