Page 1 of 1

[DONE]I want to extract all Recipes in Game in Files

Posted: Sun Aug 28, 2016 11:13 am
by LuziferSenpai
Hey,

i want to get a file for each recipe in the Game.
Here is what i have written:

Code: Select all

script.on_event(defines.events.on_player_joined_game, function(event)
	for i, recipa in pairs(data.raw["recipe"]) do
	game.write_file("recipes/"..recipa.name..".txt", recipa)
	end
	end)
I tested both, i and recipa in the end.

And this i get in Game:
Click

Can anybody help me?

Greetz,

Luzifer

Re: I want to extract all Recipes in Game in Files

Posted: Sun Aug 28, 2016 11:26 am
by prg
Yeah, the data table is not available at runtime. Try LuaForce::recipe instead.

Re: I want to extract all Recipes in Game in Files

Posted: Sun Aug 28, 2016 11:26 am
by Klonan
You can't access data from in game,
Use game.players[1].force.recipes instead

Code: Select all

script.on_event(defines.events.on_player_joined_game, function(event)
    local player = game.players[event.player_index]
	for i, recipa in pairs(player.force.recipes) do
	game.write_file("recipes/"..recipa.name..".txt", recipa)
	end
	end)

Re: I want to extract all Recipes in Game in Files

Posted: Sun Aug 28, 2016 12:01 pm
by LuziferSenpai
Klonan wrote:You can't access data from in game,
Use game.players[1].force.recipes instead

Code: Select all

script.on_event(defines.events.on_player_joined_game, function(event)
    local player = game.players[event.player_index]
	for i, recipa in pairs(player.force.recipes) do
	game.write_file("recipes/"..recipa.name..".txt", recipa)
	end
	end)
Okay, if i make i instead of recipa i get the recipe name in the file, but i want the complett Recipe in the File.
If i use recipa i get this:
Click

Re: I want to extract all Recipes in Game in Files

Posted: Sun Aug 28, 2016 12:09 pm
by prg
Have a look at LuaRecipe and print the keys you care about (using serpent.block() on the whole thing just returns __self = "userdata: ..." but it might be useful for ingredients and products)

Re: I want to extract all Recipes in Game in Files

Posted: Sun Aug 28, 2016 2:12 pm
by LuziferSenpai
Okay, next i tryed this:

Code: Select all

script.on_event(defines.events.on_player_joined_game, function(event)
	local player = game.players[event.player_index]
	for i, recipa in pairs(player.force.recipes) do
		local recipadata = {}
		table.insert(recipadata, recipa.name)
		table.insert(recipadata, recipa.category)
		table.insert(recipadata, recipa.ingredients)
		table.insert(recipadata, recipa.products)
		table.insert(recipadata, recipa.energy)
		game.write_file("recipes/"..recipa.name..".txt", recipadata)
	end
	end)
And that doesnt work too. :/

Is it so complicatet to get the recipes?

Re: I want to extract all Recipes in Game in Files

Posted: Sun Aug 28, 2016 2:22 pm
by prg
You need to pass a string to write_file, not a table. serpent.block() can turn tables into strings for you (but it doesn't work on custom types like LuaRecipe so you need to know which keys to look for yourself.)

Re: I want to extract all Recipes in Game in Files

Posted: Sun Aug 28, 2016 2:26 pm
by LuziferSenpai
prg wrote:You need to pass a string to write_file, not a table. serpent.block() can turn tables into strings for you (but it doesn't work on custom types like LuaRecipe so you need to know which keys to look for yourself.)
Can you give me an exsamble pls?

Re: I want to extract all Recipes in Game in Files

Posted: Sun Aug 28, 2016 2:34 pm
by prg

Code: Select all

for _, recipe in pairs(game.forces.player.recipes) do
    local foo = {
        name = recipe.name,
        category = recipe.category,
        ingredients = recipe.ingredients,
        products = recipe.products,
    }
    print(serpent.block(foo))
end
->

Code: Select all

{
  category = "crafting",
  ingredients = {
    {
      amount = 100,
      name = "plastic-bar",
      type = "item"
    } --[[table: 0x7f069c265510]],
    {
      amount = 200,
      name = "advanced-circuit",
      type = "item"
    } --[[table: 0x7f069c2761c0]],
    {
      amount = 20,
      name = "electric-engine-unit",
      type = "item"
    } --[[table: 0x7f069c276350]],
    {
      amount = 100,
      name = "battery",
      type = "item"
    } --[[table: 0x7f069c2763b0]]
  } --[[table: 0x7f069c265270]],
  name = "small-plane",
  products = {
    {
      amount = 1,
      name = "small-plane",
      type = "item"
    } --[[table: 0x7f069c276410]]
  } --[[table: 0x7f069c291b20]]
} --[[table: 0x7f069c268ce0]]
{
  category = "crafting",
  ingredients = {
    {
      amount = 10,
      name = "electronic-circuit",
      type = "item"
    } --[[table: 0x7f069c22d020]],
    {
      amount = 5,
      name = "iron-gear-wheel",
      type = "item"
    } --[[table: 0x7f069c22cf50]],
    {
      amount = 1,
      name = "iron-plate",
      type = "item"
    } --[[table: 0x7f069c26eef0]]
  } --[[table: 0x7f069c18d890]],
  name = "player-port",
  products = {
    {
      amount = 1,
      name = "player-port",
      type = "item"
    } --[[table: 0x7f069c26f0a0]]
  } --[[table: 0x7f069c26f040]]
} --[[table: 0x7f069c18d710]]
[...]

Re: I want to extract all Recipes in Game in Files

Posted: Sun Aug 28, 2016 2:45 pm
by LuziferSenpai
Okay, i have made a command out of it:

Code: Select all

/c for i, recipa in pairs( game.player.force.recipes ) do local recipadata = { name = recipa.name, category = recipa.category, ingredients = recipa.ingredients, products = recipa.products, energy = recipa.energy } game.write_file( "recipes/"..recipa.name..".txt", serpent.block( recipadata ) ) end
This works perfectly.

Ty for help Guys!

Re: [DONE]I want to extract all Recipes in Game in Files

Posted: Tue Jul 17, 2018 11:57 am
by Ober3550
Just gonna leave this here for anyone else who comes along

Code: Select all

/c local recipas = {} for _, recipa in pairs( game.player.force.recipes ) do 
local recipadata = 
{ 
name = recipa.name, 
category = recipa.category, 
ingredients = recipa.ingredients,
products = recipa.products, 
energy = recipa.energy 
} 
recipas[#recipas+1]=serpent.block( recipadata )
end
game.write_file("recipe.txt", serpent.block(recipas).."/n")

Re: [DONE]I want to extract all Recipes in Game in Files

Posted: Tue Jul 17, 2018 12:11 pm
by darkfrei
Or just read the wiki console command
https://wiki.factorio.com/Console#Write ... es_to_file