Addcallback does not work


include("tr_allowed_entities.lua")
 
function tr_touch( selfent, col_data )
        print("2")
        local ent = col_data.HitEntity
        if ent:IsVehicle() then
                selfent:Remove()
        end
end
 
hook.Add( "OnEntityCreated", "tr_SetColCheck", function(ent)
        if trunk_entities[ent:GetClass()] then
                print("1")
                ent:AddCallback( "PhysicsCollide", tr_touch )
        end
end )


It never prints 2, though it prints 1. What am I doing wrong?

AddCallback almost never works. There’s a very narrow range of entities it will work on and I forgot which ones they are.

I remember having the same problem, an illogical fix is just making the function within the AddCallback function, like:



ent:AddCallback("PhysicsCollide", function(selfent, col_data)
 local ent = col_data.HitEntity
        if ent:IsVehicle() then
                selfent:Remove()
        end
end )


I don’t see what that changes.

The fact it avoids the possibility of tr_touch being overridden somehow.

@RedNinja, do print(“1”, ent) and tell us the entity class.

The entity class is sent_ball, because its the only entity on the table.

It might be that AddCallback doesn’t work on scripted entities, could be a bug with the game

[editline]10th August 2015[/editline]

Or more specifically PhysicsCollide event doesn’t work on SENTs because you sort of already have ENT.PhysicsCollide

I tried using a money printer. Does the same thing.

Yeah, because that’s another SENT

Problematic. What do I do now?

if IsScripted then
local oldCallback = entity.PhysicsCollide
entity.PhysicsCollide = function() mycallback() return oldCallback() end
end

Something like that for now I guess.

Do you mean something like:



hook.Add( "OnEntityCreated", "tr_SetColCheck", function(ent)
	if trunk_entities[ent:GetClass()] then
	    ent:AddCallback("PhysicsCollide", function( self, data )
			print("its okay")
			local ent = data.HitEntity
			if ent:IsVehicle() then 
				self:Remove()
			end
			return self.PhysicsCollide
	    end)
	end
end )


Because this does not work either.
Anyways i’d simply add now a chat command for now.

Use this instead of ent:AddCallback

[editline]10th August 2015[/editline]

For only SENTs