Jump to content

Problem with FindEntities


Recommended Posts

Hello everyone! I'm trying to make a mod, that'll change color of wormhole if there's a trap near it. For this I've tried to use "FindEntities", but it's crashing :/. My function is here:

local function CheckOwner(inst)
inst:DoPeriodicTask(10,function(inst)
	local x, y, z = inst.Transform:GetWorldPosition()
	local ents = inst:FindEntities(x, y, z, 5, {"trap"})
		if ents ~= nil then
		inst.AnimState:SetMultColour(.2, .2, .2, 1)
		end
    end)
end

But it's crusing with:

[00:03:16]: [string "../mods/foxy's mod 0.2/scripts/prefabs/worm..."]:82: attempt to compare table with number

So... I realy need some help now. Thanks everyone for your replyes! :) 

  • Like 1
Link to comment
Share on other sites

@makar5000

line 82: if ents <= 0 or ents == nil then

to: if #ents <= 0 then

TheSim:FindEntities returns a table.

 

 

Also move: CheckOwner(inst)

to be before: inst:SetStateGraph("SGwormhole")

The server will know of entities in limbo while clients do not.

Edited by CarlZalph
Logic flip
Link to comment
Share on other sites

6 minutes ago, CarlZalph said:

@makar5000

line 82: if ents <= 0 or ents == nil then

to: if #ents > 0 then

TheSim:FindEntities returns a table.

Yeah, It's working! Thanks! But... I have another question. Is it possible to change colour of both wormholes? Now it looks like this:

Безымянный.png

But I need to change colour of both wormholes. Anyway thank you very much for help!:) 

  • Like 1
Link to comment
Share on other sites

3 minutes ago, makar5000 said:

Yeah, It's working! Thanks! But... I have another question. Is it possible to change colour of both wormholes? Now it looks like this:

But I need to change colour of both wormholes. Anyway thank you very much for help!:) 

local other = inst.components.teleporter.targetTeleporter
if other ~= nil
then
    other.AnimState:SetMultColour(.2, .2, .2, 1)
end

 

Link to comment
Share on other sites

3 hours ago, CarlZalph said:

local other = inst.components.teleporter.targetTeleporter
if other ~= nil
then
    other.AnimState:SetMultColour(.2, .2, .2, 1)
end

 

Oh, It's working! Thanks. I'm sorry, but can you help me again?:) I'll understand, if you won't unswer. So... Now I've changed wormholes so they change colour to green if there's no traps near it, andbecome red, if there is a trap near it. But... It looks not how I planed:

28.avi

My code is here:

local function CheckOwner(inst)
inst:DoPeriodicTask(1,function(inst)
	local x, y, z = inst.Transform:GetWorldPosition()
	local ents = TheSim:FindEntities(x, y, z, 5, {"trap"})
		if #ents > 0 then
		local other = inst.components.teleporter.targetTeleporter
		inst.AnimState:SetMultColour(1, 0, 0, 1)
				if other ~= nil
					then
				other.AnimState:SetMultColour(1, 0, 0, 1)
			end
			else
		local other = inst.components.teleporter.targetTeleporter
		inst.AnimState:SetMultColour(0, 1, 0, 1)
			if other ~= nil
				then
			other.AnimState:SetMultColour(0, 1, 0, 1)
		end
		end
    end)
end

As I said, I'll understand, if you don't answer. Anyways you're a magician. Many thanks!

Edited by makar5000
  • Like 1
Link to comment
Share on other sites

@makar5000

Reason for it is because the wormholes work both ways, so each side of the tunnel is going to fight each other the way you have the code setup.

