[LUA] LUA concepts and basics

Status
Not open for further replies.

Adil

DevBest CEO
May 28, 2011
1,276
714
Before we begin, an introduction to LUA


What is Lua?


Lua is a powerful, fast, lightweight, embeddable scripting language.


Lua combines simple procedural syntax with powerful data description constructs based on associative arrays and extensible semantics. Lua is dynamically typed, runs by interpreting bytecode for a register-based virtual machine, and has automatic memory management with incremental garbage collection, making it ideal for configuration, scripting, and rapid prototyping.


Why choose Lua?


Lua is a proven, robust language


Lua has been used in many industrial applications (e.g., Adobe's Photoshop Lightroom), with an emphasis on embedded systems (e.g., the Ginga middleware for digital TV in Brazil) and games (e.g., World of Warcraft). Lua is currently the leading scripting language in games. Lua has a solid reference manual and there are several books about it. Several versions of Lua have been released and used in real applications since its creation in 1993. Lua featured in HOPL III, the Third ACM SIGPLAN History of Programming Languages Conference, in June 2007.


Lua is fast


Lua has a deserved reputation for performance. To claim to be "as fast as Lua" is an aspiration of other scripting languages. Several benchmarks show Lua as the fastest language in the realm of interpreted scripting languages. Lua is fast not only in fine-tuned benchmark programs, but in real life too. A substantial fraction of large applications have been written in Lua.


Lua is portable


Lua is distributed in a small package and builds out-of-the-box in all platforms that have an ANSI/ISO C compiler. Lua runs on all flavors of Unix and Windows, and also on mobile devices (such as handheld computers and cell phones that use BREW, Symbian, Pocket PC, etc.) and embedded microprocessors (such as ARM and Rabbit) for applications like Lego MindStorms.


For specific reasons why Lua is a good choice also for constrained devices, read this summary by Mike Pall. See also a poster created by Timm Müller.


Lua is embeddable


Lua is a fast language engine with small footprint that you can embed easily into your application. Lua has a simple and well documented API that allows strong integration with code written in other languages. It is easy to extend Lua with libraries written in other languages. It is also easy to extend programs written in other languages with Lua. Lua has been used to extend programs written not only in C and C++, but also in Java, C#, Smalltalk, Fortran, Ada, Erlang, and even in other scripting languages, such as Perl and Ruby.


Lua is powerful (but simple)


A fundamental concept in the design of Lua is to provide meta-mechanisms for implementing features, instead of providing a host of features directly in the language. For example, although Lua is not a pure object-oriented language, it does provide meta-mechanisms for implementing classes and inheritance. Lua's meta-mechanisms bring an economy of concepts and keep the language small, while allowing the semantics to be extended in unconventional ways.


Lua is small


Adding Lua to an application does not bloat it. The tarball for Lua 5.1.4, which contains source code, documentation, and examples, takes 212K compressed and 860K uncompressed. The source contains around 17000 lines of C. Under Linux, the Lua interpreter built with all standard Lua libraries takes 153K and the Lua library takes 203K.


Lua is free


Lua is free open-source software, distributed under a very liberal license (the well-known MIT license). It may be used for any purpose, including commercial purposes, at absolutely no cost.


Now we've got that out of the way, let us begin. Before coding any LUA, we need to download the LUA tarball, from the official LUA site.
Code:
[URL]http://www.lua.org/ftp/lua-5.1.tar.gz[/URL]


1. Download and extract LUA to C:\lua
2. After you have downloaded and installed LUA, navigate to C:\lua
3. After you have opened the folder, you need to click on lua5.1
qZqOp0.jpg

4. Now, click on it and run it. You should come to a command-line interface
01igus.jpg



Now, let's begin coding.
1. Hello world
Code:
Type print("hello world")
This tells the interpreter to print whatever is in the brackets and speech marks. You could type print("Hi").


2. Basic variables
Code:
 x = 7
print(x)


This stores x as the number 7, and then prints the value of x in the next line


3. Using strings
Code:
 who = "LUA user"
print(who)


This is like tutorial 2, but this remembers a string, and prints the string when the word who is typed.


More tutorials coming soon
 

Kryptos

prjRev.com
Jul 21, 2010
2,205
1,252
Nice explanation!

Looking forward for more tuts - I'm kinda interested in learning this! ;D
 

Adil

DevBest CEO
May 28, 2011
1,276
714
Nice explanation!Looking forward for more tuts - I'm kinda interested in learning this! ;D
Thanks for the comments. :D. I'll be updating this with tutorials on mathematics in LUA, and other more advanced things :)
 

Wikipedia

Dont fluff my new pillow
Aug 4, 2011
85
1
Thanks for the comments. :D. I'll be updating this with tutorials on mathematics in LUA, and other more advanced things :)

Well i can't wait though untill you'r bringing out the new things i love it so mutch
 

habbz

Custom Title bitchssssssss
Nov 12, 2010
227
1
I can't extract it, winrar says: "No archives found in selected files and folders"
 

Adil

DevBest CEO
May 28, 2011
1,276
714
updated server binary, an alternative to the LUA link.
Lesson 4: Math(s) and variables
Code:
x = 24
y = 1.3
c = (x+y)
print (c*y/x)
What this code is telling the interpreter:
The value 'y' is equal to 24
The value 'x' is equal to 1.3
The value 'c' is equal to the sum of x and y
print (c*y/x) simply tells the interpreter to execute the script in the brackets and print it. However, this does not follow the BODMAS (Brackets or Division, Multiplication, Addition, Subtraction) rule.
 

Kryptos

prjRev.com
Jul 21, 2010
2,205
1,252
Nice, nice!

I have a request: Can you make another thread with more advance stuff? Such as teaching integration with other languages like PHP or ASP, how to get values from files, read files, db handling, and such.

Would be greatly appreciated ;D
 

habbz

Custom Title bitchssssssss
Nov 12, 2010
227
1
*facepalm* Is this for Windows? i went lua.org and got one for windows, its separate from the others i think.
anyways what you gave me says thats its an invalid win32 application. i used lua on the site though, its awesome! thanks for the TUT
 

Adil

DevBest CEO
May 28, 2011
1,276
714
*facepalm* Is this for Windows? i went lua.org and got one for windows, its separate from the others i think.
anyways what you gave me says thats its an invalid win32 application. i used lua on the site though, its awesome! thanks for the TUT
I have a 64 bit system, that's why :p. Anyway, thanks for the good comments.
Chapter 6: Advanced stuff

Code:
a = 25
b = 10
C = a*b

if C < 25
print(C is greater than 25)
else
print(C is lesser)
end
First, we are declaring variables. Next, we are putting those variables into a math sum. The 'if' statement is used as a conditional statement. The 'else' simply means any result which differs from the result we'd like. The 'end' ends the if statement block of code.
 

habbz

Custom Title bitchssssssss
Nov 12, 2010
227
1
Can i addon? you can do simple math [or any math] by doing this:
Code:
 print(5 * 6)
or any math problem [as far as ive gotten anyway]
 

Kryptos

prjRev.com
Jul 21, 2010
2,205
1,252
Can i addon? you can do simple math [or any math] by doing this:
Code:
 print(5 * 6)
or any math problem [as far as ive gotten anyway]

Yeah, he already explained that ^^

I hope you keep this thread because I like it ;D

Good job!
 

Adil

DevBest CEO
May 28, 2011
1,276
714
LUA tutorial 7 : Tables
This is a bigger tutorial, with more information than usual.
Code:
[FONT=monospace]a = {}     -- create a table and store its reference in `a'
[/FONT][FONT=monospace]k = "x"
[/FONT][FONT=monospace]a[k] = 10        -- new entry, with key="x" and value=10
[/FONT][FONT=monospace]a[20] = "great"  -- new entry, with key=20 and value="great"
[/FONT][FONT=monospace]print(a["x"])    --> 10
[/FONT][FONT=monospace]k = 20
[/FONT][FONT=monospace]print(a[k])      --> "great"
[/FONT][FONT=monospace]a["x"] = a["x"] + 1     -- increments entry "x"
print(a["x"])    --> 11
[/FONT]
What are we doing here?
a={} is creating an empty table '{}' and assigning it to the character 'a'.
k="x" is telling the compiler, our key is the character 'x'
a[k]=10 is assigning the number ten to our key 'x'
a[20]="great" is assigning the key 20 with the value "great"
k=20 tells the compiler to assign the key 20 to the character 'k'
print(a[k]) is taking the key 'k' from the table 'a' and printing the value of 'k'
a["x"] = a["x"] + 1 is telling the interpreter to increment the value 'x'
print(a["x"]) tells the interpreter to print the value of 'x' which will now be 11

I may be releasing a LUA tutorial guide, which will cover far more in more detail than here.
Please check back for the latest news :)
 

Sledmore

Chaturbate Livestreamer
Staff member
FindRetros Moderator
Jul 24, 2010
5,195
3,903
Nice, release your guide as you say, and why not take your tutorials to another level, such as youtube? Good luck :).
 

Adil

DevBest CEO
May 28, 2011
1,276
714
Nice, release your guide as you say, and why not take your tutorials to another level, such as youtube? Good luck :).
A PDF file is coming, and thanks for the youtube suggestion, videos should start appearing soon

By the way, my Vista installation is a bit screwed up, so I'll start tutorials on embedding with other languages like PHP (a scripting language within another scripting language) and SQL etc, when I get Win7 installed, which will hopefully be within the week
 
Status
Not open for further replies.

Users who are viewing this thread

Top