Some identifier linking mined entity events with mined item events

Place to ask discuss and request the modding support of Factorio. Don't request mods here.
Post Reply
sparr
Smart Inserter
Smart Inserter
Posts: 1408
Joined: Fri Feb 14, 2014 5:52 pm
Contact:

Some identifier linking mined entity events with mined item events

Post by sparr »

When on_player_mined_item fires, there seems to be no information available about the entity that was mined because it is gone. I would like for there to be some identifier in on_player_mined_entity and on_player_mined_item that allows them to be matched up, so that I can save some info about the entity while it still exists, to be referred to shortly afterward.

Bilka
Factorio Staff
Factorio Staff
Posts: 3299
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: Some identifier linking mined entity events with mined item events

Post by Bilka »

Use case?
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.

sparr
Smart Inserter
Smart Inserter
Posts: 1408
Joined: Fri Feb 14, 2014 5:52 pm
Contact:

Re: Some identifier linking mined entity events with mined item events

Post by sparr »

After the entity is removed I need to check the game state for changed belt connections, changed pipe connections, changed rail segments, etc.

sparr
Smart Inserter
Smart Inserter
Posts: 1408
Joined: Fri Feb 14, 2014 5:52 pm
Contact:

Re: Some identifier linking mined entity events with mined item events

Post by sparr »

Still wishing for this as I work on rewriting a mod.

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

Re: Some identifier linking mined entity events with mined item events

Post by Rseding91 »

Just don't use that event. The https://lua-api.factorio.com/latest/eve ... ned_entity event contains a LuaInventory with all the things that are going to end up in the event anyway.
If you want to get ahold of me I'm almost always on Discord.

sparr
Smart Inserter
Smart Inserter
Posts: 1408
Joined: Fri Feb 14, 2014 5:52 pm
Contact:

Re: Some identifier linking mined entity events with mined item events

Post by sparr »

Yes, but when that event fires the entity is still in the world, so none of my checks are valid yet. I need to run my checks after the entity is destroyed, and on_player_mined_item seems to be the only event that triggers at that point.

curiosity
Filter Inserter
Filter Inserter
Posts: 433
Joined: Wed Sep 11, 2019 4:13 pm
Contact:

Re: Some identifier linking mined entity events with mined item events

Post by curiosity »

Can sequences of these two events be interleaved? Doesn't seem so. Then it seems simple to match the events: all you need is a stack and a tick number.

There is also on_entity_destroyed.

sparr
Smart Inserter
Smart Inserter
Posts: 1408
Joined: Fri Feb 14, 2014 5:52 pm
Contact:

Re: Some identifier linking mined entity events with mined item events

Post by sparr »

Are you suggesting that when I mine two entities in the same tick I will always get the mined entity event for one, then the mined item event for the same one, then the mined entity event for the other, then the mined item event for the other? I'm not sure how to test this assumption; experiments would only be indicative, not proof.

Can the devs say (and document) this as a guarantee?

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

Re: Some identifier linking mined entity events with mined item events

Post by Rseding91 »

sparr wrote:
Tue May 07, 2024 3:19 pm
Can the devs say (and document) this as a guarantee?
That is currently how it works and I don't see any plans to change how it works.
If you want to get ahold of me I'm almost always on Discord.

curiosity
Filter Inserter
Filter Inserter
Posts: 433
Joined: Wed Sep 11, 2019 4:13 pm
Contact:

Re: Some identifier linking mined entity events with mined item events

Post by curiosity »

sparr wrote:
Tue May 07, 2024 3:19 pm
Are you suggesting that when I mine two entities in the same tick I will always get the mined entity event for one, then the mined item event for the same one, then the mined entity event for the other, then the mined item event for the other? I'm not sure how to test this assumption
experiments would only be indicative, not proof.

Code: Select all

/c for _, entity in pairs(game.player.surface.find_entities_filtered{type = 'tree'}) do game.player.mine_entity(entity) end
Oh, it seems that you will also have to handle the mining of tiles, since that also raises on_player_mined_item.
sparr wrote:
Tue May 07, 2024 3:19 pm
experiments would only be indicative, not proof.
You must assume there is some internal logic to the game, other way lies madness.

curiosity
Filter Inserter
Filter Inserter
Posts: 433
Joined: Wed Sep 11, 2019 4:13 pm
Contact:

Re: Some identifier linking mined entity events with mined item events

Post by curiosity »

Hmm... maybe this isn't so doable, after all. A mod might do something crazy to interrupt the mining process (destroy the entity, ban the player etc.), the item event might never be emitted. Best is, I guess, to check the top entity on the stack to see if it has been destroyed already, since that and not a 1-to-1 event matching is your real goal.

