eradicator wrote: Mon Oct 15, 2018 11:31 am
A notable drawback to the "merge method" is that you can not remove any keys. I.e. if you want to remove a flag or a whole property (i.e. collision_box), then with the merge method you have to remove that value after data:extend, while with deepcopy you can do it before. It's not functionally different, but i personally prefer the look of doing everything before data:extend. Oh, and merge also can't overwrite table values, which can be dangerous when you try to change..well, anything that's a table, i.e. collision_boxes, animations, fluid_boxes, flags, etcpp, because it'll just recursively merge those too. Overall i would not recommend merge until you have more modding experience.
Yes, it is a bit of a drawback. if you look at my Oil boiler example (it's in my 0.17 WIP power mod, so you can't actually look at it yet), First I do the util.merge method to copy Boiler MK2 (because I want stats to match that rather than the base boiler), and on one of those lines, is the following:
Code: Select all
minable = {result = "oil-boiler"},
and in this case, it will only replace the result tag in the minable table, it won't touch harness or mining_time, which is useful for this specific example, but no so much with energy_source. This is because it copies and merges tables within tables, so similar to the table.deepcopy method I define it after the util.merge like this:
Code: Select all
data.raw.boiler["oil-boiler"].energy_source =
{
type = "fluid",
//rest of the definition, etc.
}
and you can also blank out tags with = {} or = nil. outside of the util.merge too.
And note that I define it afterwards, not as part of the merge.
So you're right, it's not perfect, but the examples where you want to delete lines are usually the minority, not the majority.
The main difference between util.merge and table.deepcopy is that with deepcopy, you edit and delete tags in the same phase, after copying, but before adding to data.raw.
with merge, you do additions and tag replacements inside the merge, but then deletions or table replacements after it.
Also like deepcopy, you could actually save the result of util.merge to a variable then add it to data in the same way you do in deepcopy, but the difference is, because you're already changing the name in the merge, you CAN pipe the result straight into data:extend if you want to, and I usually do because most merges don't require additional editing. Boiler is one of the examples where I do change the table afterwards, and there have been a couple of cases where I set a tag to nil.