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.
View all comments
Show parent comment
2
u/TomatoCo Jan 11 '21 edited Jan 11 '21
It's difficult to say. If he wants (for example) all props with the melon model to be Use-able then I'd suggest he hook OnEntityCreated and PreCleanupMap/PostCleanupMap. If he wants to only change select entities then yes, concommand.Add. Regardless, neither of these techniques solve the problem of "What code do I run to let me hit Use on this entity?" For that, you need Entity:AddCallback.
If they're making a gamemode or a long-running server there's no good reason to use concommand.Add. It requires active player intervention, after all. They should instead hook whatever entity-creating hook they desire and use positions, entity ids, or what-have-you to decide if the object should be Callback'd or not.