Collision Hooks or a workaround?

Hi everyone,
I just had a bash at doing my first Lua scripting by making a custom gamemode (in GMod13). I want the player to have a car and I want to detect when it collides with things. Unfortunately, there is no GM:OnEntityCollision hook (a request to add this was recently discussed here: http://facepunch.com/threads/1165695 ).
This seems to be possible with scripted entities but I don’t know of a way to make a prop_model_jeep vehicle a scripted entity? I don’t really want to roll my own vehicle and have to implement motion and suspension and all the other stuff the HL vehicle physics takes care of for you. Can anybody point me in the right detection for this? I just want to make a scripted entity that is for all intents and purposes a HL2 vehicle.

Thanks :slight_smile:

Here’s what I’ve tried so far- the objective is to be eventually able to use ENT:Touch on this car. At the moment I can’t get the entity to return true for Entity:IsVehicle() :frowning:

In init.lua
[lua]
AddCSLuaFile(“shared.lua”)
AddCSLuaFile(“cl_init.lua”)

include(“shared.lua”)

function ENT:SpawnFunction(ply, tr)
if (!tr.HitWorld) then return end

local ent = ents.Create("prop_vehicle_jalopy")
ent:SetPos(tr.HitPos + Vector(0, 0, 50))


self:SetKeyValue("vehiclescript", "scripts/vehicles/jalopy.txt")

ent:Spawn()
self:Activate()

return ent

end

function ENT:Initialize()
–self:SetModel(“models/vehicle/vehicle_rich.mdl”)

self:SetModel("models/vehicle.mdl")

--[[
self:PhysicsInit( SOLID_VPHYSICS ) 
self:SetMoveType( MOVETYPE_VPHYSICS )
self:SetSolid( SOLID_VPHYSICS )
]]



--[[
local phys = self:GetPhysicsObject()
if (phys:IsValid()) then
	phys:Wake()
end
]]

Msg("Am vehicle? " .. tostring(self:IsVehicle()))

end
[/lua]

And in shared.lua
[lua]
ENT.Type = “anim”
ENT.Base = “base_gmodentity”

ENT.PrintName = “Parking Vehicle”
ENT.Author = “KJ Tsanaktsidis”
ENT.Contact = “kjtsanaktsidis@gmail.com
[/lua]

-snip-

Didn’t read everything, sorry.

Dont know if this will work but try this:

In init.lua
[lua]
AddCSLuaFile(“shared.lua”)
AddCSLuaFile(“cl_init.lua”)

include(“shared.lua”)

function ENT:SpawnFunction(ply, tr)
if (!tr.HitWorld) then return end

self.ent = ents.Create("prop_vehicle_jalopy")
self.ent:SetPos(tr.HitPos + Vector(0, 0, 50))


self:SetKeyValue("vehiclescript", "scripts/vehicles/jalopy.txt")

self.ent:Spawn()
self:Activate()

return self.ent

end

function ENT:Initialize()
–self:SetModel(“models/vehicle/vehicle_rich.mdl”)

self:SetModel("models/vehicle.mdl")

--[[
self:PhysicsInit( SOLID_VPHYSICS ) 
self:SetMoveType( MOVETYPE_VPHYSICS )
self:SetSolid( SOLID_VPHYSICS )
]]



--[[
local phys = self:GetPhysicsObject()
if (phys:IsValid()) then
	phys:Wake()
end
]]

Msg("Am vehicle? " .. tostring(self.ent:IsVehicle()))

end
[/lua]

I dont believe your SENT is a vehicle by itself so you’d have to reference your ent directly.

EDIT:
Actually what would be better is to make a function IsVehicle() for your SENT so then you can use the IsVehicle elsewhere. So just
[lua]
function ENT:IsVehicle()
if not self.ent then return false end
return self.ent:IsVehicle()
end
[/lua]

and you can then the one line to Msg("Am vehicle? " … tostring(self:IsVehicle()))

Wouldn’t that introduce an infinite loop?

you didn’t end your commented out portion properly on line 39. should be --]]

Multiline comments with --[[ only need to end in ]].

What about the ShouldCollide hook? It’s called every time a collision is about to happen.

Just don’t return anything and you should be able to use it fine for your detection.

That’s not helpful unfortunately- according to my experiments, it gets called whenever two entities get close to each other, not when a collision is about to actually occur.

SW, I don’t really understand what your code is trying to do- but trying various combinations of it don’t seem to work. My understanding was that Initialise() needs to spawn a new entity and return it (like a C++ constructor), and that this entity is the self object for any further method calls on my SENT. However, this doesn’t seem to be the case and I’m really struggling to find an explanation of how the SENT system works in terms of OO concepts :confused:

I’m beginning to think that what I want to do is not possible…

Did you read the documentation on the page? “This hook is not called right before two entities collide, but simply to poll if they should collide when they are about to touch each other.”

Your kinda late, dude.

PhysicsCollide offers exactly what you’re looking for, from colliding with the world to entities.

Should’ve posted it then if you noticed, Your post is spam.
Ethan’s on the other hand is very helpful, quality reply, much respect.

Welcome to Facepunch.

Enjoy your stay…