Page 1 of 1

How to add entries to existing prototype?

Posted: Wed Jan 28, 2015 2:11 am
by obuw
Hello all,

I'm trying to figure out how to add some stuff to an existing prototype. For instance, taking a technology like:

Code: Select all

  {
    type = "technology",
    name = "military-2",
    icon = "__base__/graphics/technology/military.png",
    effects =
    {
      {
        type = "unlock-recipe",
        recipe = "piercing-bullet-magazine"
      },
      {
        type = "unlock-recipe",
        recipe = "basic-grenade"
      }
    },
    prerequisites = {"military", "steel-processing"},
    unit =
    {
      count = 20,
      ingredients =
      {
        {"science-pack-1", 1},
        {"science-pack-2", 1}
      },
      time = 15
    },
    order = "e-a-b"
  },
and adding an additional unlock-recipe entry in the effects array like

Code: Select all

      {
        type = "unlock-recipe",
        recipe = "basic-grenade-2"
      }
I know I can just set the whole effects variable to a new array containing all 3 recipes, but that's not really ideal for compatibility so I just wanted to know if there is a cleaner way.

Thanks for the help. :)

Re: How to add entries to existing prototype?

Posted: Wed Jan 28, 2015 2:43 am
by prg
You could maybe use table.insert on effects.

Code: Select all

table.insert(data.raw["technology"]["military-2"].effects, {type=..., recipe=...})

Re: How to add entries to existing prototype?

Posted: Wed Jan 28, 2015 2:51 am
by FreeER
You use the table.insert function which takes the table to insert into and the table that is to be inserted and you access the prototype using data.raw [wiki] (sounds like you know how but I like to repeat for when people come across this later, like when they use the search feature), with the format data.raw["prototype_type"]["prototype_name"], if prototype_type or prototype_name don't have spaces or '-' (etc.) you can use the dot syntax (like data.raw) instead of the bracket syntax (data["raw"]).
So for the given example you'd use (full bracket syntax).

Code: Select all

table.insert(data.raw["technology"]["military-2"]["effects"], {type = "unlock-recipe", recipe = "basic-grenade-2"})
OR (dot syntax, where possible)

Code: Select all

table.insert(data.raw.technology["military-2"].effects, {type = "unlock-recipe", recipe = "basic-grenade-2"})
For that to take effect in already started games you'd also need to run player.force.technologies.resettechnologies() (all players share the same force if I recall correctly so you should only need to run it for one player, theoretically...), the best place for that is a lua file in a folder called migrations (a migration script, there's a wiki page but it just links to that post :))
original suggestion
edit: just saw a much cleaner way to do that from kovarex

Code: Select all

for force in pairs(game.forces) do
  force.technologies.resettechnologies()
end
edit: hm... kind of wondering what the average length of all my posts are... I seem to have trouble keeping them short :lol:

Re: How to add entries to existing prototype?

Posted: Wed Jan 28, 2015 1:31 pm
by obuw
Thanks a lot for the replies, looks like table.insert was what I was looking for. :-)

PS. Yep, I know about data.raw. Oh and I don't think your post is long, it's pretty concise and informative. =)


Now that I know about table.insert, I have another question. Is there a way to edit elements of an array? For instance, using the same example, how would I edit the piercing-bullet-magazine entry?

Would something like data.raw.technology["military-2"].effects[0].recipe = "piercing-bullet-replacement" work?

I guess even if it does work, it would be less than ideal, as it would no longer work if the ordering of elements are changed, and it would change another recipe. Is there some kind of array lookup command?

Oh, actually is there some kind of API I can look at to see all the commands I can use instead of asking these questions? :-)

Re: How to add entries to existing prototype?

Posted: Wed Jan 28, 2015 3:02 pm
by prg
A hardcoded index should work but could change. Guess you'd need to loop over all the entries in effects yourself and look for the right one to be sure.

Documentation I've been using so far:
https://forums.factorio.com/wiki/inde ... le=Modding
http://www.lua.org/manual/5.3/
http://lua-users.org/wiki/

Re: How to add entries to existing prototype?

Posted: Sat Apr 11, 2015 4:46 am
by CaptainC11
Which file do I add the table.insert to? I am making a simple mod to allow the barreling of all oil fractions. My guess is this goes into chemistry.lua inside my mod's prototypes directory.

something like:
data:extend({
{
table.insert(data.raw["technology"]["fluid-handling"]["effects"], {type = "unlock-recipe", recipe = "petroleum-gas-barrel"})
}

The mod works, but the barrels are unresearchable right now.

Re: How to add entries to existing prototype?

Posted: Sat Apr 11, 2015 5:40 am
by Natha
table.insert must be outside of data:extend({ }), because data:extend is a table and table.insert a command.

Re: How to add entries to existing prototype?

Posted: Wed Apr 29, 2015 3:15 am
by CaptainC11
Thanks, Natha! Got it working.