Jump to content

[LUA] Get any entity in world with "health" ?


dennis
 Share

Recommended Posts

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.

Link to comment
Share on other sites

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

  • Upvote 1

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

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 by thehankinator
  • Upvote 2
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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