Garry's Mod Wiki

Revision Difference

nil#527771

<cat>Dev.Lua</cat> nil is Lua's representation of empty data. Unset variables and non-existent table keys will be nil. This will be treated as false in if-statement contexts, but is not equal to false. ⤶ Example:⤶ ```⤶ print(nil) print(nil == 0) print(nil == 1) print(nil == nil) print(!nil) --because nil can be treated like a false boolean in some cases, "not nil" will equal true local myEmptyVariable --a variable with nothing in it (a 'nil' value) print(myEmptyVariable) local myTable = {a="foo", b="bar"} myTable["a"] = nil --setting a value in a table to nil effectively erases it from the table PrintTable(myTable) ```⤶ ⤶ Output:<br/>nil<br/>false<br/>false<br/>true<br/>true<br/>nil<br/>b = bar⤶ ⤶ <example>⤶ <code>⤶ print( nil ) print( nil == 0 ) print( nil == 1 ) print( nil == nil ) print( !nil ) -- because nil can be treated like a false boolean in some cases, "not nil" will equal true local myEmptyVariable -- a variable with nothing in it (a 'nil' value) print( myEmptyVariable ) local myTable = { a = "foo", b = "bar" } myTable["a"] = nil -- setting a value in a table to nil effectively erases it from the table PrintTable( myTable ) </code>⤶ <output><br/>nil<br/>false<br/>false<br/>true<br/>true<br/>nil<br/>b = bar</output>⤶ </example>⤶ # Application in VGUI You can use nil to make some VGUI element transparent. ⤶ ## Example Code⤶ ⤶ ```⤶ local menu = vgui.Create( 'DFrame' ) ⤶ <example>⤶ <code>⤶ local menu = vgui.Create( "DFrame" ) menu:SetSize( 300, 200 ) menu:Center() menu:MakePopup() menu:SetTitle( 'Made by Freline' ) local panel = vgui.Create( 'DPanel', menu ) menu:SetTitle( "Made by Freline" ) local panel = vgui.Create( "DPanel", menu ) panel:Dock( FILL ) panel.Paint = nil ```⤶ </code>⤶ </example>