Edit an inner function of a class

Say I want for just an example create an explosion every time a vehicle touches a player. Obviously I do not have access to any class and can’t edit their Ent:Touch() functions,
so how do I do that? I tried looking for a hook and found nothing.

Why would you want to? You could just copy the entity of which-ever ent you want and just paste the code in if you want 2 copies of them.

You can use Entity:AddCallback with PhysicsCollide for this purpose. Or use tracers, which is not as elegant.

Thank you, but can you show me an example how it would work with vehicles? It seems like you put it in an entity’s class

The vehicles arent even written in lua

As I said, you gotta use Entity:AddCallback() with PhysicsCollide, that doesn’t mean ENT:PhysicsCollide, that means open the function on the wiki and you will an example and why I said “with PhysicsCollide”.

[sp]If you still can’t figure it out, Entity:AddCallback( “PhysicsCollide”, print ) where Entity is the vehicle entity[/sp]

its not very clear in the wiki, what do I use instead of Entity for an example?

Can’t he hook it into the gamemode?

the car

car:AddCallback()
the car is a variable representing the entity
you need to identify the vehicle, for example when spawning it the code that spawned it references the entity

[editline]19th February 2015[/editline]

what are you talking about

He could use a ShouldCollide (http://wiki.garrysmod.com/page/GM/ShouldCollide) hook

I’d do it like this:



local function VehicleRamAssplosion( ent1, ent2 )
     local ply
     local vehickey

     if ent1:IsPlayer() then ply = ent1 end
     if ent1:IsVehicle() and IsValid(ent1:GetDriver()) then vehickey = ent1 end 
     if ent2:IsPlayer() then ply = ent2 end
     if ent2:IsVehicle() and IsValid(ent2:GetDriver()) then vehickey = ent2 end

     if IsValid(ply) and IsValid(vehickey) and SERVER then
          util.BlastDamage( vehickey, vehickey:GetDriver(), ply:GetPos(), 350, 100 ) 

          local fx = EffectData()
          fx:SetOrigin(ply:GetPos())
          util.Effect( "Explosion", fx )
     end
end


[editline]19th February 2015[/editline]

I think this is the better solution because in my experience, AddCallback simply doesn’t work

ShouldCollide runs at the wrong time for detecting actual collisions. It occurs several times when things are in proximity of each other to determine what to do WHEN the collision occurs.

The callback is for the actual collision.

If it doesn’t work, you don’t go using something completely useless to the purpose, you ask for it to be fixed.

ShouldCollide also is called when the player looks at things…

[lua]function func( ent, tbl )
print(“Touched”)
end

hook.Add(“OnEntityCreated”, “AssignCallback”, function(ent)
if ( ent:IsVehicle() ) then
ent:AddCallback( “PhysicsCollide”, func )
end
end)
[/lua]

Why wouldn’t this work?

Doesn’t it? :o

No, it doesn’t