Page 1 of 1

Destination inventory for on_picked_up_item

Posted: Sat Jan 21, 2017 1:27 pm
by Sirenfal
I'd like this event to also have one of the inventory defines (http://lua-api.factorio.com/latest/defi ... .inventory) specifying where the item went.

Re: Destination inventory for on_picked_up_item

Posted: Sat Jan 21, 2017 4:01 pm
by Rseding91
The game doesn't know that until after it finishes the transfer and even then the place that fires the code doesn't know where it ended up.

Virtually nothing in the game knows information like that which makes it near impossible to add it to an event without a ton of reworking which I'm not willing to do.

Re: Destination inventory for on_picked_up_item

Posted: Sat Jan 21, 2017 4:24 pm
by Sirenfal
Sorry if I come across as rude, I'm just not a terribly social person. With respect, that information is contextual based on where the event is being fired, and since the event is fired after the item is already in the inventory I would think it should know that from where the dispatch is.

I know the event fires after the item was added to it's destination inventory because I can just look for it in the event handler itself:

Pastebin to make it easier to read: https://paste.whitefire.in/view/IzjqxogpC5

Code: Select all

script.on_event(defines.events.on_picked_up_item, function(event)

	if(event.item_stack.name ~= 'fre_factory_transit') then
		return
	end

	local player = game.players[event.player_index]

	local item = nil
	local check = {defines.inventory.player_main, defines.inventory.player_quickbar}
	local inven = nil

	for _, inv_id in pairs(check) do
		inven = player.get_inventory(inv_id)
		item = inven.find_item_stack('fre_factory_transit')

		if(item ~= nil) then
			break
		end
	end

	if(item == nil) then
		error('Failed to locate factory transit')
		return
	end
end)
So I would think your event's control flow (psuedo-code) looks something like...

Pastebin https://paste.whitefire.in/view/9GWmw8R7Gi

Code: Select all

-- recover the item this stack entity is holding
item_stack = entity.stack

if(item_stack.prototype.flags['goes-to-quickbar'] == true) then
	inventory = player.get_inventory(quickbar)
else
	inventory = player.get_inventory(hotbar)
end

if(inventory.can_insert(item_stack)) then 
	-- destroy the item_stack entity on the ground
	entity.destroy()

	-- place the item_stack in the inventory
	inventory.insert(transform_SimpleItemStack_to_LuaItemStack(item_stack))

	-- dispatch the event
	event.dispatch(defines.events.on_picked_up_item, item_stack=item_stack)
end
How is the event being fired in such a way that the item has already been placed into it's destination inventory, but the destination is somehow unknown to the event dispatch? Perhaps I'm missing something obvious, but I can't imagine how your event system works if this information isn't available at that point.

Asking for clarification purely out of curiosity about what's going on internally. If it's impossible to reasonably implement so be it :)


EDIT: Unrelated, maybe you should look into some kind of code highlighting plugin. It's pretty difficult to read a block here. I added Pastebin links.

Re: Destination inventory for on_picked_up_item

Posted: Sat Jan 21, 2017 4:50 pm
by Rseding91
What you've got there is a far simplified version of what actually ends up happening.
  • Say you've got this super simple setup: http://imgur.com/ZirZHoe
  • You press "F" to pick it up and it hits this line of code: http://imgur.com/YNHEwNi
  • That does a virtual dispatch to the class that owns the builder interface (because it can be owned by a standalone character, character attached to a player, or the god controller) to transfer the item into what ever wants it.
  • Deep inside the destination after several more virtual dispatches (because inventories have multiple types and where an item ends up depends on the item flags/health/durability/inventory state of whoever wants it (filters/existing items)) you end up here: http://imgur.com/cduZV2x
  • Then after all of the transfer is finished you go back to the builder interface and it does the event dispatch of the item if it was actually transferred and it has absolutely no idea where it ended up because that's not part of the code in that class.

Re: Destination inventory for on_picked_up_item

Posted: Sat Jan 21, 2017 4:57 pm
by Sirenfal
That makes sense. It'd take a lot of refactoring to clean that up.

Thanks for the explanation.

Re: Destination inventory for on_picked_up_item

Posted: Wed Feb 01, 2017 10:04 pm
by aubergine18
@Sirenfal see if this code is any help - I was mainly interested in when an item was dropped rather than picked up, but it could be refactored to work the other way, ie. when something is picked up.

viewtopic.php?p=213673#p213673

Re: Destination inventory for on_picked_up_item

Posted: Wed Feb 01, 2017 10:41 pm
by Sirenfal
aubergine18 wrote:@Sirenfal see if this code is any help - I was mainly interested in when an item was dropped rather than picked up, but it could be refactored to work the other way, ie. when something is picked up.

viewtopic.php?p=213673#p213673
I've been able to hack around these things successfully, it's just messy :P

In this case I just checked all the inventories the item could have gone into. I don't think it's really avoidable, sadly