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

Place to get help with not working mods / modding interface.
Post Reply
User avatar
LuziferSenpai
Filter Inserter
Filter Inserter
Posts: 333
Joined: Tue Jul 08, 2014 10:06 am
Contact:

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

Post 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
Last edited by LuziferSenpai on Sun Aug 28, 2016 2:45 pm, edited 1 time in total.
Coding is awesome!
Animes are love!
Factorio is life!

My MODs:
Click

Greetz,

Senpai

User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

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

Post by prg »

Yeah, the data table is not available at runtime. Try LuaForce::recipe instead.
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!

User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5150
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

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

Post 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)

User avatar
LuziferSenpai
Filter Inserter
Filter Inserter
Posts: 333
Joined: Tue Jul 08, 2014 10:06 am
Contact:

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

Post 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
Coding is awesome!
Animes are love!
Factorio is life!

My MODs:
Click

Greetz,

Senpai

User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

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

Post 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)
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!

User avatar
LuziferSenpai
Filter Inserter
Filter Inserter
Posts: 333
Joined: Tue Jul 08, 2014 10:06 am
Contact:

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

Post 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?
Coding is awesome!
Animes are love!
Factorio is life!

My MODs:
Click

Greetz,

Senpai

User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

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

Post 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.)
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!

User avatar
LuziferSenpai
Filter Inserter
Filter Inserter
Posts: 333
Joined: Tue Jul 08, 2014 10:06 am
Contact:

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

Post 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?
Coding is awesome!
Animes are love!
Factorio is life!

My MODs:
Click

Greetz,

Senpai

User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

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

Post 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]]
[...]
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!

User avatar
LuziferSenpai
Filter Inserter
Filter Inserter
Posts: 333
Joined: Tue Jul 08, 2014 10:06 am
Contact:

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

Post 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!
Coding is awesome!
Animes are love!
Factorio is life!

My MODs:
Click

Greetz,

Senpai

User avatar
Ober3550
Inserter
Inserter
Posts: 28
Joined: Sun Jan 15, 2017 9:01 am
Contact:

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

Post 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")


Post Reply

Return to “Modding help”