However, if you check the player's inventory for the item upon the event it isn't present. It will be found 1 tick later. This applies to items that go to the main inventory player tools inventory and quickbar.Called when the player crafts an item (upon inserting into player's inventory, not clicking the button to craft)
So I don't know if the API description is wrong or its a bug with the timing of the event firing within the Factorio core code?
Code run to test in game
Code: Select all
/c
itemName = "iron-chest"
ItemCrafted = function(event, text)
script.on_nth_tick(1, nil)
local player = game.players[event.player_index]
if event.item_stack.name ~= itemName then return end
local toolSearch = player.get_inventory(defines.inventory.player_tools).find_item_stack(itemName)
if toolSearch ~= nil then
game.print(text .. " : player tools : " .. itemName .. " found")
else
game.print(text .. " : player tools : " .. itemName .. " NOT found")
end
local inventorySearch = player.get_inventory(defines.inventory.player_main).find_item_stack(itemName)
if inventorySearch ~= nil then
game.print(text .. " : player main inventory : " .. itemName .. " found")
else
game.print(text .. " : player main inventory : " .. itemName .. " NOT found")
end
local quickbarSearch = player.get_inventory(defines.inventory.player_quickbar).find_item_stack(itemName)
if quickbarSearch ~= nil then
game.print(text .. " : player quickbar : " .. itemName .. " found")
else
game.print(text .. " : player quickbar : " .. itemName .. " NOT found")
end
end
QueueItemCrafted = function(event)
ItemCrafted(event, "instant")
script.on_nth_tick(1, function() ItemCrafted(event, "delayed") end)
end
script.on_event(defines.events.on_player_crafted_item, QueueItemCrafted)
- Started a new game
- Ran the above code
- Crafted an iron chest and it appears in my quickbar.
- logging on screen shows "instant" doesn't find the item in any of the inventories, the "delayed" does find it where it is.