r/GLua • • Jan 07 '21

How to use "function ENT:Use() ... end" from the game console (lua_run command)

I need my function to be called when picking up an item, but I don't know how to create "function ENT:Use() ... end" event of prop_physics from the console. Through this script, I got the entity:

lua_run barrel = Entity(1):GetEyeTrace().Entity

and its functions like "barrel:Ignite(1)" work properly

But how to use its events? I tried write "function barrel:Use(activator) print("Test") end", but it doesn't work, even the errors don't appear. The wiki says to use "function ENT:Use(activator) ... end", but as I understood the ENT variable is available only by addons, then how can this event be used in the console (lua_run)?

I will be glad to help and sorry for my English :)

Edit: I found another event that does what I need:

hook.Add("PlayerUse", "PrintUseInformation", function(ply, ent) ... end)

but then there is a problem. This function runs until USE is released (many triggers in one pick up).

Solution:

agent = ents.Create("lua_run")
agent:SetName("propHook")
agent:Spawn()
prop = Entity(1):GetEyeTrace().Entity
prop:SetUseType(SIMPLE_USE)
prop:Fire("AddOutput", "OnPlayerPickup propHook:RunPassedCode:hook.Run('CustomPlayerUse'):0:-1")
hook.Add("CustomPlayerUse", "My bicycle", function()
    print(ACTIVATOR, CALLER)
end)

and Look for example in this article for a complete view. Unlike "hook.Add("PlayerUse", "PrintUseInformation", function(ply, ent) ... end)" this code is called when picking up the specified entity and the function is called once. Thank you all for your help.

3 Upvotes

13 comments sorted by

View all comments

Show parent comment

0

u/[deleted] Jan 11 '21

[deleted] — view deleted comment

0

u/TomatoCo Jan 11 '21

The wiki only lists OnAngleChange and PhysicsCollide. Quite frankly I've never heard of OnAngleChange but PhysicsCollide, you'll observe, is in the same section as Use. There's also the vital line "This also allows you to use certain hooks in engine entities (non-scripted entities)." which is the exact usecase desired here. By default, engine entities do not have SENT hooks called on them. AddCallback is the (maybe only) way to have SENT hooks called on them.

As for it working... It's hard to tell. It's not clear what kind of entity OP is trying to hook. I have only had occasion to use AddCallback once or twice in the 15 years (started in gmod 9) I've been playing GMod. If it were a SENT then the code written would certainly work. If it weren't a SENT then AddCallback is the way to go. AddCallback should work regardless of it it's a SENT or not.

0

u/[deleted] Jan 11 '21

[deleted] — view deleted comment

1

u/TomatoCo Jan 11 '21

I didn't test Use specifically. It appears to work for PhysicsCollide but I didn't get any further than testing copy/pasted code. https://i.imgur.com/6tWc6Ld.png

0

u/[deleted] Jan 11 '21

[deleted] — view deleted comment

2

u/[deleted] Jan 12 '21 edited Jan 12 '21

I found a good solution!

agent = ents.Create("lua_run")
agent:SetName("propHook")
agent:Spawn()
prop = Entity(1):GetEyeTrace().Entity
prop:SetUseType(SIMPLE_USE)
prop:Fire("AddOutput", "OnPlayerPickup propHook:RunPassedCode:hook.Run('CustomPlayerUse'):0:-1")
hook.Add("CustomPlayerUse", "My bicycle", function()
    print(ACTIVATOR, CALLER)
end)

Output: Player [1][Weinsone] Entity [126][prop_physics]