Cunning fox Posted March 28, 2017 Share Posted March 28, 2017 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! 1 Link to comment Share on other sites More sharing options...
CarlZalph Posted March 28, 2017 Share Posted March 28, 2017 @makar5000 It's TheSim:FindEntities. Link to comment Share on other sites More sharing options...
Cunning fox Posted March 28, 2017 Author Share Posted March 28, 2017 Just now, CarlZalph said: @makar5000 It's TheSim:FindEntities. Still crashing :/ 1 Link to comment Share on other sites More sharing options...
CarlZalph Posted March 28, 2017 Share Posted March 28, 2017 1 minute ago, makar5000 said: Still crashing :/ Is it in modmain? GLOBAL.TheSim:FindEntities. If not, then post more code. Link to comment Share on other sites More sharing options...
Cunning fox Posted March 28, 2017 Author Share Posted March 28, 2017 2 minutes ago, CarlZalph said: Is it in modmain? GLOBAL.TheSim:FindEntities. If not, then post more code. oops, that was wrong one. Here's another: wormhole.lua 1 Link to comment Share on other sites More sharing options...
CarlZalph Posted March 28, 2017 Share Posted March 28, 2017 (edited) @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 March 28, 2017 by CarlZalph Logic flip Link to comment Share on other sites More sharing options...
Cunning fox Posted March 28, 2017 Author Share Posted March 28, 2017 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: But I need to change colour of both wormholes. Anyway thank you very much for help! 1 Link to comment Share on other sites More sharing options...
CarlZalph Posted March 28, 2017 Share Posted March 28, 2017 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 More sharing options...
Cunning fox Posted March 28, 2017 Author Share Posted March 28, 2017 (edited) 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 March 28, 2017 by makar5000 1 Link to comment Share on other sites More sharing options...
CarlZalph Posted March 28, 2017 Share Posted March 28, 2017 (edited) @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 March 28, 2017 by CarlZalph Link to comment Share on other sites More sharing options...
Cunning fox Posted March 29, 2017 Author Share Posted March 29, 2017 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: 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! 1 Link to comment Share on other sites More sharing options...
CarlZalph Posted March 29, 2017 Share Posted March 29, 2017 (edited) @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 March 29, 2017 by CarlZalph Link to comment Share on other sites More sharing options...
Cunning fox Posted March 29, 2017 Author Share Posted March 29, 2017 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! 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now