Destination inventory for on_picked_up_item

Things that we aren't going to implement
Post Reply
User avatar
Sirenfal
Long Handed Inserter
Long Handed Inserter
Posts: 50
Joined: Tue Jan 10, 2017 1:42 am
Contact:

Destination inventory for on_picked_up_item

Post 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.

Rseding91
Factorio Staff
Factorio Staff
Posts: 13235
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: Destination inventory for on_picked_up_item

Post 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.
If you want to get ahold of me I'm almost always on Discord.

User avatar
Sirenfal
Long Handed Inserter
Long Handed Inserter
Posts: 50
Joined: Tue Jan 10, 2017 1:42 am
Contact:

Re: Destination inventory for on_picked_up_item

Post 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.

Rseding91
Factorio Staff
Factorio Staff
Posts: 13235
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: Destination inventory for on_picked_up_item

Post 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.
If you want to get ahold of me I'm almost always on Discord.

User avatar
Sirenfal
Long Handed Inserter
Long Handed Inserter
Posts: 50
Joined: Tue Jan 10, 2017 1:42 am
Contact:

Re: Destination inventory for on_picked_up_item

Post by Sirenfal »

That makes sense. It'd take a lot of refactoring to clean that up.

Thanks for the explanation.

User avatar
aubergine18
Smart Inserter
Smart Inserter
Posts: 1264
Joined: Fri Jul 22, 2016 8:51 pm
Contact:

Re: Destination inventory for on_picked_up_item

Post 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
Better forum search for modders: Enclose your search term in quotes, eg. "font_color" or "custom-input" - it prevents the forum search from splitting on hypens and underscores, resulting in much more accurate results.

User avatar
Sirenfal
Long Handed Inserter
Long Handed Inserter
Posts: 50
Joined: Tue Jan 10, 2017 1:42 am
Contact:

Re: Destination inventory for on_picked_up_item

Post 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

Post Reply

Return to “Won't implement”