Jump to content

Recommended Posts

@SenL, TheSim is a C++ object, so you won't find its function definitions directly in the Lua code.

 

But the useful code is found in scripts/simutil.lua:

function FindEntity(inst, radius, fn, musttags, canttags, mustoneoftags)    if inst ~= nil 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 i, v in ipairs(ents) do            if v ~= inst and v.entity:IsVisible() and (fn == nil or fn(v, inst)) then                return v            end        end    endend

Which I found after ~20s of looking through results from find-in-files for "TheSim:FindEntities"

What's difference between that and FindEntity()

 

In abigail lua retarget fn:

return FindEntity(inst, 20, function(guy)
        return inst._playerlink ~= nil
            and inst.components.combat:CanTarget(guy)
            and (guy.components.combat.target == inst._playerlink or
                inst._playerlink.components.combat.target == guy)
    end)
 
Edit:
Oh, duh .. FindEntity calls TheSim:FindEntities .... nevermind.
Edited by SenL

musttags - array of tags. All tags are required.

canttags - array of tags. All tags are forbidden.

mustoneoftags - array of tags. Only one of these tags is needed.

 

For example, if an object has all musttags tags and one of cantstags, it can't be found.

If an object has only few of musttags tags, it can't be found.

etc

 

Be careful with this function. It can eat huge amount of CPU. Depends on radius.

​For example, FindEntities(x,y,z,9000) takes a few seconds.

 

 

 

@SenL, Yes, if you have a list of the entities that you're checking for, then definitely use that. Especially players, since there are so few of them compared to other entities. Depending on how clever the underlying implementation of FindEntities is (we can't easily tell because it's in the C++ code), it either has to cycle through all entities in the world, calculating a distance, or it has to cycle through all entities with the specified tag, calculating distance. So it's to be avoided whenever possible.

Edited by rezecib

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