insert items in active armor

Place to get help with not working mods / modding interface.
Post Reply
mophydeen
Filter Inserter
Filter Inserter
Posts: 529
Joined: Sun Nov 22, 2015 5:02 pm
Contact:

insert items in active armor

Post by mophydeen »

Is it possible to insert items into the armor?

Code: Select all

    player.insert{name="power-armor", count=1}
-- instead of in the player, imidiately inside the armor
    player.insert{name="personal-roboport-equipment", count=2}
    player.insert{name="fusion-reactor-equipment", count=1}

mophydeen
Filter Inserter
Filter Inserter
Posts: 529
Joined: Sun Nov 22, 2015 5:02 pm
Contact:

Re: insert items in active armor

Post by mophydeen »

Code: Select all

local armorname = "power-armor"
 	if player.get_inventory(defines.inventory.player_armor).can_insert(armorname) then
	    player.insert(armorname)
	    local armorSlot = player.get_inventory(defines.inventory.player_armor)
	    if (not armorSlot.is_empty()) then
            for j = 1, #armorSlot do
                if armorSlot[j].valid_for_read and armorSlot[j].name == armorname then
                    local armor = armorSlot[j]


                    game.print("armor active inventory  : " .. armor.get_inventory(defines.inventory.item_active))      -- CRASH HERE : LINE 99
                    game.print("armor main inventory  : " .. armor.get_inventory(defines.inventory.item_main))
                    
		        	local armorinventory = armor.get_inventory(defines.inventory.item_active)

		        	local freactor = {name="fusion-reactor-equipment", count=1}
				   	if armorinventory.can_insert(freactor) then
					    armorinventory.insert(freactor)
				   	end

			        local proboport = {name="personal-roboport-equipment", count=2}
					if armorinventory.can_insert(proboport) then
					    armorinventory.insert(proboport)
					end
				end
            end
        end
   	end

Code: Select all

Error while running event on_player_joined_game (ID 43)
Given item is not an ItemWithInventory.
stack traceback:
	...l/Downloads/factorio/temp/currently-playing/sl_utils.lua:99: in function 'givePlayerItems'
	...l/Downloads/factorio/temp/currently-playing/sl_utils.lua:151: in function 'givePlayerStarterItems'
	...l/Downloads/factorio/temp/currently-playing/sl_utils.lua:393: in function 'playerSpawnItems'
	...sl/Downloads/factorio/temp/currently-playing/control.lua:195: in function <...sl/Downloads/factorio/temp/currently-playing/control.lua:177

Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: insert items in active armor

Post by Nexela »

You need to use LuaGrid and .put

this is making assumptions on validity of slots, but it should point you in the right direction

Code: Select all

  local inv = player.get_inventory(defines.inventory.player_armor)
  inv.insert("power-armor-mk2")
  local armor = inv[1].grid
  armor.put{name="fusion-reactor-equipment"}
  armor.put{name="personal-roboport-equipment"}

end
Last edited by Nexela on Fri Jan 06, 2017 11:52 am, edited 2 times in total.

User avatar
Adil
Filter Inserter
Filter Inserter
Posts: 945
Joined: Fri Aug 15, 2014 8:36 pm
Contact:

Re: insert items in active armor

Post by Adil »

Armor has a grid, not inventory.

Code: Select all

   
if armor.grid then
    armor.grid.put({name="fusion-reactor-equipment"})
end
I do mods. Modding wiki is friend, it teaches how to mod. Api docs is friend too...
I also update mods, some of them even work.
Recently I did a mod tutorial.


User avatar
cpeosphoros
Inserter
Inserter
Posts: 40
Joined: Fri Dec 23, 2016 10:57 pm
Contact:

Re: insert items in active armor

Post by cpeosphoros »

Adil wrote:Armor has a grid, not inventory.

Code: Select all

   
if armor.grid then
    armor.grid.put({name="fusion-reactor-equipment"})
end
Have a look at https://mods.factorio.com/mods/arumba/A ... ated_Start code.

Basically, it does armor.grid.put, but has some nice tricks there.

Post Reply

Return to “Modding help”