How to find item_request_proxy targeting an entity?

Place to get help with not working mods / modding interface.
robot256
Smart Inserter
Smart Inserter
Posts: 1224
Joined: Sun Mar 17, 2019 1:52 am
Contact:

How to find item_request_proxy targeting an entity?

Post by robot256 »

I have question about item_request_proxy entities, specifically the following scenario:

1. Player creates a blueprint with locomotive containing fuel item stacks.
2. Player places the blueprint, creating locomotive ghost with item_request property.
3. Construction bot places locomotive, creating locomotive entity and item_request_proxy entity with fuel targeting that locomotive.
4. Mod detects locomotive was created and replaces it with a nearly-identical locomotive entity, copying all parameters so the player doesn't notice the difference.
5. Old item_request_proxy target is invalid, new locomotive does not get fuel. Player notices the difference. :(

How does Mod know if there was an item_request_proxy targeting the locomotive it replaced? How can it create a new item_request_proxy with the same contents targeting the new locomotive entity?

The revive() function returns the associated item_request_proxy, but only if the script is the one reviving it. For non-script reviving, the on_built and on_robot_built_entity events do not appear to return a reference to any item_request_proxy entities created.

Do I have to search the entire list of entities, or at least item_request_proxy entities, to find out of any of them target the locomotive I am replacing? How would I do that?

Thanks in advance for your help.
My mods: Multiple Unit Train Control, RGB Pipes, Shipping Containers, Rocket Log, Smart Artillery Wagons.
Maintainer of Auto Deconstruct, Cargo Ships, Vehicle Wagon, Honk, Shortwave.
robot256
Smart Inserter
Smart Inserter
Posts: 1224
Joined: Sun Mar 17, 2019 1:52 am
Contact:

Re: How to find item_request_proxy targeting an entity?

Post by robot256 »

I thought that was the answer too, but it appears not. Entity.item_requests is a property of the ghost and item_request_proxy entites, but not built entities. The game crashes if you try to access it on the locomotive itself after it has been revived from a ghost even if it was valid on the ghost.
My mods: Multiple Unit Train Control, RGB Pipes, Shipping Containers, Rocket Log, Smart Artillery Wagons.
Maintainer of Auto Deconstruct, Cargo Ships, Vehicle Wagon, Honk, Shortwave.
User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3749
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: How to find item_request_proxy targeting an entity?

Post by DaveMcW »

Code: Select all

local proxies = locomotive.surface.find_entities_filtered{
  name = "item-request-proxy",
  force = locomotive.force,
  position = locomotive.position
}
for _, proxy in pairs(proxies) do
  if proxy.proxy_target == locomotive then
    game.print("proxy found")
  else
    game.print("proxy does not match")
  end
end
robot256
Smart Inserter
Smart Inserter
Posts: 1224
Joined: Sun Mar 17, 2019 1:52 am
Contact:

Re: How to find item_request_proxy targeting an entity?

Post by robot256 »

Thank you, this is just what I need! I was hoping there was a simpler way than searching every time (seems like a hole in the API), but using find_filtered_entities makes it pretty clean.
My mods: Multiple Unit Train Control, RGB Pipes, Shipping Containers, Rocket Log, Smart Artillery Wagons.
Maintainer of Auto Deconstruct, Cargo Ships, Vehicle Wagon, Honk, Shortwave.
Post Reply

Return to “Modding help”