Jump to content
Phodex Games

Get Specific Entities In An Area

Recommended Posts

What would be the best way to get a specific type of entity in an area? I am currently working on my AI and I want it to search a weapon, if it has no at the moment. So I want it to check its sourrounding for a weapon.

Whats the best way to do it. Or I could need it that my AI can check for enemies in its sourrounding.

 

I thought of getting all entities in the scene (world) check their distance to the AI and if it is smaller then the AIs sense radius, then it checks them for the type (for example if it is a weapon or not) and returns to the AI.

 

So whats the best way to get specific entities in a defined area (for example around a NPC)? Any ideas on how to make it better then the concept I have?

Share this post


Link to post

if you are working with lua, there is an already made function and you can use it in any lua script of your own

 

just include it, declare a table variable to store the list of entities that "GetEntityNeighbors" function returns

 

--add this line to include the function
import "Scripts/Functions/GetEntityNeighbors.lua"
--delcare a table to store neighbors
Script.EntitiesInAABB = {}
then in the update world yo may
function Script:UpdateWorld()
   EntitiesInAABB = GetEntityNeighbors(self.entity,4,true)  -- 4 means look 4 Leadwerks unit ahead, back left, right
   --self.entty is at the center of a box of you desired measure
   -- the second parameter "true" in my example dictates that only entities with a script attached to them will be included (it's your choice)
  --then you may do something like
  for key,EntInAABB in pairs(EntitiesInAABB) do
  if type(EntInAABB.script.Open)=="function" then
	  EntInAABB.script:Open() --if an antity inside the box has a function called "Open" then i invoke it, just as example
  end
   end

  ....
  .... rest of your updateworld
end

 

it should be unnecessary to check the surrounding area each frame, you may check it once in a second for example, here a more elaborated example, that includes a variable RadarRange that you may set in the editor and checks for neighbors once a second:

 

import "Scripts/Functions/GetEntityNeighbors.lua"
Script.EntitiesInAABB = {}
Script.RadarRange = 5 --float "Radar Range"
Script.AcumTime = 0
function Script:UpdateWorld()
self.AcumTime = self.AcumTime + Time:GetSpeed()
if self.AcumTime>100 then
 self.AcumTime=0
 --look for neighbors once a second instead of every frame
 EntitiesInAABB = GetEntityNeighbors(self.entity,self.RadarRange,true)
 for key,EntInAABB in pairs(EntitiesInAABB) do
  if type(EntInAABB.script.Open)=="function" then
   EntInAABB.script:Open()
  end
 end
end

...
... rest of your update world
...
end

 

Juan

Share this post


Link to post

Ah ok cool that was what I am searching for biggrin.png cool thanks both of you!!

 

P.S.: Could somebody quickly explain what AABB is? I am a little confused of it and I would like to know how it works.

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

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...