Page 1 of 1

How to add items to existing tech?

Posted: Sat Apr 30, 2016 1:45 am
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'
})

Re: How to add items to existing tech?

Posted: Sat Apr 30, 2016 3:40 pm
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

Re: How to add items to existing tech?

Posted: Sun May 01, 2016 8:05 pm
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'})?

Re: How to add items to existing tech?

Posted: Mon May 02, 2016 12:20 am
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.

Re: How to add items to existing tech?

Posted: Tue May 03, 2016 8:31 am
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.

Re: How to add items to existing tech?

Posted: Tue May 03, 2016 9:28 am
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.