Thread Tools Display Modes
11/11/14, 09:15 AM   #1
justinbarrett
Join Date: Nov 2014
Posts: 25
looking for direction

ok, I have asked a few things already in this forum, and I assumed I could just jump in a get to making an addon or two...but it is now abundantly clear that I am lost
I do not understand some of the basic concepts of the language...

how do I declare a global variable?

these "[]" do not encapsulate a function or action...

where are all the semicolans

so I am wondering if someone, anyone, could point me to a LUA for beginners tutorial...not a tutorial that tells me how to do something specific, but that breaks down the syntax for me....

how to write a basic function

how do I declare variables(global or local)

etc....just a very simple tutorial...I could probably get through it in a few minutes based on my current programming skills but I need direction...thank you.
please, no youtube, or video. I am using a slow connection and I assume a simple page or two on the internet should do the trick

Last edited by justinbarrett : 11/11/14 at 09:40 AM.
  Reply With Quote
11/11/14, 10:08 AM   #2
merlight
AddOn Author - Click to view addons
Join Date: Jul 2014
Posts: 671
I learned it from here and reading other people's code.

Local variables and functions are declared with the local keyword. Everything else is global (well, there's a way around this, but I'm pretty sure you won't need that any time soon, so let's not complicate it ) Global variables may also be accessed via the _G table.

Each statement is terminated by a semicolon, a newline or a block-closing keyword (end). Thus semicolons are mostly optional, and I'd even say undesirable in readable code. One statement per line is usually better.
  Reply With Quote
11/11/14, 10:12 AM   #3
justinbarrett
Join Date: Nov 2014
Posts: 25
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
  Reply With Quote
11/11/14, 10:31 AM   #4
merlight
AddOn Author - Click to view addons
Join Date: Jul 2014
Posts: 671
Everything that's not local is global. Simple as that. Scope has nothing to do with the type of values you can store in variables.

Lua Code:
  1. g_str = "hello world" -- string stored in global var
  2. g_num = 5 -- number
  3. g_tab = { foo = 5, bar = 7} -- table

Tables are a hybrid between what other languages call dictionary/mapping/hashtable and list/array/vector. Lua likes to look smart, so you can have a table indexed by any type of value, or even a mix of types (strings, numbers, even functions). If you happen to use a contiguous sequence of numbers 1..N as indices, it happens to be an indexed array. I suggest you read a lot, and carefully, about tables, they might be a bit tough to grasp at first, but in the end the hybrid nature makes them super easy to use
  Reply With Quote
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
11/11/14, 01:53 PM   #6
justinbarrett
Join Date: Nov 2014
Posts: 25
ok, ty...looks like I am making this far more complicated than it needs to be. and accessing the table seems easy enough similar to arrays and other stuff in c...ok, I think I got just about all I need thank you.
  Reply With Quote
11/11/14, 09:45 PM   #7
katkat42
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 155
Yeah, pretty much the only other thing I would add is that LUA and its ilk bring new meaning to the term "loosely-typed." Variables don't have data types -- just the values stored within them do. So
Lua Code:
  1. local foo = true
  2. foo = 15
  3. foo = "Hello World!"
  4. foo = { "a", "b", "c" }
  5. foo = function() return 42 end
would cause no problems whatsoever.
  Reply With Quote
11/11/14, 09:56 PM   #8
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Feb 2014
Posts: 648
http://lua-users.org/wiki/TutorialDirectory
  Reply With Quote
11/12/14, 11:10 AM   #9
Garkin
 
Garkin's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 832
http://www.luafaq.org/gotchas.html
  Reply With Quote
11/13/14, 03:55 PM   #10
mctaylor
 
mctaylor's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 21
Originally Posted by justinbarrett View Post
... accessing the table seems easy enough similar to arrays and other stuff in c...
Lua is all about Tables. Tables are really more like dictionaries, associative arrays or hash tables, rather than merely indexed arrays.

They can be misused^H^H^H, coerced into Classes for Object-Oriented Programming style, and are a flexible and powerful replacement for C's struct.

Learn the two styles of for loops (numeric, and generic) and Tables, and you'll have a good grasp of Lua.
  Reply With Quote

ESOUI » Developer Discussions » General Authoring Discussion » looking for direction

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off