[noob] The easiest way to hooking all collides - how to do it?
15 replies, posted
Hi
I try write my first useful lua plugin but i have many problems. This plugin should prevent from irresponsible players which lags server (spams props etc.).
My idea is that plugin should count number of collides for each entity in a given unit of time. If server will have fps drop - he will freeze entities with the biggest count of collides and kick/ban players which created/used them.
I think i know how to save count of collides in entity (Entity:SetVar) but i don't know how to hook collides. I found interesting event: ENTITY:PhysicsCollide but, first, im not sure do i properly hook it.
First way:
[CODE]
function Collide( colData, collider )
Msg("bleble")
end
hook.Add("PhysicsCollide", "physCol", Collide)
[/CODE]
Second way:
[CODE]
function PhysicsCollide( colData, collider )
Msg("bleble")
end
[/CODE]
Looks like both doesn't work (I spawn props, touch them together but in console nothing appear).
In old wiki, at "ENT.PhysicsCollide" I found:
[QUOTE]
Description: Controls what a [B]scripted[/B] entity does during a collision
[/QUOTE]
I searched additional information about "Scripted Entities" and i found in old wiki:
[QUOTE]A Scripted Entity (SENT for short) is a Lua scripted entity in Garry's Mod. For example, the [B]bouncy ball[/B] entity, included in Garry's Mod.[/QUOTE]
But I tested my script for Bouncy Ball and doesn't work too :( Also i dont know how to convert normal entities to scripted entities (easy way).
What I am doing wrong? Help please.
Greg
PhysicsCollide is an ENTITY hook, not a gamemode hook. To log every single physics collide, you will have to add "PhysicsCollide" hook to each entity using the [URL="http://wiki.garrysmod.com/page/Entity/AddCallback"]Entity:AddCallback[/URL] function in something like [URL="http://wiki.garrysmod.com/page/GM/OnEntityCreated"]OnEntityCreated[/URL] hook.
Thanks for fast answer :)
I created that script:
[CODE]
hook.Add("OnEntityCreated", "entityCreated", EntityCreated)
function EntityCreated( ent )
Msg("Entity created\n");
ent:AddCallback("PhysicsCollide", onColide);
end
function onColide( ent )
Msg("Collide!\n");
end
[/CODE]
Is it ok? Looks like it works but, i dont know why, i must load it 2x (i use lua_openscript command).
Put the hook.Add stuff AFTER you define the function you put into the hook.Add:
[code]
local function onColide( ent ) -- Localize this too, since we only use it from function below
Msg("Collide!\n");
end
local function EntityCreated( ent ) -- Make it local, we are not going to use this anywhere else, except for the line below
Msg("Entity created\n");
ent:AddCallback("PhysicsCollide", onColide);
end
hook.Add("OnEntityCreated", "entityCreated", EntityCreated)
[/code]
Or like this:
To save space, if you want
[code]
hook.Add("OnEntityCreated", "entityCreated", function( ent )
Msg("Entity created\n");
ent:AddCallback("PhysicsCollide", function( self, data )
Msg("Collide!\n");
end)
end)
[/code]
Please note, that semicolons ( ; ) are optional in Lua.
Thanks again. Now i have next problem:
[CODE]
local function onColide ( colData, collider )
Msg("Collide!\n");
Msg(collider:GetName())
Msg("\n")
end
local function EntityCreated( entity )
Msg("Entity created\n");
entity:AddCallback("PhysicsCollide", onColide)
Msg(entity:GetCreator())
Msg("\n")
end
hook.Add("OnEntityCreated", "entityCreated", EntityCreated)
[/CODE]
The problem is that i can't get collider's name
[CODE][ERROR] lua/test.lua:3: attempt to call method 'GetName' (a nil value)[/CODE]
And can't get Creator when entity is created, even when I spawn (return nil)
Tried also a few other methods and they don't work too. What I am doing wrong?!
[QUOTE=grzesmlodzian;45749909]The problem is that i can't get collider's name[/QUOTE]
If collider isn't a player you'll get this error. Add :IsPlayer() check or something.
EDIT: I think it'll never be player since it's PhysObj of entity, not entity itself.
But problems is that other methods like GetMass() or GetEntity() doesn't work too (probably other too). Error happens when method is called. Maybe something is wrong with 'collider' param. When I write it by 'Msg' i get table (look below)
[CODE]
local function onColide ( colData, collider )
Msg("Collide!\n")
Msg(" collider = ", collider, "\n") //collider = table: 0x23e113b8
Msg(" collider:GetName() = ", collider:GetName(), "\n") //attempt to call method 'GetName' (a nil value)
Msg(" collider:GetMass() = ", collider:GetMass(), "\n") //attempt to call method 'GetMass' (a nil value)
Msg(" collider:GetEntity() = ", collider:GetEntity(), "\n") //attempt to call method 'GetEntity' (a nil value)
Msg("Done Collide!\n");
end
local function EntityCreated( entity )
entity:AddCallback("PhysicsCollide", onColide)
Msg("Entity created!\n")
Msg(" entity = ", entity, "\n") //entity = Entity [67][prop_physics]
Msg(" entity:GetCreator() = ", entity:GetCreator(), "\n") //entity:GetCreator() = nil
Msg("Done entity create!\n")
end
hook.Add("OnEntityCreated", "entityCreated", EntityCreated)
[/CODE]
[editline]21st August 2014[/editline]
Wait wait.... maybe in wiki is mistake and params are swapped?!
[editline]21st August 2014[/editline]
[CODE]
Msg(" colData = ", colData, "\n") //colData = Entity [93][prop_physics]
Msg(" collider = ", collider, "\n") //collider = table: 0x2b568968
[/CODE]
[editline]21st August 2014[/editline]
If I think good this is wrong ([URL="http://wiki.garrysmod.com/page/ENTITY/PhysicsCollide"]http://wiki.garrysmod.com/page/ENTITY/PhysicsCollide[/URL]):
[CODE]ENTITY:PhysicsCollide( table colData, PhysObj collider )[/CODE]
And should be something like this:
[CODE]ENTITY:PhysicsCollide( Entity collider, table colData )[/CODE]
Indeed it's wrong, I just checked. Arguments are swapped and it passes Entity instead of PhysObj.
EDIT: I changed the wiki page. Not sure if it's correct actually, this may be a bug. For now it still should be this way though.
Wasnt there a ShouldCollide gamemode hook he could use?
[QUOTE=mijyuoon;45758164]Indeed it's wrong, I just checked. Arguments are swapped and it passes Entity instead of PhysObj.
EDIT: I changed the wiki page. Not sure if it's correct actually, this may be a bug. For now it still should be this way though.[/QUOTE]
It is not wrong.
You are wrong because you are looking at a wiki page for ENT:PhysicsCollide hook, but you are using AddCallback, which is entirely different story.
Now I have next problem. I can't send param (Entity) to function called by Timer:
[CODE]
local function RefreshPhysicsAttacker(ent)
Msg(ent, "\n") //nil
end
local function PhysgunPickup( ply, ent )
Msg("Pick! ", ent, "\n") //here is ok
timer.Create( "PhysAttackRefr_" .. ent:GetCreationID(), 0.5, 0, RefreshPhysicsAttacker, ent )
end
hook.Add("PhysgunPickup", "GrzesioPhysgunPickup", PhysgunPickup)
[/CODE]
[editline]21st August 2014[/editline]
Sorry about that. Soo how can i use ENT:PhysicsCollide hook? And where can i find function params using AddCallback?
that's not how you use timers. You can't use ENT:PhysicsCollide with prop_physics on engine entities, as said before. Just print out the parameters.
Ok I resolved problem with timer:
[CODE]timer.Create( "PhysAttackRefr_" .. ent:GetCreationID(), 0.5, 0, function() RefreshPhysicsAttacker(ent) end )[/CODE]
[QUOTE]You can't use ENT:PhysicsCollide with prop_physics on engine entities, as said before.[/QUOTE]
I tested Bouncy Ball and it doesn't work with AddCallback. If it is not engine entity, how I can hook its collision?
[QUOTE]Just print out the parameters.[/QUOTE]
What do you mean?
Wrote this just for you: [url]http://wiki.garrysmod.com/page/Entity_Callbacks[/url]
Thanks but, if I will have in future similator problem, still I don't where/how search information. Sometimes they just don't exists? Or just I dont know where/how search (for example that params)?
And still I don't know how use all hooks ([URL="http://wiki.garrysmod.com/page/Category:Hooks"]http://wiki.garrysmod.com/page/Category:Hooks[/URL]) which are listed on wiki :(
I've tried doing this except without saying the collide message, i had it remove itself, i did this with a sniper rifle of my design, however, it removes the thing I'm shooting at as well, and now whenever i try to spawn an entity other than my sniper bullet, it created errors, any help on this?
Sorry, you need to Log In to post a reply to this thread.