View Single Post
11/11/14, 10:44 AM   #5
circonian
AddOn Author - Click to view addons
Join Date: May 2014
Posts: 613
Originally Posted by justinbarrett View Post
EDIT: ty for the link and the info. I'll be sure to look at it now.

ok looking through some code it seems the basic function is closed with "end"...
so..


function thisFunction()
do stuff here
end

is a basic function, as far as I can tell....
and local variables are simply

"local a = 0 "

I still cannot see how global variables are done though. I did see at the top of a few scripts something that could be an array, or list...not sure. I thought maybe things could be stored in them, but hey...noob here
Yes, that is a function and they are closed with the end statement.
Lua Code:
  1. function addFunction(value1, value2)
  2.    return value1 + value2
  3. end

A local variable would be defined with the word local:
Lua Code:
  1. -- this variable is local
  2. local counter = 1

If you want a global you just leave off the word local
Lua Code:
  1. -- this variable is global
  2. counter = 1

The thing you mentioned that looked like an array was a table. They have no pre-defined size, you just put stuff in them.
Lua Code:
  1. -- This is an empty table (has nothing in it):
  2. myTable = {}

You can put stuff in your table in more than one way, one example would be while you are creating the table:
Lua Code:
  1. myAddon = {
  2.   -- Notice the commas at the end of each statement in the table
  3.    -- Put my addons name in the table:
  4.    addonName = "My Addon",
  5.  
  6.    -- Put a function in the table:
  7.    fun1 = addFunction,
  8.  
  9.    -- Put another table in here:
  10.    someTable = myTable,
  11. }

They can then be accessed by calling:
Lua Code:
  1. myAddon.addonName    -- returns My Addon
  2. myAddon.fun1(1, 2)      -- returns 3
  3. -- ... --

Items can also be placed in tables by an integer index:
Lua Code:
  1. myTable = {
  2.    [1] = "My Addon Name",
  3.    [2] = "something else",
  4.    -- .... --
  5. }

And then called upon by doing:
Lua Code:
  1. myTable[1]    -- returns: My Addon Name
  2. myTable[2]    -- returns:  something else
  Reply With Quote