How to add items to existing tech?

Place to get help with not working mods / modding interface.
Post Reply
User avatar
bigyihsuan
Filter Inserter
Filter Inserter
Posts: 299
Joined: Thu Jan 01, 2015 12:57 pm
Contact:

How to add items to existing tech?

Post by bigyihsuan »

See title. I know for a fact that there's a way, but I don't remember what it is exactly.

Is it this?

Code: Select all

data.raw.technology['tech-name-here'].effects = table.append({
    type = 'recipe', --or whatever type this is
    name = 'name'
})

User avatar
bigyihsuan
Filter Inserter
Filter Inserter
Posts: 299
Joined: Thu Jan 01, 2015 12:57 pm
Contact:

Re: How to add items to existing tech?

Post by bigyihsuan »

Never mind, I found the solution, it's this:

Code: Select all

table.insert(data.raw["technology"]["tech-name"].effects, {type = "unlock-recipe",recipe = "item-name"}) --can only add one item

User avatar
bigyihsuan
Filter Inserter
Filter Inserter
Posts: 299
Joined: Thu Jan 01, 2015 12:57 pm
Contact:

Re: How to add items to existing tech?

Post by bigyihsuan »

Question, is there a more efficient way of mass-adding items to existing researches other than just spamming table.insert(data.raw['technology']['tech-name'], {type = unlock-whatever', name = 'whatever'})?

orzelek
Smart Inserter
Smart Inserter
Posts: 3911
Joined: Fri Apr 03, 2015 10:20 am
Contact:

Re: How to add items to existing tech?

Post by orzelek »

bigyihsuan wrote:Question, is there a more efficient way of mass-adding items to existing researches other than just spamming table.insert(data.raw['technology']['tech-name'], {type = unlock-whatever', name = 'whatever'})?
Take a look at bobs library mod - he created utility functions for stuff like this.

User avatar
bobingabout
Smart Inserter
Smart Inserter
Posts: 7352
Joined: Fri May 09, 2014 1:01 pm
Contact:

Re: How to add items to existing tech?

Post by bobingabout »

My library does need a lot of new things adding to it, but yes, it does allow easier ways to add items to technologies.

One thing that my library doesn't do though, but you can look at my mods as an example is...

Migration scripts!!!

Adding a new unlock to a technology is fine, but when you load an old save where the research is already unlocked, you'll find the item is not... For this, you need to write a migration script.

I don't currently have access to code, but it would look something like...

Code: Select all

for i, force in ipairs (game.forces) do
  if force.technology["technologyname"].researched == true then
    force.recipe["recipename"].enabled = true
  end
end
Like I said, no reference material to work from, that's off the top of my head, so is probably wrong.

You'd put that in the mod's migrations folder, with a name of your mod, with version number, .lua.


Note... what's the difference between pairs and ipairs?

ipairs will give the number from an indexed list in the first variable, followed by the directly usable address of the item (the whole force table of information in this case)
pairs works best if the table being iterated are tags with an = in them, they separate at the =, everything left of it is taken to be the element name and returned as the first variable, and everything on the right as the second variable.
Creator of Bob's mods. Expanding your gameplay since version 0.9.8.
I also have a Patreon.

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

Re: How to add items to existing tech?

Post by prg »

bobingabout wrote:Note... what's the difference between pairs and ipairs?

ipairs will give the number from an indexed list in the first variable, followed by the directly usable address of the item (the whole force table of information in this case)
pairs works best if the table being iterated are tags with an = in them, they separate at the =, everything left of it is taken to be the element name and returned as the first variable, and everything on the right as the second variable.
ipairs iterates over integer keys in increasing order, starting from 1, stopping at the first missing one, ignoring all other keys in the table.
pairs iterates over every key in a table in arbitrary order.
Both produce key, value tuples when used in a for.
All tables are key = value mappings.

ipairs(game.forces) won't do much, since game.forces contains string = LuaForce mappings, so no integer keys. You need to use pairs() here.
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!

Post Reply

Return to “Modding help”