Lua Tips

From GMod Wiki

Jump to: navigation, search
Icon-info.png Go to:
Useful Information


Contents

Lua tips

Feel free to add your tips for beginning scripters here.

Naming Objects

It can be very hard to name objects correctly. Try to abide by these naming rules to prevent errors.

Cross.pngDo not name an object a number.

local 1 = "Hello"


Cross.pngDo not name an object to begin with a number.

local 0string = "Hello"


Tick.pngName an object with a descriptive name.

local message = "Hello"
local position = Vector( 32, 32, 32 )
local blue = Color( 0, 0, 255, 255 )


Comments

Tick.pngIt's best practice to keep commenting your code. In GMod Lua, you use comments with the code // or with --, like this:

 
// Loads a player's data
function pmeta:Load()
 

Or this:

-- Loads a player's data
function pmeta:Load()

You can use multi-line comments with /* Comment */ or --[[ Comment ]].

Cross.pngBe careful not to comment out statements unless you intend to.

 
 function pmeta: // Player metatable Load() // This is our load section
 


Cross.pngDon't give useless comments. The key rule is to describe not WHAT, but WHY. Commenting more then you need to can be worse then not commenting at all. Examples:

Bad

 
local sMessage = "Player has died." // Sets sMessage to "Player has died"
 

Good

 
local sMessage = "Player has died." // We need to inform the players of a death.
 


Tick.pngWhen you call a return function of which you do not change the result to much, store the result in a simple variable to save processing time and keep the code more efficent

Good

 
//the computer only needs to reference a local variable and preform the method a small amount of times
local s = self:GetTable();
s.newammo = 100;
s.propallow = { [1] = 0 }
s.ispowerful = false;
s.savepos = Vector( 10, 20, 80 );
 

Bad

 
//the computer needs to reference the variable self and call the method GetTable() 4 times as aposed to 1 time
self:GetTable().newammo = 100;
self:GetTable().propallow = { [1] = 0 }
self:GetTable().ispowerful = false;
self:GetTable().savepos = Vector( 10, 20, 80 );
 

Using Lua to manipulate functions in more or less the same way as any variables:

function LOL(Args)
        print("Hello World!")
end
 

Is the same as:

LOL = function(Args)
        print("Hello World!")
end
 

And so using this principle this works:

 
ROFL = LOL
 
ROFL() -- Will print "Hello World"
 


You can use this to add onto other functions like:

 
function LOL(Args)
        print("Hello World!")
end
 
local Backup = LOL -- Backup the old function
 
function LOL(Args) -- Re-make it
	print("Hello Universe!")
 
	Backup() -- Call the old LOL (Which prints "Hello World")
 
	print("Space Travel is fun!")
end
 
LOL() -- Prints Hello Universe followed by Hello World followed by Space Travel is fun!
 


Using as many arguments as needed

function Function(...) -- Pass as many arguments you want
        local arg = {...} -- Put the arguments into a table
        print(arg[1]) -- Print the first one
        print(arg[10]) -- Print the tenth one
end

Using type()

local Function = function() end
local String = "lol"
local Number = 10
local Table = {}
 
print(type(Function)) -- Prints "function"
print(type(String)) -- Prints "string"
print(type(Number)) -- Prints "number"
print(type(Table)) -- Prints "table" 

Don't Cram Codes into one file!

Tick.png As a lua programmer I know that cramming lots of coding into one file is a BAD idea.

-Shoving a HUD system, menus, ect. into one file makes it hard to find stuff you need. Having too many things in the same file will also make it harder to pinpoint errors in your code and you will face the risk of a single syntax error breaking the whole thing. Say you have 200 lines of codes for a menu or two. What you want to do is make a seperate folder for that large bit of coding like menus, hud, ect.

cl_init
  -cl_vgui
  -cl_hud
  -cl_help


Cross.png WARNING: Don't make a whole file just for 1 function, when you finish your gamemode you will have 9000 files and folder size will increase greatly due to fragmentation!

cl_init
   -cl_showteammenu
   -cl_menu1
   -cl_menu2
   -cl_menu3
   -cl_menu4
   -cl_healthbar
   -cl_armorbar
   -cl_setteam
   -cl_help1
Personal tools
Namespaces
Variants
Actions
Navigation
Lua Scripting
Functions
Hooks
Toolbox