Page 1 of 1

Add a modded tag to entities

Posted: Tue Dec 08, 2015 9:31 am
by MrDoomah
I would like an ability for mods to add a field to entities. Right now I've got some code that looks like this:

Code: Select all

entity_data = {{ ["entity"] = entity, ["tag"] = entity_tag},
               { ["entity"] = entity_2, ["tag"] = entity_2_tag}}
new_entities = surface.find_entities(area)

for k1,v1 in pairs(new_entities) do
  for k2,v2 in pairs(entity_data) do
    if v1 == v2.entity do
      some code involving v2.tag
    else
      code to add v1 to entity_data
    end
  end
end
While this works fine for small amounts of entities, this code scales quadratic with the number of entities. Even as few as 100 entities cause 10.000 iterations.

My suggestion is then to add a way to tag entities. For example: entity.set_tag(tag) to set a tag and entity.get_tag() return said tag (or nil). Preferable that this tag is mod specific just as global is.