sparr
Smart Inserter
Smart Inserter
Posts: 1408
Joined: Fri Feb 14, 2014 5:52 pm
Contact:

Re: Some identifier linking mined entity events with mined item events

Post by sparr »

To be clear, my "real goal" here is to be able to identify what entity was just destroyed as soon as possible after the entity gets destroyed.

curiosity
Filter Inserter
Filter Inserter
Posts: 433
Joined: Wed Sep 11, 2019 4:13 pm
Contact:

Re: Some identifier linking mined entity events with mined item events

Post by curiosity »

sparr wrote:
Fri May 17, 2024 5:00 pm
To be clear, my "real goal" here is to be able to identify what entity was just destroyed as soon as possible after the entity gets destroyed.
Is it your mod's entity or some arbitrary one? Because in the latter case another mod can always screw you over and it will be within reason. Either you don't get any event by the time the entity is destroyed or it's destroyed after you decided it's not.
So what's your actual real goal? Why are you unsatisfied with the intended ways to detect this?

sparr
Smart Inserter
Smart Inserter
Posts: 1408
Joined: Fri Feb 14, 2014 5:52 pm
Contact:

Re: Some identifier linking mined entity events with mined item events

Post by sparr »

This is for https://mods.factorio.com/mod/belt-overflow

When a belt is destroyed, I need to recalculate whether any nearby belts might now be places items could back up. All of my calculations for that depend on the destroyed belt being gone, but it isn't when the destroyed event happens, and it is when the mined event happens, so I'd really like to run my check on the mined event.

curiosity
Filter Inserter
Filter Inserter
Posts: 433
Joined: Wed Sep 11, 2019 4:13 pm
Contact:

Re: Some identifier linking mined entity events with mined item events

Post by curiosity »

No, the entity is most definitely gone by the time the destroyed event happens. So just register every belt to that event and problem solved. Not like a tick of delay will make a noticeable difference to the player.

sparr
Smart Inserter
Smart Inserter
Posts: 1408
Joined: Fri Feb 14, 2014 5:52 pm
Contact:

Re: Some identifier linking mined entity events with mined item events

Post by sparr »

Sorry, I meant on_player_mined_entity, which fires right before the entity is destroyed.

So, using on_entity_destroyed, I could register every belt, and keep a table of their locations, then when one is destroyed I'd know where it was previously located? That's a good idea. A lot of data storage that won't get used, but it would work. That event didn't exist when I created this thread. I'll look into using it!

curiosity
Filter Inserter
Filter Inserter
Posts: 433
Joined: Wed Sep 11, 2019 4:13 pm
Contact:

Re: Some identifier linking mined entity events with mined item events

Post by curiosity »

...I mentioned it in my first post here, almost two months ago.

If you are concerned with memory use, you can register belts from inside on_player_mined_entity, though of course there are more ways an entity can be destroyed. So pick your poison.

sparr
Smart Inserter
Smart Inserter
Posts: 1408
Joined: Fri Feb 14, 2014 5:52 pm
Contact:

Re: Some identifier linking mined entity events with mined item events

Post by sparr »

How can I handle the situation where an entity gets moved, like with Picker Dollies or another mod or script?

Pi-C
Smart Inserter
Smart Inserter
Posts: 1716
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Some identifier linking mined entity events with mined item events

Post by Pi-C »

sparr wrote:
Mon Jul 01, 2024 10:43 am
How can I handle the situation where an entity gets moved, like with Picker Dollies or another mod or script?
Picker Dollies will raise a custom event when it's moving an entity. From its info page:
In your mods on_load and on_init events add:

Code: Select all

if remote.interfaces["PickerDollies"] and remote.interfaces["PickerDollies"]["dolly_moved_entity_id"] then
    script.on_event(remote.call("PickerDollies", "dolly_moved_entity_id"), your_function_to_update_the_entity)
end
The dolly moved event returns a table with the following the information:

Code: Select all

{
    player_index = player_index, -- The index of the player who moved the entity
    moved_entity = entity, -- The entity that was moved
    start_pos = position -- The position that the entity was moved from
}
In addition a remote api to disallow moving of an entity is available:

Code: Select all

if remote.interfaces["PickerDollies"] and remote.interfaces["PickerDollies"]["add_blacklist_name"] then
    remote.call("PickerDollies", "add_blacklist_name", "name-of-your-entity")
end
Other mods that move entities around should have their own custom events.
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

Post Reply

Return to “Modding interface requests”