Tuesday, December 1, 2009

discovering lua

Well, as i was playing game after the exam, i found out that many of the add-ons in games are programmed in lua, and as programming students, i think i should write my own add-on, so as to make it more cater to my need and it is cool to do that. Therefore, I search the net and find out that lua is actually a very interesting programming language.
Lua is a lightweight, reflective, imperative and functionalprogramming language, designed as a scripting language with extensible semantics as a primary goal. The name comes from the Portuguese word lua meaning "moon". Lua has a relatively simple C API compared to other scripting languages.
Because both Lua and JavaScript use prototype-based objects and were influenced by Scheme, they feature many common semantics, despite the great differences in syntax. In its design, Lua is also similar to Icon, perhaps due to both of them being influenced by SNOBOL.

Lua is widely used in the video game industry. Apart from games, Lua has been used in many applications, both commercial and non-commercial.

for i=1,5 do  

print("i is now " .. i) //here ".." is a sign to connect two string it is like the "+" in Java

end


this is a sample of a loop in lua.and the result should be:
i is now 1

i is now 2

i is now 3

i is now 4

i is now 5

What i find about lua is that it is very familiar to Java and C syntax, most of the code I am able to understand.

Besides loop, lua also have other functions such as if-else statements, array, however, in lua, there is no "{ }" which we use in java.
The function of "{ }"is done by the reserved word "end".
if op == "+" then

r = a + b

elseif op == "-" then

r = a - b

elseif op == "*" then

r = a*b

elseif op == "/" then

r = a/b

else

error("invalid operation")

end
this is a sample of one if and many else statement. I notice that there is only one end at behind, which means a if only need one end even it has many else.
lua use table to save data .And array can be written in term of table.


myData = {}

myData[0] = “foo”

myData[1] = 42

-- Hash tables
myData[“bar”] = “baz”

-- Iterate through the
-- structure
for key, value in myData do
print(key .. “=“ .. value)
end

this is a sample of table. that real programmes are quite different from the simple programmes we do in class.A add-on of size 300KB is so long that make me feel dizzy. I will work harder and try to make my own add-on.




No comments:

Post a Comment

Followers