编辑“︁
StendhalScripting/Lua
”︁(章节)
跳转到导航
跳转到搜索
警告:
您没有登录。如果您进行任何编辑,您的IP地址会公开展示。如果您
登录
或
创建账号
,您的编辑会以您的用户名署名,此外还有其他益处。
反垃圾检查。
不要
加入这个!
== Data Types == Some common data types in Lua are ''string'', ''integer'', ''boolean'', & ''table''. Type names do not need to be declared when setting variables. Examples: <pre> -- string variable local var1 = "Hello world!" -- integer variable local var2 = 11 -- boolean variable local var3 = true -- table variable local var4 = {} </pre> === Strings === ==== String Concatenation ==== String concatenation is simple, much like Java uses a plus operator (<code>+</code>) to join strings, Lua uses two periods (<code>..</code>). Example: <pre> -- create a string variable local var = "Hello" -- append another string var = var .. " world!" print(var) -- prints "Hello world!" </pre> === Tables === A Lua table is a data type similar to a Java list or map. Tables can be indexed or use key=value pairs. ''(<span style="color:red;">IMPORTANT NOTE: Lua table indexes begin at 1, not 0</span>)'' ==== Creating Tables ==== An empty table is initialized with a pair of curly braces (<code>{}</code>): <pre> local mytable = {} </pre> You can add values to indexed tables at initialization or with the <code>table.insert</code> method: <pre> -- create a table with values local mytable = {"foo"} -- add value table.insert(mytable, "bar") </pre> To create a key=value table, any of the following methods can be used to add values: <pre> -- all of these do the same thing, that is, assigning "bar" to mytable.foo local mytable { foo = "bar", ["foo"] = "bar", } mytable.foo = "bar" mytable["foo"] = "bar" </pre> ==== Accessing Table Values ==== Square brackets (<code>[]</code>) enclosing an index number are used to access values in indexed tables (''remember that Lua table indexes start at "1" not "0"''): <pre> local mytable = {"foo", "bar"} print(mytable[1]) -- prints "foo" print(mytable[2]) -- prints "bar" </pre> In a key=value table, values can be accessed by either enclosing the key string in square brackets or concatenating the key member using a <code>.</code>: <pre> local mytable = {foo="bar"} -- using square brackets print(mytable["foo"]) -- prints "bar" -- using concatenated member print(mytable.foo) -- prints "bar" </pre> ==== Iterating Tables ==== Tables can be iterated in a <code>for</code> loop using the <code>pairs</code> or <code>ipairs</code> iterators. Loops are terminated with the <code>end</code> keyword: <pre> local mytable = {"foo", "bar"} print("indexes:") for idx in pairs(mytable) do print(idx) end print("\nvalues:") for idx, value in pairs(mytable) do print(value) end </pre> Output: <pre> indexes: 1 2 values: foo bar </pre> Using a key=value table: <pre> local mytable = { ["foo"] = "hello", ["bar"] = " world!", } print("keys:") for key in pairs(mytable) do print(key) end print("\nvalues:") for key, value in pairs(mytable) do print(value) end </pre> Output: <pre> keys: foo bar values: hello world! </pre> See also: [http://lua-users.org/wiki/TablesTutorial Lua Tables Tutorial] === Functions === Like normal variables, functions can be declared as '''global''' or '''local''' & must be terminated with the <code>end</code> keyword. There are two ways to define functions with the <code>function</code> keyword: <pre> local function myFunction() print("Hello world!") end </pre> or <pre> local myFunction = function() print("Hello world!") end </pre> Functions can also be members of a table: <pre> local myTable = {} function myTable.myFunction() print("Hello world!") end </pre> or <pre> local myTable = {} myTable.myFunction = function() print("Hello world!") end </pre> or <pre> local myTable = { myFunction = function() print("Hello world!") end, } -- execute with myTable.myFunction() </pre>
摘要:
请注意,所有对gamedev的贡献均可能会被其他贡献者编辑、修改或删除。如果您不希望您的文字作品被随意编辑,请不要在此提交。
您同时也向我们承诺,您提交的内容为您自己所创作,或是复制自公共领域或类似自由来源(详情请见
Gamedev:著作权
)。
未经许可,请勿提交受著作权保护的作品!
取消
编辑帮助
(在新窗口中打开)
导航菜单
个人工具
未登录
讨论
贡献
创建账号
登录
命名空间
页面
讨论
不转换
不转换
简体
繁體
大陆简体
香港繁體
澳門繁體
大马简体
新加坡简体
臺灣正體
查看
阅读
编辑
查看历史
更多
搜索
导航
首页
最近更改
随机页面
MediaWiki帮助
工具
链入页面
相关更改
特殊页面
页面信息