Einlander Posted March 16, 2014 Share Posted March 16, 2014 Can a mod move this to the programming section? Is there a way to progamatically get a list of entities that are currently colliding with an object? I would like to be able to get a a list of all the unique entities in a collision trigger then compare it later. For example, a person walks into a room, then leaves. I want to be able to detect when a person leaves the area. I have been partially able to solve some of my needs. --[[ This script will make any entity act as a collision trigger. It works best when you set the entity's collision type to "Trigger". This will continuously detect collisions without causing any physical reaction. --This Has been modified to behave more like the Source Engine/Unity Collision Triggers ]]-- function Script:Start() --Create a table to keep track of all entitys that are interacting with the collision trigger self.CollisionEntites = {} end function Script:Collision(entity, position, normal, speed) --[[ We are going to keep track of 3 things: [Done] When an entity ENTERS the collision trigger [Done] When an entity is IN the collision trigger [] When an entity EXITS the collision trigger ]]-- --When an entity ENTERS the collision trigger if (self.CollisionEntites[entity] )== nil then -- Check table, is the entity already in it? -- No? --check if its in the list local matchfound = false if matchfound == false then for itemnum,value in ipairs(self.CollisionEntites) do if entity == value then matchfound=true end end table.insert(self.CollisionEntites,entity) --add entity to list of objects currenty in the collision trigger if matchfound == false then self:CollisionOnEnter(entity, position, normal, speed) -- Raise CollisionOnEnter event end end -- yes? Call the CollisionOnStay Event self:CollisionOnStay(entity, position, normal, speed) end end function Script:CollisionOnEnter(entity, position, normal, speed) self.component:CallOutputs("CollisionOnEnter") end function Script:CollisionOnStay(entity, position, normal, speed) self.component:CallOutputs("CollisionOnStay") end I can tell when an entity enters, and stays inside. But not when it leaves. Quote Link to comment Share on other sites More sharing options...
Rick Posted March 16, 2014 Share Posted March 16, 2014 You can do this with an entity script (no need to get a list) http://leadwerks.wikidot.com/wiki:collision-enter-exit Quote Link to comment Share on other sites More sharing options...
Einlander Posted March 16, 2014 Author Share Posted March 16, 2014 True, but that one indiscriminately report when itself has been entered and exited. I need the script to report which entity has exited. I would like the on exit to be able to report the same information the collision event does. Which entity left, and optionally, position and speed Quote Link to comment Share on other sites More sharing options...
Rick Posted March 16, 2014 Share Posted March 16, 2014 It was a rough night last night so maybe it's me, but what do you mean it indiscriminately reports when itself has been entered/exited? You would put this on a csg brush and paint the invisible material on it and it becomes your volume trigger reporting things that have entered and exited it. Quote Link to comment Share on other sites More sharing options...
Einlander Posted March 16, 2014 Author Share Posted March 16, 2014 The script you have, when a collision enters,exits and stays it reports an event happens. By indiscriminately I mean that it doesn't take account of which entity entered, or which entity exited. With that script, it doesn't say which entity entered, or which exited. I want the collision script to be able to affect other entities. For example, When an entity enters, set its gravity state off, then when it leaves turn it back on. That script will only know its holding something, not what or how many. Don't worry, I wrote this post after spending an all nighter. I'm writing this post while at work as fast as possible, so I dont know if I'm communicating my idea properly. Quote Link to comment Share on other sites More sharing options...
Rick Posted March 16, 2014 Share Posted March 16, 2014 This script, as is, works for 1 entity, but you should easily be able to extend that to work with any number of entities. There are 3 variables (entered, exited, hasCollision). Make a table that stores objects of: entity, entered, exited, hasCollision). OnEnter you would add them to this list. Then you loop through the list and set variables for that element like the script does for the one. Also, it does tell you which entity entered. The collision function has an entity parameter which is the entity that entered. What is doesn't do, but you can modify it to, is tell you which entity exited, because it doesn't care, but again you can make it care by modifying it Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.