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 [URL="http://www.glua.me/bin/?path=/gamemodes/sandbox/gamemode/commands.lua"]the source code for the command.[/URL]
Since it's apparently added using concommand.add, I could detect if it runs by detouring engineConsoleCommand... right?
[CODE]
local old_ecc = engineConsoleCommand
function engineConsoleCommand(ply, cmd, args)
print(cmd)
return old_ecc(ply, cmd, args)
end
[/CODE]
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]
[QUOTE=JustSoFaded;37477304]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][/QUOTE]
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
[CODE]
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
[/CODE]
And here is what happened:
[IMG]http://gyazo.com/e6c5158580596ae542f9145ace6b9b68.png?1346385617[/IMG]
(I ran rp_vote to see if the detour was working)
Also, thanks for the quick reply. :)
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:
[URL]http://glua.me/search/?keywords=Playerspawn[/URL]
EDIT:
[CODE]GM:PlayerSpawnedProp( [IMG]http://glua.me/search/media/Player.png[/IMG] ply, [IMG]http://glua.me/search/media/String.png[/IMG] model, [IMG]http://glua.me/search/media/Entity.png[/IMG] ent )
[B]Description:[/B] Called when a player has already spawned a prop.
GM:PlayerSpawnedVehicle( [IMG]http://glua.me/search/media/Player.png[/IMG] player, [IMG]http://glua.me/search/media/Vehicle.png[/IMG] vehicle, )
[B]Description:[/B] Called when a player has already spawned a vehicle. Use this to record the vehicle's propid. Useful for prop protection.
GM:PlayerSpawnedSENT( [IMG]http://glua.me/search/media/Player.png[/IMG] userid, [IMG]http://glua.me/search/media/Entity.png[/IMG] prop )
[B]Description:[/B] Called when a player has already spawned a Scripted Entity. Use this to record the SENT's propid. Useful for prop protection.
GM:PlayerSpawnedRagdoll( [IMG]http://glua.me/search/media/Player.png[/IMG] player, [IMG]http://glua.me/search/media/String.png[/IMG] model, [IMG]http://glua.me/search/media/Entity.png[/IMG] ragdoll )
[B]Description:[/B] Called when a player has already spawned a ragdoll.[/CODE]
then you can do it like:
[CODE]
function PlayerSpawnedSomething(ply,...)
local args = {...}
MsgN(ply:Name().." spawned something : "..args[-1]:[B]GetClass([/B] [B])[/B])
end
hook.Add("PlayerSpawnedProp","Props",PlayerSpawnedSomething(ply,...))
hook.Add("PlayerSpawnedVehicle","Props",PlayerSpawnedSomething(ply,...))
hook.Add("PlayerSpawnedRagdoll","Props",PlayerSpawnedSomething(ply,...))
[/CODE]
(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!
Sorry, you need to Log In to post a reply to this thread.