Slastraf Posted November 22, 2015 Share Posted November 22, 2015 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. Quote Link to comment Share on other sites More sharing options...
shadmar Posted November 22, 2015 Share Posted November 22, 2015 Make tower search for enemies by doing World:ForEachEntityInAABBDo() Then check Entity:GetDistance() for all and choose the least one. Quote HP Omen - 16GB - i7 - Nvidia GTX 1060 6GB Link to comment Share on other sites More sharing options...
Slastraf Posted November 22, 2015 Author Share Posted November 22, 2015 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 ? Quote Link to comment Share on other sites More sharing options...
Rick Posted November 22, 2015 Share Posted November 22, 2015 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 Quote Link to comment Share on other sites More sharing options...
Slastraf Posted November 22, 2015 Author Share Posted November 22, 2015 http://pastebin.com/yahRFMcJ Updated script. : 11 : attempt to index global 'self' (a nil value) I get this error. what could be the cause ? Quote Link to comment Share on other sites More sharing options...
macklebee Posted November 22, 2015 Share Posted November 22, 2015 you cannot use 'self' inside the callback function as there is nothing to refer to... try using 'Script.target' instead of 'self.target' Quote Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590 LE / 3DWS / BMX / Hexagon macklebee's channel Link to comment Share on other sites More sharing options...
Slastraf Posted November 22, 2015 Author Share Posted November 22, 2015 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 Quote Link to comment Share on other sites More sharing options...
Josh Posted November 22, 2015 Share Posted November 22, 2015 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. Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
Josh Posted November 22, 2015 Share Posted November 22, 2015 http://www.leadwerks.com/werkspace/page/api-reference/_/world/worldforeachentityinaabbdo-r66 Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
Rick Posted November 22, 2015 Share Posted November 22, 2015 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. Quote Link to comment Share on other sites More sharing options...
Slastraf Posted November 23, 2015 Author Share Posted November 23, 2015 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 ? Quote Link to comment Share on other sites More sharing options...
Rick Posted November 23, 2015 Share Posted November 23, 2015 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. 1 Quote Link to comment Share on other sites More sharing options...
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.