Jump to content

Recommended Posts

So, I am trying to collect creatures by searching for their tags using FindEntities as follows:

 

local ents = TheSim:FindEntities(x, y, z, 100,creaturetags, creatureno_tags)

 

"ents" is always empty even though I know there are creatures in range with a tag in "creaturetags".

 

As I understood it FindEntities is (x,y,z, radius, musttags, canttags, mustoneoftags) and I am using:

 

local maincharacter = GetPlayer()    
    if maincharacter then
        local x,y,z = maincharacter.Transform:GetWorldPosition()  

 

to set the x,y,z.

 

Would anyone know why it is always empty? Am I just misusing the function, or is it something else? Is there a better way to find creatures repeatedly?

 

Thanks in advance

So, I am trying to collect creatures by searching for their tags using FindEntities as follows:

 

local ents = TheSim:FindEntities(x, y, z, 100,creaturetags, creatureno_tags)

 

"ents" is always empty even though I know there are creatures in range with a tag in "creaturetags".

 

As I understood it FindEntities is (x,y,z, radius, musttags, canttags, mustoneoftags) and I am using:

 

local maincharacter = GetPlayer()    

    if maincharacter then

        local x,y,z = maincharacter.Transform:GetWorldPosition()  

 

to set the x,y,z.

 

Would anyone know why it is always empty? Am I just misusing the function, or is it something else? Is there a better way to find creatures repeatedly?

 

Thanks in advance

This is the FindEntities function as it appears in simutil.lua:

 

function FindEntity(inst, radius, fn, musttags, canttags, mustoneoftags)    if inst and inst:IsValid() then        local x,y,z = inst.Transform:GetWorldPosition()                    --print ("FIND", inst, radius, musttags and #musttags or 0, canttags and #canttags or 0, mustoneoftags and #mustoneoftags or 0)        local ents = TheSim:FindEntities(x,y,z, radius, musttags, canttags, mustoneoftags) -- or we could include a flag to the search?        for k, v in pairs(ents) do            if v ~= inst and v.entity:IsValid() and v.entity:IsVisible() and (not fn or fn(v, inst)) then                return v            end        end    endend

 

As you can see, the first argument is inst, not x,y,z. It does x,y,z of inst for you in the first line within the if statement.

Yes, I know about that, but I see all over the game files entries like this:

 

TheSim:FindEntities(pt.x, pt.y, pt.z, SEE_FOOD_DIST, FOOD_TAGS, NO_TAGS)

 

where they use only the x,y,z, range and tags, so I assumed I could do the same. Is this not the case?

Yes, I know about that, but I see all over the game files entries like this:

 

TheSim:FindEntities(pt.x, pt.y, pt.z, SEE_FOOD_DIST, FOOD_TAGS, NO_TAGS)

 

where they use only the x,y,z, range and tags, so I assumed I could do the same. Is this not the case?

Okay, so I think I misunderstood.

 

Try this:

 

local maincharacter = GetPlayer()        if maincharacter then        local pt = maincharacter.Transform:GetWorldPosition()

 

and your FindEntities should take that into account:

 

local ents = FindEntities(pt.x, pt.y, pt.z, SEE_FOOD_DIST, FOOD_TAGS, NO_TAGS)

 

The issue with that is you're then going to have to include something like this, since ents is a table:

 

for k,v in pairs(ents) do    if v.entity:IsValid() then        return v    endend

 

when you could just use FindEntity as described, if all you're doing is finding an entity with a tag. What exactly are you trying to do?

Yes, I know about that, but I see all over the game files entries like this:

 

TheSim:FindEntities(pt.x, pt.y, pt.z, SEE_FOOD_DIST, FOOD_TAGS, NO_TAGS)

 

where they use only the x,y,z, range and tags, so I assumed I could do the same. Is this not the case?

Yes, you can do this, and it's the most general way. Could you post the whole code (or at least the whole function)? Because the snippet you posted in itself is correct.

Okay, so I think I misunderstood.

 

Try this:

local maincharacter = GetPlayer()        if maincharacter then        local pt = maincharacter.Transform:GetWorldPosition()

and your FindEntities should take that into account:

local ents = FindEntities(pt.x, pt.y, pt.z, SEE_FOOD_DIST, FOOD_TAGS, NO_TAGS)

The issue with that is you're then going to have to include something like this, since ents is a table:

for k,v in pairs(ents) do    if v.entity:IsValid() then        return v    endend

when you could just use FindEntity as described, if all you're doing is finding an entity with a tag. What exactly are you trying to do?

Thanks, I will try this as it seems to make sense. I am trying to change stats of creatures found with the tag specified, more of a test than anything.

Thanks, I will try this as it seems to make sense. I am trying to change stats of creatures found with the tag specified, more of a test than anything.

Make sure to use GLOBAL.GetPlayer() as Heavenfall suggested, or it won't give you coords since it'll return as nil.

Make sure to use GLOBAL.GetPlayer() as Heavenfall suggested, or it won't give you coords since it'll return as nil.

 

Nevermind, I forgot I was already using the same thing you posted but it returns an error trying to index pt, which is a number. It does work if I do for example:

 

local ptx, pty, ptz = maincharacter.Transform:GetWorldPosition()

 

but the ents table still is always empty.

Post the whole code.

 

I think I am going to go a completely different way with this. On that note, when adding a component to something. In the component class, will "inst" always refer to the object that the component is added to?

 

ie

local RPGArmor = Class(function(self, inst)    	self.inst = inst

Thanks again.

You can refer to the entity which has the component added to it using self.inst as long as it is declared in the component. Merely using inst is not enough in any child function of the component class, unless you pass it to the component child function through an argument.

Edited by Heavenfall

You can refer to the entity which has the component added to it using self.inst as long as it is declared in the component. Merely using inst is not enough in any child function of the component class, unless you pass it to the component child function through an argument.

 

Perfect, thanks. I appreciate the help.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
  • Create New...