[Done] find_entities_filtered help

Place to get help with not working mods / modding interface.
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1459
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

[Done] find_entities_filtered help

Post by TheSAguy »

Morning,

I want to count all the units with a certain name around a trigger event.
Would do that with "find_entities_filtered", but the names need to be a list?

Below I'm trying to count all the fast biters. Their names range from "ne-biter-fast-1" to "ne-biter-fast-20", around a trigger event. A green trigger projectile fired: "ne_green_splash_1"

Just not sure how to implement to list of names with find_entities_filtered

Code: Select all

local green_units = entity.surface.find_entities_filtered{area=area, name=unit_names[]}
Extended code:

Code: Select all


--------------------- TRIGGERS  ---------------------------------
script.on_event(defines.events.on_trigger_created_entity, function(event)
	
	local entity = event.entity	

 	--- Unit Launcher Projectile Trigger
	if entity.valid and entity.name == "ne_green_splash_1" then
	
		local unit_names = {["ne-biter-fast-1"] = true,
                 ["ne-biter-fast-1"] = true,
				 ["ne-biter-fast-2"] = true,
                 ["ne-biter-fast-3"] = true,
				 ["ne-biter-fast-4"] = true,
                 ["ne-biter-fast-5"] = true,
				 ["ne-biter-fast-6"] = true,
                 ["ne-biter-fast-7"] = true,
				 ["ne-biter-fast-8"] = true,
                 ["ne-biter-fast-9"] = true,
				 ["ne-biter-fast-10"] = true,
                 ["ne-biter-fast-11"] = true,
				 ["ne-biter-fast-12"] = true,
                 ["ne-biter-fast-13"] = true,
				 ["ne-biter-fast-14"] = true,
                 ["ne-biter-fast-15"] = true,
				 ["ne-biter-fast-16"] = true,
                 ["ne-biter-fast-17"] = true,
				 ["ne-biter-fast-18"] = true,
                 ["ne-biter-fast-19"] = true,
				 ["ne-biter-fast-20"] = true
				 }
				 
		local radius = 10
		local pos = entity.position	
		local area = {{pos.x - radius, pos.y - radius}, {pos.x + radius, pos.y + radius}}
		local green_units = entity.surface.find_entities_filtered{area=area, name=unit_names[]}
		
		if #green_units >= 10 then
			SpawnLaunchedUnits(entity)
		end
		
    end

end)



Thankas.
Last edited by TheSAguy on Sun Apr 14, 2019 8:05 pm, edited 1 time in total.
Bilka
Factorio Staff
Factorio Staff
Posts: 3684
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: find_entities_filtered help

Post by Bilka »

First of all, use count_entities_filtered for counting, not find entities filtered. Second, the parameter is an array of strings. You have a table indexed by strings. Furthermore, "unitnames[]" is not valid lua.

Here is example code:

Code: Select all

local unit_names = {"ne-biter-fast-1", "ne-biter-fast-2", "ne-biter-fast-3", ... } 
local green_units = entity.surface.count_entities_filtered{area=area, name=unit_names}
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1459
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: find_entities_filtered help

Post by TheSAguy »

Great, thanks!!
Post Reply

Return to “Modding help”