Define for prototype-type tree

Place to ask discuss and request the modding support of Factorio. Don't request mods here.
Post Reply
Boodals
Fast Inserter
Fast Inserter
Posts: 129
Joined: Sun Feb 11, 2018 7:10 pm
Contact:

Define for prototype-type tree

Post by Boodals »

In the data stage, I often need to find an entity or item by name in data.raw. As it can be of any type (eg. item, ammo, capsule, etc) I have to search through several different types in order to find it. I made a table and a helper function to make this easier:

Code: Select all

local ItemPrototypeTypes =
{
	["item"] = true,
	["ammo"] = true,
	["capsule"] = true,
	["gun"] = true,
	["item-with-entity-data"] = true,
	["item-with-label"] = true,
	--theres 20 lines of this.. and this is just for items, there's another for entities
}

local function FindPrototypeFromTypeSet(prototypeName, typeSet)
	for prototypeType, _ in pairs(typeSet) do
		local prototype = data.raw[prototypeType][prototypeName]
		if prototype ~= nil then
			return prototype
		end
	end
	return nil
end
Having to hardcode the PrototypeTypes table means that any new item or entity types that get added won't work without me remembering to add them to the tables. I want a native solution.

I propose a new define: defines.prototypeTypes, which is a tree mapping all prototype types to a table of their child types, and so on recursively, similar to the tree structure on the wiki.
As an example, defines.prototypeTypes["item"] would look like this:

Code: Select all

defines.prototypeTypes["item"] = {
	["ammo"] = {},
	["capsule"] = {},
	["gun"] = {},
	["item-with-entity-data"] = {},
	["item-with-label"] = {
		["item-with-inventory"] = {
			["blueprint-book"] = {},
		},
		["item-with-tags"] = {},
		["selection-tool"] = {
			["blueprint"] = {},
			["copy-paste-tool"] = {},
			["deconstruction-item"] = {},
			["upgrade-item"] = {},
		},
	},
	["module"] = {},
	["rail-planner"] = {},
	["tool"] = {
		["armor"] = {},
		["mining-tool"] = {},
		["repair-tool"] = {},
	},
}
One issue with this idea is abstract classes, as they don't have a name so they can't be included in the tree. I think the best solution for them would be to give them a name. For example, Equipment would be called "equipment", Smoke would have to be called "base-smoke" or something, as "smoke" already exists and type names should be unique.

By parsing this define, modders can make cached tables for their specific needs, and be assured that it will remain up to date.
Another use that this could solve is knowing whether an entity/item inherits a certain type. If I want to read an item's tags, but I'm not sure if it is an ItemWithTags, I could check its type in the tree to know whether or not calling get_tag would throw an error.

I tried making this myself with source access, but the game doesn't have a list of of all prototype types, nor a list of every prototype (you have to already know the type), so a more involved solution would have to be made.

Post Reply

Return to “Modding interface requests”