• ShoudCollide and CollisionRulesChanged
    6 replies, posted
Hello again! I came across both of these functions during the making of an entity, but it was rather vague as to how I should be using [URL="https://wiki.garrysmod.com/page/Entity/CollisionRulesChanged"]CollisionRulesChanged[/URL] correctly (The wiki provided minimal meaningful information). I stumbled upon Willox's statement: [url]https://facepunch.com/showthread.php?t=1532864&p=51107083&viewfull=1#post51107083[/url] [QUOTE]There's only one way to use ShouldCollide properly. All your logic should live outside of the ShouldCollide hook (e.g. in a Think hook) and [B]whenever the result of two entities changes, you call CollisionRulesChanged on both of them[/B].[/QUOTE] Here comes a few questions I have in mind, 1. Why do we need to call CollisionRulesChanged()? 2. Let's say we have Entity A and a player. By default the collision on Entity A is turned on (It will collide with anything). We're going to adjust the ShouldCollide property so that the Entity A only allows Team RED to go through(return false), while Team BLUE to collide(return true). When the player from Team RED collides with Entity A, we're going to do [B]return false[/B] in ShouldCollide hook, hence they will not collide. a. Are we supposed to call CollisionRulesChanged() upon returning false? (Definitely not inside the hook) Read below b. In this case, how should we call CollisionRulesChanged()? Since we're not suppose to call it inside the hook, from what I've researched, we're supposed to create a Think hook for the entity and call it when the ShouldCollide return false somehow? Or should we be calling CollisionRulesChanged() when the player changes their teams? c. To which entity are supposed to call CollisionRulesChanged() to? Entity A or the player or both? Note: Only Entity A has SetCustomCollisionCheck() set to true. The player don't, will that affect on which entity CollisionRulesChanged() should be called on? This is the code for above simulation: [CODE]local function ShouldCollide( wall, ply ) if wall and ply:IsPlayer() then if ply:Team() == "RED" then return false -- Allowing players to go through end end end hook.Add("ShouldCollide", "Collision Check", function( ent1, ent2 ) if IsValid( ent1 ) and IsValid( ent2 ) then if ent1:GetClass() == "ent_collision_wall" then return ShouldCollide( ent1, ent2 ) elseif ent2:GetClass() == "ent_collision_wall" then return ShouldCollide( ent2, ent1 ) end end end )[/CODE] When these gets answered, I'll most probably edit the wiki for the better understanding of their functions and roles.
First of all, you are very right about the wiki lacking information on this subject. I wanted to add this info but I've never used this hook and function and so I preferred not to add possibly-wrong information. The quote you found from Willox is very helpful, and confirms what I already suspected about ShouldCollide behavior. [QUOTE=nubpro;51306994]Or should we be calling CollisionRulesChanged() when the player changes their teams?[/QUOTE] Bingo. The one thing that needs clearing up, is whether we really need to call CollisionRulesChanged on BOTH entities. I can imagine a case where having to do this would be dumb - for example, in the example you provided, what if the wall changes the team it's associated with? Suddenly team red should collide but team blue shouldn't. Obviously we'd need to call CollisionRulesChanged on the wall entity, but do we really need to call it for ALL PLAYERS?
What NeatNit says is true and nothing you're doing looks wrong at a glance. SetTeam should be calling CollisionRulesChanged itself (but Team() should never return a string, so maybe you've overridden all of that?)
[QUOTE=Willox;51313125]What NeatNit says is true and nothing you're doing looks wrong at a glance. SetTeam should be calling CollisionRulesChanged itself (but Team() should never return a string, so maybe you've overridden all of that?)[/QUOTE] Thank you for responding :) Don't worry, that's just part of the example. Now, what if SetCustomCollisionCheck() is set to true on the entity and not the player, should we call CollisionRulesChanged on both of them or just the entity?
You can call it on both, but you only need to call it on whatever entity the rules have changed for. The rule in this case is just the team of the player.
I just rewrote both [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/ShouldCollide]GM:ShouldCollide[/url] and [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Entity/CollisionRulesChanged]Entity:CollisionRulesChanged[/url]. Feel free of course to make improvements. Still need to add an example. I think the team-based force field is a fantastic example, as it shows multiple scenarios in one easy-to-understand concept. I can't do that now though, so anyone is free to take charge and write it up!
I appreciate the responses! Thank you both!
Sorry, you need to Log In to post a reply to this thread.