Jump to content

Recommended Posts

Posted

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.

Posted

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 ?

Posted

I believe the callback has to be a normal function not a script function. That means it would know about the self variable so you can't use that. Instead if you wanted access to that script you would pass self.entity as the extra param to the callback

Posted

In the callback function use entity.script, and make sure it isn't nil.

 

entity.script is like the equivalent of "self" in this case.

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

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

Posted
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 ?

Posted

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

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