Factorio-Stdlib icon indicating copy to clipboard operation
Factorio-Stdlib copied to clipboard

Data.Util.Duplicate on normal/expensive recipes

Open kryzeth opened this issue 5 years ago • 6 comments

Not sure if I'm not using it right, but when I try using duplicate on a recipe that has normal/expensive variants, the result of those normal/expensive recipes are not set to the new name, but kept as the old name.

However, when using duplicate on a simple recipe that does not have difficulty defined, it works as I think is intended, where the result is set to the new name. Example code below:

local Data = require('__stdlib__/stdlib/data/data')
data:extend{(Data.Util.duplicate("recipe", "electric-mining-drill", "test-mining-drill"))}
error(serpent.block(data.raw.recipe["test-mining-drill"]))

kryzeth avatar Jun 10 '19 06:06 kryzeth

Data.Util.duplicate is old school code

See if this does what you want

Data(electric-mining-drill):copy('test-mining-drill)

No need to extend as Data handles all that

Keep in mind the Data Lib is still very much WIP (and looking for developers to assist!) and doesn't fully handle results in a lot of cases

Nexela avatar Jun 11 '19 02:06 Nexela

Ah. That... completely changes how I have to do things. I wrote all of my current code on the assumption that I could edit the things before pushing them into the global table. At least I wasn't done yet, so rewriting everything won't be too bad, I suppose.

But yeah, Data.Util.duplicate was just the only thing I could find (and use) from the API. I still haven't figured out how to use the colon expressions, like the one you provided. Which also gives an error, but seems I just had to provide the data type (item, recipe, mining-drill, etc)

But it does seem to work as intended, setting the normal/expensive result properly.

kryzeth avatar Jun 11 '19 03:06 kryzeth

Lua is just table references, Data.raw is nothing special just a table. data:extend is just a function with very limited error checking that puts your table into data.raw in the correct spot. You can push whatever you want to it. and as long as when you are finished pushing to it it has everything it needs you won't have a problem.

The colon is just a shortcut syntax it passes the table (Data or more realistly the table created by Data as its first argument) Using what you are familiar with data:extend({my_entity}) is just a shortcut for data.extend(data, {my_entity})

local test_recipe = Data('electric-mining-drill', 'recipe'):copy('test-mining-drill')
-- Its created, and pushed but that doesn't mean we have to be done with it.
test_recipe.energy_required = 200

There are even shortcuts if needed (ps just examples not even sure if it supports everything I put here)

Data('electric-mining-drill', 'recipe'):copy('test-mining-drill'):set_field('energy_required', 200):add_flag('hidden').change_this = 2

Nexela avatar Jun 11 '19 04:06 Nexela

Also Data can take a table in which case it doesnt need the second paramater as long as the table contains it (it also won't extend an already extended table, not that it matters)

Data(data.raw.recipe['electric-mining-drill']):copy('test-mining-drill') -- the copy will extend it.

Nexela avatar Jun 11 '19 04:06 Nexela

Woah... that second example seems close to what I might need. So in that case, set_field is changing the value of 'energy_required', which is located within the newly created 'test-mining-drill' recipe? Hmm....

But wow, I never actually knew that. I just assumed data:extend({whatever}) was just the way Factorio added things. Thanks for the help! Also.. I also don't really use github, so not sure if I'm supposed to close this.. or something?

kryzeth avatar Jun 11 '19 04:06 kryzeth

We can leave it open, I might actually update duplicate :P

Nexela avatar Jun 11 '19 23:06 Nexela