GM:ShouldCollide
Description
Called to decide whether a pair of entities should collide with each other. This is only called if Entity:SetCustomCollisionCheck was used on one or both entities.
Where applicable, consider using constraint.NoCollide or a logic_collision_pair entity instead - they are considerably easier to use and may be more appropriate in some situations.
Arguments
Returns
Example
A more reliable version of Entity:CollisionRulesChanged which should prevent issues with this hook. The examples on this page assume this is used.
Example
A simple example where players do not collide with eachother.
Example
As a best practice it is reccommended to use cached values for this hook as much as possible, calculating anything inside of this hook itself is both extremely expensive and runs the risk of not having called Entity:CollisionRulesChanged for a change in return value of the same entity pair.
Example
The conditions that control a ShouldCollide return should always be setup before calling Entity:SetCustomCollisionCheck, not doing so will result in physics breaking from uninitialised values being different for a couple frames affecting the return value.
Example
You should always return the same value for a pair of entities, so you have to check them the same way regardless of the order of arguments
Example
Avoid adding this hook more than once, it is extremely expensive to run. Instead you can use an inverse pattern where you give entities a function to disable collisions instead when needed, this will prevent having to add many hooks.
Example
If you want to use a players Team as a variable inside this hook you will have to keep in mind that you will have to call Entity:CollisionRulesChanged immediately after team changes.
Example
This hook can also be used to create custom Collision Groups, as long as the conditions of it are not expected to change this is quite a safe way to use the hook. This is especially powerful if used in a custom entity base.
Example
This hook should only be added BEFORE any entity affected by it exists, this ussually means you should create it during the loading phase of a server. Dynamically adding and removing them is asking for trouble.