Detect when player runs gm_spawn

Hello guys, I have an issue with my code that I am trying to fix. I am trying to capture when I run a key bound to the command gm_spawn.
I went onto glua.me and found the source code for the command.
Since it’s apparently added using concommand.add, I could detect if it runs by detouring engineConsoleCommand… right?



local old_ecc = engineConsoleCommand
function engineConsoleCommand(ply, cmd, args)
    print(cmd)
    return old_ecc(ply, cmd, args)
end


I tried using the above code with no luck. Anyone have any idea what I need to do to detect that command?

(Sorry if my writing sounds weird. Very tired right now)

Yes, you should be able to detect when a player runs a command by detouring engineConsoleCommand. You aren’t doing anything in your detour though.

[lua]
local _engineConsoleCommand = engineConsoleCommand;
function engineConsoleCommand(ply, cmd, args)
if( cmd == “gm_spawn” ) then
– Do anything you want to do when they run gm_spawn here.
end

return _engineConsoleCommand(ply, cmd, args);

end
[/lua]

In the code I provided in the OP, when engineConsoleCommand is run, I have it print the cmd (line 3).

However, I decided to try your code, just to see if it would somehow work. This is what I ran



local _engineConsoleCommand = engineConsoleCommand;
function engineConsoleCommand(ply, cmd, args)
    if( cmd == "gm_spawn" ) then
    	print("Prop was spawned!")
    end
    print("A concommand was run")
 
    return _engineConsoleCommand(ply, cmd, args);
end


And here is what happened:

(I ran rp_vote to see if the detour was working)

Also, thanks for the quick reply. :slight_smile:

It’s probably because gm_spawn is a server side console command. Your engineConsoleCommand detour is purely client side, meaning the gm_spawn command is ran through the servers engineConsoleCommand rather then yours.

you can also use hooks to detect if a player spawned something:

EDIT:


GM:PlayerSpawnedProp(

http://glua.me/search/media/Player.png

ply,

http://glua.me/search/media/String.png

model,

http://glua.me/search/media/Entity.png

ent )
**Description:** Called when a player has already spawned a prop.

GM:PlayerSpawnedVehicle(

http://glua.me/search/media/Player.png

player,

http://glua.me/search/media/Vehicle.png

vehicle, )
**Description:** Called when a player has already spawned a vehicle. Use this to record the vehicle's propid. Useful for prop protection.

GM:PlayerSpawnedSENT(

http://glua.me/search/media/Player.png

userid,

http://glua.me/search/media/Entity.png

prop )
**Description:** Called when a player has already spawned a Scripted Entity. Use this to record the SENT's propid. Useful for prop protection.

GM:PlayerSpawnedRagdoll(

http://glua.me/search/media/Player.png

player,

http://glua.me/search/media/String.png

model,

http://glua.me/search/media/Entity.png

ragdoll )
**Description:** Called when a player has already spawned a ragdoll.

then you can do it like:



function PlayerSpawnedSomething(ply,...)
     local args = {...}
     MsgN(ply:Name().." spawned something : "..args[-1]:**GetClass(** **)**)
end

hook.Add("PlayerSpawnedProp","Props",PlayerSpawnedSomething(ply,...))
hook.Add("PlayerSpawnedVehicle","Props",PlayerSpawnedSomething(ply,...))
hook.Add("PlayerSpawnedRagdoll","Props",PlayerSpawnedSomething(ply,...))


(i hope you can use ‘-1’ on a table to get the last entry)

In this instance you’re best of using the gamemode hook Uke posted.
For generally detecting a change in a convar use cvars.AddChangeCallback
[lua]
local function onChange(cmdName, oldValue, newValue)

end

cvars.AddChangeCallback(“gm_spawn”,onChange)
[/lua]

Thanks everyone for your suggestions. I am aware of the hooks posted, but in this application I cannot use them. I have, however, determined an alternate course of action. Thanks again!