local function CheckOwner(inst)
    inst.mymodname_HasTrap = false
    inst:DoPeriodicTask(
        1,
        function(inst)
            local other = inst.components.teleporter.targetTeleporter
            local x, y, z = inst.Transform:GetWorldPosition()
            local ents = TheSim:FindEntities(x, y, z, 5, {"trap"})
            inst.mymodname_HasTrap = (#ents > 0) or (other and other.mymodname_HasTrap and other ~= inst)
            if inst.mymodname_HasTrap
            then
                inst.AnimState:SetMultColour(1, 0, 0, 1)
            else
                inst.AnimState:SetMultColour(0, 1, 0, 1)
            end
        end
    )
end

This will make it propagate so it will also work with wormhole chains (A->B->C->D) or multiwormhole networks (A->B<-C<-D) created by other things.

It will only propagate if the leader in the chain has a trap or if itself has a trap, so if C has a trap then only {A B C} get coloured- D goes nowhere.

Reason for "mymodname_" part is that it's making a variable on the instance itself so you'll have name conflicts if other mods also touch the wormhole entity, so it should be unique.

Edited by CarlZalph
Link to comment
Share on other sites

14 hours ago, CarlZalph said:

@makar5000

Reason for it is because the wormholes work both ways, so each side of the tunnel is going to fight each other the way you have the code setup.


local function CheckOwner(inst)
    inst.mymodname_HasTrap = false
    inst:DoPeriodicTask(
        1,
        function(inst)
            local other = inst.components.teleporter.targetTeleporter
            local x, y, z = inst.Transform:GetWorldPosition()
            local ents = TheSim:FindEntities(x, y, z, 5, {"trap"})
            inst.mymodname_HasTrap = (#ents > 0) or (other and other.mymodname_HasTrap and other ~= inst)
            if inst.mymodname_HasTrap
            then
                inst.AnimState:SetMultColour(1, 0, 0, 1)
            else
                inst.AnimState:SetMultColour(0, 1, 0, 1)
            end
        end
    )
end

This will make it propagate so it will also work with wormhole chains (A->B->C->D) or multiwormhole networks (A->B<-C<-D) created by other things.

It will only propagate if the leader in the chain has a trap or if itself has a trap, so if C has a trap then only {A B C} get coloured- D goes nowhere.

Reason for "mymodname_" part is that it's making a variable on the instance itself so you'll have name conflicts if other mods also touch the wormhole entity, so it should be unique.

Thanks! It stoped blinking, but... It stoped changing coloures when there's no any trap near it. It works like this now:

pijSZGVIak.png

Is that fixable? I've tried to fix it, but nothing changed. Thanks for you help! 

Also... Do you want any skin? I can give it to you as a reward for your help! :D 

  • Like 1
Link to comment
Share on other sites

@makar5000

local function CheckOwner(inst)
    inst.mymodname_HasTrap = false
    inst:DoPeriodicTask(
        1,
        function(inst)
            local other = inst.components.teleporter.targetTeleporter
            local x, y, z = inst.Transform:GetWorldPosition()
            local ents = TheSim:FindEntities(x, y, z, 5, {"trap"})
            inst.mymodname_HasTrap = (#ents > 0)
            if inst.mymodname_HasTrap or (other and other.mymodname_HasTrap)
            then
                inst.AnimState:SetMultColour(1, 0, 0, 1)
            else
                inst.AnimState:SetMultColour(0, 1, 0, 1)
            end
        end
    )
end

The feeling of tiredness and cut-pasting parts onto the wrong line.

Edited by CarlZalph
Link to comment
Share on other sites

14 minutes ago, CarlZalph said:

@makar5000


local function CheckOwner(inst)
    inst.mymodname_HasTrap = false
    inst:DoPeriodicTask(
        1,
        function(inst)
            local other = inst.components.teleporter.targetTeleporter
            local x, y, z = inst.Transform:GetWorldPosition()
            local ents = TheSim:FindEntities(x, y, z, 5, {"trap"})
            inst.mymodname_HasTrap = (#ents > 0)
            if inst.mymodname_HasTrap or (other and other.mymodname_HasTrap)
            then
                inst.AnimState:SetMultColour(1, 0, 0, 1)
            else
                inst.AnimState:SetMultColour(0, 1, 0, 1)
            end
        end
    )
end

The feeling of tiredness and cut-pasting parts onto the wrong line.

Yeah, It's working perfectly! Many thanks! You're a genius!

  • Like 1
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...