Add Item to Inventory

Place to get help with not working mods / modding interface.
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1455
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Add Item to Inventory

Post by TheSAguy »

I'd like to add an item to the players inventory, once he researches a specific technology.

So, this should only happen once, upon researching the technology.

I'm assuming this would be done in the control.lua

Something like:

Code: Select all

if game.players.force.technologies["Tech_Name"].researched then
game.player.insert{name="iron-plate",count=1}
end
(the above code was not working)

I can't seem to figure it out.
Can someone possibly help me or point me to a mod that does this?
Where in control.lua should I place this and what is the correct syntax.
Thanks.
User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

Re: Add Item to Inventory

Post by prg »

Sounds like a job for the on_research_finished event. Check event.research.name, iterate over event.research.force.players to insert items.
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1455
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: Add Item to Inventory

Post by TheSAguy »

prg wrote:Sounds like a job for the on_research_finished event. Check event.research.name, iterate over event.research.force.players to insert items.
I believe you're right.
Has anyone actually used this? Like to see an example.

Thanks.
Choumiko
Smart Inserter
Smart Inserter
Posts: 1352
Joined: Fri Mar 21, 2014 10:51 pm
Contact:

Re: Add Item to Inventory

Post by Choumiko »

Code: Select all

game.on_event(defines.events.on_research_finished, function(event)
  if event.research.name == "character-logistic-trash-slots-1" then
    for _, player in pairs(event.research.force.players) do
      player.insert{name="iron-plate",count=1}
    end
  end
end)
Adds 1 iron plate to every player of the force that researched the tech
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1455
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: Add Item to Inventory

Post by TheSAguy »

Cool!,
Thanks Choumiko.
P.S. Love your mod.
Post Reply

Return to “Modding help”