func_button use hook

Before i go any further, yes i have searched :stuck_out_tongue:

http://www.facepunch.com/threads/1031954-Lua-even-triggered-with-func_button

I created the following hook to test:
[lua]
function GM:PlayerUse( ply, ent )
print(“test”)
end[/lua]

It prints “test” to console whenever i use on any custom SENT, but when i press use on a func_button / map ent it doesn’t.

In the other thread it mentions to create a SENT, in my case I assume it would be in gamemode/entities/entities/func_button(?)

Could anyone suggest as to why it doesn’t work on the func_button, or direct me towards “overriding” the func_button ent :3

if ent:GetClass() == “func_button” then?

A somewhat hackish way to do it is to use they KeyPress hook and check if key == IN_USE and if the player is close enough to his trace.

I thought about that Entoros, but there HAS to be a more efficient way >_>

and joker tried that, made no difference :frowning:

[editline]22nd January 2011[/editline]

Disregard thread >.>

To anyone who stumbles across same problem… make sure the “Use Activates” flag is checked in hammer… :stuck_out_tongue:

/wrists

[lua]
function OnButtonPressed( ply, ent )
if ent:GetClass() == “func_button” and ent.ID == 1 then – replace 1 with the Button ID you want

end

end
hook.Add( “PlayerUse”, “when a button is pressed”, OnButtonPressed )

function GiveButtonIDs()
for k, v in pairs(ents.GetAll) do
if ent:GetClass() == “func_button” then
v.ID = k – the button number will allways be the same, also if you restart you PC
print("Found a button and gave it ID: "…k)
end
end
end
hook.Add( “InitPostEntity”, “this give button IDs”, GiveButtonIDs )

function GetButtonID( ply )
local trace = ply:GetEyeTrace()
if trace.Entity:GetClass() == “func_button” then
print("Button ID = "…trace.Entity.ID) – this prints the button ID to console
end
end
concommand.Add(“GetButtonID”, GetButtonID)
[/lua]

so what you wanna do is go to a button and write ‘GetButtonID’ in console and a number will be printet and then you wanna replace that number with 1 in PlayerUse Hook

EDIT: you have to look directly at the button :wink:

He solved it.