using find_entities_filtered to find Prototype/EntityWithHealth

Place to get help with not working mods / modding interface.
User avatar
adamwong246
Fast Inserter
Fast Inserter
Posts: 148
Joined: Tue Dec 01, 2020 5:01 am
Contact:

using find_entities_filtered to find Prototype/EntityWithHealth

Post by adamwong246 »

Is it possible to use that function to find all entities with health?

Code: Select all

local entities = nauvisSurface.find_entities_filtered({area = area, type = "wall" })
works to get all walls but

Code: Select all

local entities = nauvisSurface.find_entities_filtered({area = area, type = "entity_with_health" })
does not.

I sure hope I don't have to hardcode a list of every Class which implements `Prototype/EntityWithHealth`?!
User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3749
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: using find_entities_filtered to find Prototype/EntityWithHealth

Post by DaveMcW »

Not possible.

If you don't want to build a list of EntityWithHealth types, you can find everything and check if entity.prototype.max_health > 0.
User avatar
adamwong246
Fast Inserter
Fast Inserter
Posts: 148
Joined: Tue Dec 01, 2020 5:01 am
Contact:

Re: using find_entities_filtered to find Prototype/EntityWithHealth

Post by adamwong246 »

Oh dear.

Well, I'd rather have a performant hard-coded list rather than a non-performant query. Will a hard-coded list clash with other mods? Is it possible for a mod to add a prototype which implements directly from EntityWithHealth or must they implement a descendant class?
User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3749
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: using find_entities_filtered to find Prototype/EntityWithHealth

Post by DaveMcW »

Only the developers can add a new type.

Maybe the best approach is to scan game.entity_prototypes in the on_init and on_configuration_changed events, and store the list in global[].
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5211
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: using find_entities_filtered to find Prototype/EntityWithHealth

Post by eradicator »

Code: Select all

/c
local TypesWithHealth = {}
for _,prot in pairs(game.entity_prototypes) do
  if prot.max_health ~= 0 then
    TypesWithHealth[prot.type] = true
    end
  end

print(serpent.block(TypesWithHealth))
That list is longer than the one of entities without health though. So i wouldn't take any bets on that actually being faster than not filtering. Probably worth comparing with an inverted no-health filter too.

WithHealth:

Code: Select all

{
  accumulator = true,
  ["ammo-turret"] = true,
  ["arithmetic-combinator"] = true,
  ["artillery-turret"] = true,
  ["artillery-wagon"] = true,
  ["assembling-machine"] = true,
  beacon = true,
  boiler = true,
  ["burner-generator"] = true,
  car = true,
  ["cargo-wagon"] = true,
  character = true,
  ["combat-robot"] = true,
  ["constant-combinator"] = true,
  ["construction-robot"] = true,
  container = true,
  ["curved-rail"] = true,
  ["decider-combinator"] = true,
  ["electric-energy-interface"] = true,
  ["electric-pole"] = true,
  ["electric-turret"] = true,
  fish = true,
  ["fluid-turret"] = true,
  ["fluid-wagon"] = true,
  furnace = true,
  gate = true,
  generator = true,
  ["heat-interface"] = true,
  ["heat-pipe"] = true,
  ["infinity-container"] = true,
  ["infinity-pipe"] = true,
  inserter = true,
  lab = true,
  lamp = true,
  ["land-mine"] = true,
  ["linked-container"] = true,
  loader = true,
  ["loader-1x1"] = true,
  locomotive = true,
  ["logistic-container"] = true,
  ["logistic-robot"] = true,
  market = true,
  ["mining-drill"] = true,
  ["offshore-pump"] = true,
  pipe = true,
  ["pipe-to-ground"] = true,
  ["player-port"] = true,
  ["power-switch"] = true,
  ["programmable-speaker"] = true,
  pump = true,
  radar = true,
  ["rail-chain-signal"] = true,
  ["rail-signal"] = true,
  reactor = true,
  roboport = true,
  ["rocket-silo"] = true,
  ["simple-entity"] = true,
  ["simple-entity-with-force"] = true,
  ["simple-entity-with-owner"] = true,
  ["solar-panel"] = true,
  ["spider-leg"] = true,
  ["spider-vehicle"] = true,
  splitter = true,
  ["storage-tank"] = true,
  ["straight-rail"] = true,
  ["train-stop"] = true,
  ["transport-belt"] = true,
  tree = true,
  turret = true,
  ["underground-belt"] = true,
  unit = true,
  ["unit-spawner"] = true,
  wall = true
}
WithoutHealth:

Code: Select all

  arrow = true,
  ["artillery-flare"] = true,
  ["artillery-projectile"] = true,
  beam = true,
  ["character-corpse"] = true,
  cliff = true,
  corpse = true,
  ["deconstructible-tile-proxy"] = true,
  ["entity-ghost"] = true,
  explosion = true,
  fire = true,
  ["flame-thrower-explosion"] = true,
  ["flying-text"] = true,
  ["highlight-box"] = true,
  ["item-entity"] = true,
  ["item-request-proxy"] = true,
  ["leaf-particle"] = true,
  particle = true,
  ["particle-source"] = true,
  projectile = true,
  ["rail-remnants"] = true,
  resource = true,
  ["rocket-silo-rocket"] = true,
  ["rocket-silo-rocket-shadow"] = true,
  smoke = true,
  ["smoke-with-trigger"] = true,
  ["speech-bubble"] = true,
  sticker = true,
  stream = true,
  ["tile-ghost"] = true
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
Post Reply

Return to “Modding help”