Jump to content

Recommended Posts

Posted

There's not really a single good way in Lua to retrieve a selection of entities you are interested in.

You usually don't want to sort through every single entity in the world because it would be slow to do in Lua. Instead, you can use GetTaggedEntities() or store them in a global array-style table. However, this requires that other scripts also had the same idea and added the tag or inserted the entity into the same table. This requirement reduces automatic interoperability of scripts, because the author of the other script might not have been thinking about the same things you are.

For entities in a specific area, you can call GetEntitiesInArea() but if the area is very big, you could potentially be sorting through hundreds of results. This isn't something you probably want to use very heavily in Lua.

C++ can iterate through and perform comparisons much, much faster than Lua can. I'm experimenting with query filters added to both World:GetEntities() and World:GetEntitiesInArea():

function Turret:ChooseTarget()
  
	-- Get the area we are interested in
	local pos = self:GetPosition(true)
	local p0 = pos - self.range
	local p1 = pos + self.range

	-- Select all entities that match the following criteria:
	-- entity.health is more than zero
	-- entity.team is defined (not nil) and is different from the turret's team
	local targets = self.world:GetEntitiesInArea(p0,p1, "health",">","0", "team","~=",nil, "team","~=",self.team)

	-- Any of the returned entities are valid targets. We probably just want to choose the closest one
	for n = 1, #targets do
		--code to find the nearest entity goes here
	end
  
end

Variadic arguments are used, so any number of conditions can be specified.

An additional optional parameter at the end could specify "and" or "or" for the logic of the query.

My job is to make tools you love, with the features you want, and performance you can't live without.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...