Phodex Games Posted November 20, 2016 Posted November 20, 2016 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? Quote
Genebris Posted November 20, 2016 Posted November 20, 2016 This: http://www.leadwerks.com/werkspace/page/api-reference/_/world/worldforeachentityinaabbdo-r66 1 Quote
Charrua Posted November 21, 2016 Posted November 21, 2016 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 Quote Paren el mundo!, me quiero bajar.
Phodex Games Posted November 21, 2016 Author Posted November 21, 2016 Ah ok cool that was what I am searching for 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. Quote
Genebris Posted November 21, 2016 Posted November 21, 2016 It's a box, you know it's center and size, that's it. http://www.leadwerks.com/werkspace/page/api-reference/_/aabb/ Quote
AggrorJorn Posted November 21, 2016 Posted November 21, 2016 An Axis Aligned Bounding Box is a minimal box shape that encompasses the entire model. It is often used for physics. Quote
Phodex Games Posted November 21, 2016 Author Posted November 21, 2016 Ok thank you always willing to learn something new about game development! Quote
reepblue Posted November 21, 2016 Posted November 21, 2016 Anyway to draw the bounds of these in-game for debugging purposes? Quote Cyclone - Ultra Game System - Component Preprocessor - Tex2TGA - Darkness Awaits Template (Leadwerks) If you like my work, consider supporting me on Patreon!
Genebris Posted November 21, 2016 Posted November 21, 2016 No, but you can create an actual model box with the same size. Quote
Recommended Posts
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.