
| Lua OpenGL Surface Iso-surface 3D plot Sub-plots |
Lua BasicsAs a scripting language, Lua is small and yet powerful. Users of zeGraph only need to know the very basics. All programming languages deal with variable, assignment, control block, looping, and user-defined function. After learned these grammers of Lua and the basics of OpenGL, you are ready to use zeGraph. This note summarizes Lua basics and highlights Lua's special features. For more details, please refer to the online user's guide and the language manual. 1. Comments-- this a one line short comment
--[[ a multi-line comment like this is very convenient for
code documentation]]
2. VariablesVariables in Lua are typeless - they accepts any data type that Lua supports in an assignment. The data types include nil, boolean, number, string, function, userdata, table, and thread. Comparing with many other languages, treating function, userdata, table, and thread as variable is unique to Lua. Because OpenGL is not thread safe, using the thread with zeGraph is not recommended; so you can bypass learning this data type. Using nil, Boolean, number, and string is simple: -- nil is automatically assigned to a variable declared as local local a -- this statement assigns true to b, 10.1 to c, and "Hello" to name. b, c, name = true, 10.1, "Hello!" -- String can be concatenated by operator .. name = "John: " .. name print(a, b, c, name) --[[output: nil true 10.1 John: Hello! ]] Because a function is a variable, you should be careful not to use a function name as a variable accidentally. A function may return more than one results:
function plus(x, y)
local z = x + y
return x, y, z
end
print(plus(1, 2))
--[[output:
1 2 3
]]
The table and userdata variable types are unique to Lua. As implied by the name, a userdata is defined by the host application. For example all zeGraph objects are userdata. Because a table is a kind of associate array and can contain any type of variables, it can be used in such a situation as returning structured data by a function. 3. ControlThe control structure of Lua has the form: if a > b then
print(a)
elseif (a < b) then -- this part and () is optional
print(b)
else -- this part is also optional
print(a, b)
end
4. LoopsThere are three kinds of loops: for, while, and repeat. for k = 0, 10, 2 do -- if the step is one, it can be omitted.
print(k)
end
t = {0, 2, 4, 6, 8, 10}
for k, v in t do
print(k, v) -- k (1 to 6) is the key and v is the value (0 to 10)
end
for k in t do
print(k) -- k is the key in table t, not the value
end
k = 0
while k <= 10 do
print(k) -- show the same results as the first for-loop
k = k + 1
end
repeat
k = k - 1
print(k) -- show the reverse of numbers of the while-loop
until k = 0
|