{"id":82,"date":"2023-09-21T13:15:16","date_gmt":"2023-09-21T11:15:16","guid":{"rendered":"https:\/\/extendsclass.com\/blog\/?p=82"},"modified":"2023-05-18T22:23:33","modified_gmt":"2023-05-18T20:23:33","slug":"lua-cheatsheet","status":"publish","type":"post","link":"https:\/\/extendsclass.com\/blog\/lua-cheatsheet","title":{"rendered":"Lua cheatsheet"},"content":{"rendered":"\n<p>Lua is a lightweight, fast, and versatile scripting language known for its simplicity and extensibility, making it popular for embedding in applications and game development.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_47_1 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"ez-toc-toggle-icon-1\"><label for=\"item-69daafb15b9d5\" aria-label=\"Table of Content\"><span style=\"display: flex;align-items: center;width: 35px;height: 30px;justify-content: center;direction:ltr;\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/label><input  type=\"checkbox\" id=\"item-69daafb15b9d5\"><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/extendsclass.com\/blog\/lua-cheatsheet\/#Conditionals\" title=\"Conditionals\">Conditionals<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/extendsclass.com\/blog\/lua-cheatsheet\/#Loops\" title=\"Loops\">Loops<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/extendsclass.com\/blog\/lua-cheatsheet\/#Variables\" title=\"  Variables\">  Variables<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/extendsclass.com\/blog\/lua-cheatsheet\/#Tables_Arrays\" title=\"Tables \/ Arrays\">Tables \/ Arrays<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/extendsclass.com\/blog\/lua-cheatsheet\/#Functions\" title=\"Functions\">Functions<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/extendsclass.com\/blog\/lua-cheatsheet\/#Comments\" title=\"Comments\">Comments<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conditionals\"><\/span>Conditionals<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>if condition then\n  -- code\nelseif condition then\n  -- code\nelse\n  -- code\nend<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Loops\"><\/span>Loops<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>while condition do\n-- use break to break end\nend\n\nfor i = start, stop, step do\n    -- code\nend\n\nfor key, value in pairs(table) do\n    -- code\nend\n\nrepeat\n    -- code\nuntil condition<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Variables\"><\/span>  Variables<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>local x = 1\ny, z = 2, 4<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Tables_Arrays\"><\/span>Tables \/ Arrays<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>table = {key1 = value1, key2 = value2, ...}\nvalue = table&#91;key]\ntable&#91;key] = value\nfor key, value in pairs(table) do\n    -- code\nend\n\narray = {element1, element2, ...}\nvalue = array&#91;index]\narray&#91;index] = value\nlength = #array<\/code><\/pre>\n\n\n\n<p>Arrays are a specific form of tables in Lua, often starting from index 1 and maintaining contiguous indices. Tables, on the other hand, can have non-numeric keys and non-contiguous indices, allowing for more flexible data structures.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Functions\"><\/span>Functions<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>function functionName(arg1, arg2)\n    -- code\nend\n\nfunctionName(arg1, arg2)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Comments\"><\/span>Comments<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>-- comment\n--&#91;&#91; Multiline\n     comment ]]<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Lua is a lightweight, high-level scripting language known for its simplicity, flexibility, and efficiency. Designed to be embedded in applications, Lua offers a straightforward syntax that emphasizes readability and ease of use. It provides powerful features such as dynamic typing, automatic memory management, and first-class functions.<\/p>\n\n\n\n<p>Lua&#8217;s versatility extends to various domains, including game development, where it has been used in popular games like World of Warcraft and Angry Birds. Its small footprint and fast execution make it ideal for resource-constrained environments, making Lua a popular choice for embedded systems and scripting tasks.<\/p>\n\n\n\n<p>One of Lua&#8217;s standout features is its extensibility. It offers a C API that allows developers to integrate Lua seamlessly with other languages and frameworks. Additionally, Lua&#8217;s ability to define and manipulate functions as data makes it a powerful tool for implementing domain-specific languages and customizable scripting interfaces.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Lua is a lightweight, fast, and versatile scripting language known for its simplicity and extensibility, making it popular for embedding in applications and game development.<\/p>\n","protected":false},"author":1,"featured_media":117,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_sitemap_exclude":false,"_sitemap_priority":"","_sitemap_frequency":""},"categories":[2],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/posts\/82"}],"collection":[{"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/comments?post=82"}],"version-history":[{"count":8,"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/posts\/82\/revisions"}],"predecessor-version":[{"id":89,"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/posts\/82\/revisions\/89"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/media\/117"}],"wp:attachment":[{"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/media?parent=82"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/categories?post=82"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/tags?post=82"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}