Page 1 of 1

Add Item to Inventory

Posted: Wed Sep 09, 2015 3:34 am
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.

Re: Add Item to Inventory

Posted: Wed Sep 09, 2015 5:59 am
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.

Re: Add Item to Inventory

Posted: Thu Sep 10, 2015 8:28 pm
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.

Re: Add Item to Inventory

Posted: Thu Sep 10, 2015 8:46 pm
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

Re: Add Item to Inventory

Posted: Thu Sep 10, 2015 9:23 pm
by TheSAguy
Cool!,
Thanks Choumiko.
P.S. Love your mod.