Utility function: data.raw trawler

Topics and discussion about specific mods
Post Reply
User avatar
Taehl
Long Handed Inserter
Long Handed Inserter
Posts: 50
Joined: Sun Jan 18, 2015 2:23 am
Contact:

Utility function: data.raw trawler

Post by Taehl »

Working with Factorio's data.raw schema, it can be easy to change more than you intended. For example, if we wanted to change Productivity Module 3's infamously inconsistent recipe, we could simply write to our data.lua:

Code: Select all

data.raw.recipe["productivity-module-3"].ingredients =
    {
      {"productivity-module-2", 4},
      {"advanced-circuit", 5},
      {"processing-unit", 5},
      {"alien-artifact", 1}
    }
But what if another mod had already changed or added an ingredient? Our code would be incompatible with theirs, as it reverts the entire recipe. An iterative approach will help fix this situation. Instead of rewriting the entire table, we iterate through each item, check it, and change only the ones we intend to.

So for your convenience and code-hygiene, here's my Factorio data skimmer/updater function "trawl":

Code: Select all

function trawl( class, check, ... ) for k,item in pairs( class ) do return check( k, item, ... ) end end

Code: Select all

trawl( data.raw.recipe["productivity-module-3"].ingredients,
	function(k,item) if item[1] == "productivity-module-2" then item[2] = 4 end end
)

Post Reply

Return to “Mods”