ESOUI

ESOUI (https://www.esoui.com/forums/index.php)
-   General Authoring Discussion (https://www.esoui.com/forums/forumdisplay.php?f=174)
-   -   looking for direction (https://www.esoui.com/forums/showthread.php?t=2425)

justinbarrett 11/11/14 09:15 AM

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 :)

merlight 11/11/14 10:08 AM

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.

justinbarrett 11/11/14 10:12 AM

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 :)

merlight 11/11/14 10:31 AM

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 ;)

circonian 11/11/14 10:44 AM

Quote:

Originally Posted by justinbarrett (Post 13160)
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

justinbarrett 11/11/14 01:53 PM

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.

katkat42 11/11/14 09:45 PM

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.

Seerah 11/11/14 09:56 PM

http://lua-users.org/wiki/TutorialDirectory :D

Garkin 11/12/14 11:10 AM

http://www.luafaq.org/gotchas.html

mctaylor 11/13/14 03:55 PM

Quote:

Originally Posted by justinbarrett (Post 13167)
... 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.


All times are GMT -6. The time now is 09:50 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2014 - 2022 MMOUI