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 :) |
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. |
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 :) |
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:
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 ;) |
Quote:
Lua Code:
A local variable would be defined with the word local: Lua Code:
If you want a global you just leave off the word local Lua Code:
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:
You can put stuff in your table in more than one way, one example would be while you are creating the table: Lua Code:
They can then be accessed by calling: Lua Code:
Items can also be placed in tables by an integer index: Lua Code:
And then called upon by doing: Lua Code:
|
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.
|
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:
|
|
|
Quote:
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