{
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.
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).
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 )
for player in ipairs(game.players) do
player.force.technologies.resettechnologies()
break
-- remove the break if you notice any issues, just limits it to only doing one player
end
a potentially 'cleaner' way to do that (if you don't like the break basically) is
local index, player = next(game.players, nil)
player.force.technologies.resettechnologies()
where next takes a table and an index into that table and returns the next index, element pair (if the index is nil it gives the first index, value pair in the table). Next is used by both pairs and ipairs, just fyi
or you could be more "future proof" (for if/when players can eventually have different forces) and just run it for all players, it would only run once per save after all.
edit: just saw a much cleaner way to do that from kovarex
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
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.
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.