How to add entries to existing prototype?

Place to get help with not working mods / modding interface.
Post Reply
obuw
Long Handed Inserter
Long Handed Inserter
Posts: 72
Joined: Tue May 06, 2014 7:49 pm
Contact:

How to add entries to existing prototype?

Post 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. :)
Obuw's Warfare - Combat improvements

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

Re: How to add entries to existing prototype?

Post by prg »

You could maybe use table.insert on effects.

Code: Select all

table.insert(data.raw["technology"]["military-2"].effects, {type=..., recipe=...})
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!

User avatar
FreeER
Smart Inserter
Smart Inserter
Posts: 1266
Joined: Mon Feb 18, 2013 4:26 am
Contact:

Re: How to add entries to existing prototype?

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

obuw
Long Handed Inserter
Long Handed Inserter
Posts: 72
Joined: Tue May 06, 2014 7:49 pm
Contact:

Re: How to add entries to existing prototype?

Post 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? :-)
Obuw's Warfare - Combat improvements

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

Re: How to add entries to existing prototype?

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

CaptainC11
Manual Inserter
Manual Inserter
Posts: 3
Joined: Sat Apr 11, 2015 4:36 am
Contact:

Re: How to add entries to existing prototype?

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

Natha
Fast Inserter
Fast Inserter
Posts: 177
Joined: Sun Mar 15, 2015 1:48 pm
Contact:

Re: How to add entries to existing prototype?

Post by Natha »

table.insert must be outside of data:extend({ }), because data:extend is a table and table.insert a command.

CaptainC11
Manual Inserter
Manual Inserter
Posts: 3
Joined: Sat Apr 11, 2015 4:36 am
Contact:

Re: How to add entries to existing prototype?

Post by CaptainC11 »

Thanks, Natha! Got it working.

Post Reply

Return to “Modding help”