• "self" seemingly absent from gmod documentation
    11 replies, posted
I know "self" is a base lua thing, but I really don't understand how it works in the context of a swep. self.Owner doesn't show up in the wiki at all
Here is the PIL explanation: Programming in Lua "self" is the calling object in a class method. For SWEP's, this is the actual weapon entity. Ex. function SWEP:PrimaryAttack() print(self) end Will print the weapon. You can also use self to call more methods, like self:Clip1() or self:GetColor(). self.Owner is actually a deprecated hack that redirects to self:GetOwner() - you should use the latter, instead.
Thank you again. Also quick stupid question, I tried running self.Owner:Flashlight(true) within a SWEP:DrawHUD hook and it said that the Flashlight method was nil, despite it working in other hooks such as Secondary and PrimaryAttack
Player/Flashlight is a server side function, WEAPON/DrawHUD is called client side.
Flashlight is on the server, while HUD is on the client. The HUD doesn’t even know the Flashlight function exists.
So I'd have to use networking if I wanted a client hook to run a server function?
Oh jeez
The day i learned that i could use self in tables was the day i finally understood how to properly use Lua. Previous to that everything had been hacky and unreliable.
If we're just enabling a flashlight, it's not a big deal. Don't scare the lad. Besides, you can always just do RunConsoleCommand("impulse",100). That toggles the flashlight. Impulse
Sorry, you need to Log In to post a reply to this thread.