Jump to content

How to get the position of the nearest Enemy ? Planning thread


Slastraf
 Share

Recommended Posts

How would you get a tower to get the next enemy in the scene ?

I have already done the aligning to a target thats manually put in.

I have a pivot thats attached to a prefab of the crawler.

This pivot is the target , and when the crawler spawns the turret should know if a pivot is in range and align to it.

Link to comment
Share on other sites

Make tower search for enemies by doing World:ForEachEntityInAABBDo()

Then check Entity:GetDistance() for all and choose the least one.

Script.rotSpeed = 0.1
Script.target = nil --entity "target"
Script.Pivot = nil

function Script:Callback(entity,extra)

--We only want to execute this on models. If we don't check the class we will change the color of the light, too.
if (entity:GetKeyValue("type")=="enemy") then
self.target = entity
end
end


function Script:Start()

end


--[[
function Script:UpdateWorld()

end
--]]

function Script:UpdatePhysics()
--Create an axis-aligned bounding box for testing. This will occupy an area 10x10x10 centered around the position -5,0,0.
--So it will intersect all entities on the left
local aabb = AABB()
aabb.min=self.entity:GetPosition()
aabb.max=self.entity:GetPosition()+Vec3(10,10,10)
aabb.center=self.entity:GetPosition()
aabb:Update()
--We're going to pass a Vec3 to the callback in the extra parameter
local v = Vec3(1,0,0)
world:ForEachEntityInAABBDo(aabb,"self.Callback",v)


--self.entity:Point(self.target, 2, Time:GetSpeed() * self.rotSpeed)
if self.target~=nil then
self.vector = self.target:GetPosition(true)-self.entity:GetPosition(true)
self.entity:AlignToVector(self.vector:Normalize(),2)
local newRot = self.entity:GetRotation(true)
self.entity:SetRotation( Vec3( 90, -180+newRot.y, newRot.z ), true)
end
end 

I got this now. my problem is , how do you call the callback function inside the entity script, no tthe main.lua script?

How do you check for the keyvalue then ?

self.entity:SetKeyValue("type","enemy")

I have this in the enemy script. is this right ?

Link to comment
Share on other sites

you cannot use 'self' inside the callback function as there is nothing to refer to... try using 'Script.target' instead of 'self.target'

Similar error with "Script.target = entity --choose target"

http://www.leadwerks.com/werkspace/topic/6273-le3-how-to-find-a-specific-entity/ An related thread where I got the script from.

It seems like the Callback function is not made to acces self variables

Link to comment
Share on other sites

It seems like the Callback function is not made to acces self variables 

 

This is just a fundamental misunderstanding of how Lua is working from you. Until you fully get the 'self' idea you will find things not making sense or difficult to implement. This is all about variable scope and understanding what 'self' means.

Link to comment
Share on other sites

It seems like the Callback function is not made to acces self variables 

 

This is just a fundamental misunderstanding of how Lua is working from you. Until you fully get the 'self' idea you will find things not making sense or difficult to implement. This is all about variable scope and understanding what 'self' means.

Before I am going to Implement an enemy array which stores current enemies and make a distance request for every turret in every updatephysics, what would be the way you program it ? I have now researched that "self" is a local variable and I assume the callback function is sort of global , what is the way you would do it ?

Link to comment
Share on other sites

I would do it the way you are doing it. I was just pointing out if you aren't sure what 'self' means then it's why you are having the issues you are and makes it harder for you to fully understand how to design something.

 

 

world:ForEachEntityInAABBDo(aabb,"Callback",self.entity)

 

 

function Callback(entity,extra)

--We only want to execute this on models. If we don't check the class we will change the color of the light, too.

if entity:GetKeyValue("type")=="enemy" then

extra.script:AddEnemy(entity)

end

end

 

 

Then make a function called AddEnemy() in your script. So you see what you are doing is making a global function and then you are passing that to ForEachEntityInAABBDo() along with the entity itself as the extra parameter. The engine will call this function and pass the entity around with it so you can get access to your function inside your callback.

  • Upvote 1
Link to comment
Share on other sites

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.

 Share

×
×
  • Create New...