Garry's Mod Wiki

Revision Difference

Beginner_Tutorial_Self#561204

<title>Concepts - self </title>⤶ <cat>Dev.Lua</cat>⤶ ⤶ # What is self⤶ self is just a quality of life variable added in Lua that represents whatever the function is called on. Lua automatically creates this variable when defining functions with the `:` symbol.⤶ ⤶ hopefully you know how variable assigning works and if you don't, read <page>Beginner_Tutorial_Variables</page>⤶ ⤶ Here is an example:⤶ ```lua⤶ -- Using Lua Syntactical Sugar⤶ function myTable:anyFunction( arg1 )⤶ PrintTable( self )⤶ end⤶ ⤶ -- Using Literal Definitions ⤶ myTable.anyFunction = function( self , arg1 )⤶ PrintTable( self )⤶ end⤶ ⤶ ```⤶ ⤶ The only difference between the ways are defined is if we let Lua define `self` for us, or if we want to define it ourselves