dennis Posted July 5, 2016 Share Posted July 5, 2016 Hi all, I was wondering how I can get any entity in the world with a certain variable inside such as "health"? This is because I am making a script with exploding stuff in it. but when something near it with the health variable, it has to get the same amount of damage too. Thanks in common. Quote Link to comment Share on other sites More sharing options...
Josh Posted July 5, 2016 Share Posted July 5, 2016 For explosions, this function will quickly gather all entities in an area. It works independently from scene size, so no matter how many entities there are in the distance, this will be fast without having to loop through everything in the world: http://www.leadwerks.com/werkspace/page/api-reference/_/world/worldforeachentityinaabbdo-r66 1 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...
thehankinator Posted July 5, 2016 Share Posted July 5, 2016 (edited) I'd use GetEntityNeighbors() function in Scripts/Functions/GetEntityNeighbors.lua. You could do something like: local radius = 10 local entities = GetEntityNeighbors(self.entity, radius, true) for e=1, #entities do if entities[e].script.health ~= nil then --deal damage end end Edited July 6, 2016 by thehankinator 2 Quote Link to comment Share on other sites More sharing options...
dennis Posted July 6, 2016 Author Share Posted July 6, 2016 I'd use GetEntityNeighbors() function in Scripts/Functions/GetEntityNeighbors.lua. You could do something like: local radius = 10 local entities = GetEntityNeighbors(self.entity, radius, true) for e=0, #entities do if entities[e].script.health ~= nil then --deal damage end end Won't work for me, it gave me: 119 : attempt to index a nil value. Quote Link to comment Share on other sites More sharing options...
macklebee Posted July 6, 2016 Share Posted July 6, 2016 Won't work for me, it gave me: 119 : attempt to index a nil value. Did you import the GetEntityNeighbors script into your script before trying to use the above code? 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...
dennis Posted July 6, 2016 Author Share Posted July 6, 2016 Did you import the GetEntityNeighbors script into your script before trying to use the above code? Yes, I have. It gave me the error on: if entities[e].script.health ~= nil then Quote Link to comment Share on other sites More sharing options...
mdgunn Posted July 6, 2016 Share Posted July 6, 2016 As a guess, maybe check the entity has a script exists before you access something on it? Some entities coming back are probably not the ones you want to look at and may not have scripts. Quote Link to comment Share on other sites More sharing options...
dennis Posted July 6, 2016 Author Share Posted July 6, 2016 As a guess, maybe check the entity has a script exists before you access something on it? Some entities coming back are probably not the ones you want to look at and may not have scripts. Entities next to it (between the radius of 10) are my character and another object with that value. I am not getting why it wouldn't work. I can't really return the values using System:Print() because we can't print tables to the system. So I don't know what is returned I tried another value in the same script, but it is still a Nil value. So I guess the: for e=0, #entities do end isn't working. Quote Link to comment Share on other sites More sharing options...
macklebee Posted July 6, 2016 Share Posted July 6, 2016 It should be: for e=1, #entities do end 2 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...
dennis Posted July 6, 2016 Author Share Posted July 6, 2016 It should be: for e=1, #entities do end Macklebee ! you're the hero of today Completely works. Quote Link to comment Share on other sites More sharing options...
thehankinator Posted July 6, 2016 Share Posted July 6, 2016 It should be: for e=1, #entities do end Good catch! Old habits die hard. 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.