I have a new custom entity that I would like to track all instances of. My mod needs to check all of them for various conditions on every few ticks, so I would like to be able to iterate over them quickly and easily.
I have a couple of options:
1. Keep a global collection of entities and update it on on_built_entity, on_robot_built_entity, all the mined events and so forth. Iterate over this every N ticks.
2. Run find_entities_filtered for the entire game board to find all entities of the given type.
I worry that 2 will take too long if run at high frequency in on_tick. Is that a bad practice? I worry with 1 that I will miss some events and end up with invalid items (which I can detect), or missing items (which I can't).
I don't feel like this is too unusual a scenario. I am looking at viewtopic.php?f=92&t=18102 and they use a version of approach #1, but it also bugs out with things like blueprint placed chests not getting registered properly.
How have other people approached this problem. Is there some sample code or a library for this? Thanks!
How to track all items of a type
-
- Burner Inserter
- Posts: 7
- Joined: Thu Mar 10, 2016 3:37 am
- Contact:
Re: How to track all items of a type
The problem with the mod you linked is that it simply doesn't handle on_robot_built_entity at all. Just do this and you should be fine. You certainly want to avoid running find_entities_filtered over every single generated chunk repeatedly.
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!
-
- Burner Inserter
- Posts: 7
- Joined: Thu Mar 10, 2016 3:37 am
- Contact:
Re: How to track all items of a type
Thanks, it seems to be working ok for me like that.