Jump to content

Need help fixing code


Recommended Posts

Hello, I need help figuring out why this code isn't working :?.

Spoiler

local function Find_Pig(inst)
	local x, y, z = inst.Transform:GetWorldPosition()
	local pig = TheSim:FindEntities(x, y, z, 10,{"pig"})
	
	for k, v in pairs(pig) do
		if v then
			print("Pig in range")
		else
			print("Pig not in range")
		end
	end
end

--masterpostinit
inst:DoPeriodicTask(1, Find_Pig)

 

I basically want if pigs are in range to do something for my char and if they aren't do something else, but print("Pig not in range") never triggers only "Pig in range" triggers when I'm near a pig? I don't understand what's wrong, hopefully somebody can help, thanks for your time and help, have a good day/night :D!

Edited by Warbucks
Link to comment
Share on other sites

6 minutes ago, Warbucks said:

Hello, I need help figuring out why this code isn't working :?.

  Hide contents


local function Find_Pig(inst)
	local x, y, z = inst.Transform:GetWorldPosition()
	local pig = TheSim:FindEntities(x, y, z, 10,{"pig"})
	
	for k, v in pairs(pig) do
		if v then
			print("Pig in range")
		else
			print("Pig not in range")
		end
	end
end

--masterpostinit
inst:DoPeriodicTask(1, Find_Pig)

 

I basically want if pigs are in range to do something for my char and if they aren't do something else, but print("Pig not in range") never triggers only "Pig in range" triggers when I'm near a pig? I don't understand what's wrong, hopefully somebody can help, thanks for your time and help, have a good day/night :D!

If you want to know if there is or isn't any pigs in range, then either calculate the size of the return result from your filtered entity scan or check if the first result is not nil.

 

if #pig > 0
then
  -- a pig is in range
else
  -- no pigs in range
end

~or~

if pig[1] ~= nil
then
  -- a pig is in range
else
  -- no pigs in range
end

 

Link to comment
Share on other sites

You can use FindEntity if you don't need to get all of them.

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

Since it returns the prefab if exist, it could be faster.

Link to comment
Share on other sites

52 minutes ago, YakumoYukari said:

You can use FindEntity if you don't need to get all of them.


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

Since it returns the prefab if exist, it could be faster.

FindEntity calls TheSim:FindEntities, which his code uses, and thus is effectively the same in terms of performance.

Link to comment
Share on other sites

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
 Share

×
  • Create New...