Page 1 of 1

Utility function: data.raw trawler

Posted: Wed Sep 14, 2016 12:34 am
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
)