Module:Recipe: Difference between revisions

From Subnautica Wiki
(switching to ipairs)
m (slightly customizing the output)
Line 20: Line 20:
if data[prod].recipe then
if data[prod].recipe then
for i,v in ipairs(data[prod].recipe) do
for i,v in ipairs(data[prod].recipe) do
output = output .. v[1] .. " -- " .. v[2] .. "<br>"
output = output .. "[[File:" .. data[v[1]].image .. "|50px]] " .. data[v[1]].name .. " -- x" .. v[2] .. "<br>"
end
end
return output
return output

Revision as of 22:59, 8 June 2022

This module standardizes the display of crafting recipes and makes it easy to display a specified item's recipe or all recipes that use a specified ingredient. See Template:RecipeNew for more information. Thanks to BryghtShadow for helping out with the loops!


local p = {}
local data = mw.loadData('Module:Recipe/data')
local getArgs = require('Dev:Arguments').getArgs

function p.main(frame)
	local args = getArgs(frame)
	local prod
	local output = ""
	
	if args[1] then
		prod = args[1]
	else
		prod = mw.title.getCurrentTitle().text
	end
	
	if data[prod] then
		if data[prod].original then
			error ("'" .. prod .. "' is not crafted standalone; use '" .. data[prod].original .. "' instead")
		else
			if data[prod].recipe then
				for i,v in ipairs(data[prod].recipe) do 
					output = output .. "[[File:" .. data[v[1]].image .. "|50px]] " .. data[v[1]].name .. " -- x" .. v[2] .. "<br>"
				end
				return output
			else
				error ("'" .. prod .. "' is not crafted; use Template:Uses instead")
			end
		end
	else
		error ("'" .. prod .. "' recipe does not exist")
	end
end

return p