Jump to content

Recommended Posts

Hi guys, I'm trying to make a function that calls to the nearest entity with a specific tag on it, but I'm not sure how I need to phrase it. I know that TheSim:FindFirstEntityWithTag will work in some circumstances, but I really need nearest as opposed to first. TheSim:FindNearest and TheSim:FindClosest don't work so I'm wondering how to get this to work.

Link to comment
https://forums.kleientertainment.com/forums/topic/81276-thesim-entities-help/
Share on other sites

There has already been a built-in global function in simutil.lua, you can simply use it:

function GetClosestInstWithTag(tag, inst, radius)
    local x, y, z = inst.Transform:GetWorldPosition()
    local ents = TheSim:FindEntities(x, y, z, radius, type(tag) == "string" and { tag } or tag)
    return ents[1] ~= inst and ents[1] or ents[2]
end

On 8/18/2017 at 5:37 PM, ptr said:

There has already been a built-in global function in simutil.lua, you can simply use it:

function GetClosestInstWithTag(tag, inst, radius)
    local x, y, z = inst.Transform:GetWorldPosition()
    local ents = TheSim:FindEntities(x, y, z, radius, type(tag) == "string" and { tag } or tag)
    return ents[1] ~= inst and ents[1] or ents[2]
end

so 

TheSim:GetClosestInstWithTag("tag") would work for any prefab instance with that tag and apply to the nearest one? not just the prefab where that line of code is?

Which line of code are you referring to?

If you break down GetClosestEntityWithTag, you see that the only thing it is doing is find you all the entity with a specific tag in a radius around an origin point and return you the entity which is the closest to that origin (excluding the entity "inst" which is the one use to determine the origin point).

So it just looks for the tag and is not doing anything else.

5 hours ago, ZupaleX said:

Which line of code are you referring to?

If you break down GetClosestEntityWithTag, you see that the only thing it is doing is find you all the entity with a specific tag in a radius around an origin point and return you the entity which is the closest to that origin (excluding the entity "inst" which is the one use to determine the origin point).

So it just looks for the tag and is not doing anything else.

so it would be GetClosestEntity and not GetClosestInst? I think I already tried GetClosestInst

local function RebindChester(inst, chester)
    chester = chester-- or TheSim:GetClosestInstWithTag("chester")--or TheSim:GetClosestEntityWithTag("chester")-- -- or TheSim:FindNearestEntityWithTag("chester")
    if chester ~= nil then
        OpenEye(inst)
        inst:ListenForEvent("death", function() StartRespawn(inst, TUNING.CHESTER_RESPAWN_TIME) end, chester)

        if chester.components.follower.leader ~= inst then
            chester.components.follower:SetLeader(inst)
        end
        return true
    end
end

 

note that all the different --TheSim lines are commented out, I'm only using one at a time when I test them, but none of the ones you see there work

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