Define for prototype-type tree
Posted: Sat Sep 14, 2019 10:26 pm
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:
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:
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.
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
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"] = {},
},
}
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.