{"id":48,"date":"2023-02-15T12:29:14","date_gmt":"2023-02-15T12:29:14","guid":{"rendered":"http:\/\/blog.aryapurwadana.com\/?p=48"},"modified":"2023-02-15T12:29:16","modified_gmt":"2023-02-15T12:29:16","slug":"lua-introduction","status":"publish","type":"post","link":"https:\/\/blog.aryapurwadana.com\/index.php\/2023\/02\/15\/lua-introduction\/","title":{"rendered":"Lua &#8211; Introduction"},"content":{"rendered":"\n<p id=\"7313\">Lua is a simple and powerful scripting language. It supports a variety of programming paradigms, including procedural, object-oriented, and functional programming. While Lua can also be used as a standalone language, it is more common nowadays to embed it within other applications. Lua was designed with speed, portability, and ease-of-use in mind.<\/p>\n\n\n\n<p id=\"b905\">Lua is also one of the leading scripting languages used in game development, due to its simple syntax and short learning curve.<\/p>\n\n\n\n<p id=\"c23b\"><strong>A brief history<\/strong><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/256\/1*pJF3CSzdvUzo6zUq6sQ4Uw.jpeg\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Sol for Sun, Lua for Moon <\/figcaption><\/figure>\n<\/div>\n\n\n<p id=\"d998\">Lua was created in 1993 by members of the Computer Graphics Technology Group at the Pontifical Catholic University of Rio de Janeiro in Brazil. One of Lua\u2019s predecessors was SOL, or <em>Simple Object Language<\/em>, a language mainly used for data-description and configuration. The first version of Lua was designed in a way that its object constructors incorporated the data-description syntax of SOL. <em>Sol <\/em>is also the Portuguese word for Sun. The name <em>Lua<\/em>, meaning Moon in Portuguese, was coined to symbolize it as being the successor to SOL.<\/p>\n\n\n\n<p id=\"b881\"><strong>Why use Lua<\/strong>?<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/147\/1*qct6ZAApK8iEs06XVNE7Og.png\" alt=\"\" width=\"211\" height=\"93\"\/><figcaption class=\"wp-element-caption\">LuaJIT for even more speed!<\/figcaption><\/figure>\n<\/div>\n\n\n<p id=\"7d72\"><strong>It is simple and fast.<\/strong><\/p>\n\n\n\n<p id=\"1d75\">As if it hasn\u2019t been stressed enough, Lua is a breeze to learn due to its simple syntax. This also means that compiling a Lua application is equally as fast. While native Lua compilation is already fast by itself, a <a href=\"https:\/\/luajit.org\/\" rel=\"noreferrer noopener\" target=\"_blank\">Lua JIT compiler<\/a> is also available for even faster compiling.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/432\/1*Uj9OvLJqWvH4sZxZyzldHg.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Lua is lightweight!<\/figcaption><\/figure>\n<\/div>\n\n\n<p id=\"2acb\"><strong>It is portable and lightweight.<\/strong><\/p>\n\n\n\n<p id=\"ebd8\">Installing Lua on your system is easy and would not cause it to bloat at all. It simply involves downloading either a zip file (for Windows) or a tar file (for Linux and MacOS) from the official <a href=\"http:\/\/luabinaries.sourceforge.net\/\" rel=\"noreferrer noopener\" target=\"_blank\">Lua <em>SourceFourge<\/em> website<\/a> that contains all the binary files you would need. You just need to either unzip the file (for Windows) or issue a few commands in the terminal to extract the tar file contents (for Linux and MacOS), and you\u2019d be ready to code Lua scripts in no time. Once done, the Lua binaries will only take up more or less 900 kB of space in your system!<\/p>\n\n\n\n<p id=\"51ff\">An IDE is not required at all, though the official website recommends <a href=\"https:\/\/studio.zerobrane.com\/\" rel=\"noreferrer noopener\" target=\"_blank\">ZeroBrane Studio<\/a>.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/441\/1*aUGAZpBOYUw-j9DUZqVdaA.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">L\u00d6VE, a game engine that uses Lua scripting<\/figcaption><\/figure>\n<\/div>\n\n\n<p id=\"a2a9\"><strong>You can create games with it!<\/strong><\/p>\n\n\n\n<p id=\"8676\">Some of the popular games that uses Lua Scripting include the <em>Angry Birds<\/em> and <em>World of Warcraft <\/em>series. In game development, there are times when you want to be able to perform a simple operation, such as make the character move in the right direction when you press the arrow keys. Or perhaps, you want to change the colour of the character\u2019s outfit. By embedding Lua in your application, you can let Lua take care of all these simple tasks, and let the main programming language take care of the more complex tasks.<\/p>\n\n\n\n<p id=\"402f\">A popular game engine that uses Lua Scripting is <a href=\"https:\/\/love2d.org\/\" rel=\"noreferrer noopener\" target=\"_blank\">L\u00d6VE <\/a>(or L\u00d6VE2D).<\/p>\n\n\n\n<p id=\"d191\"><strong>Lua Features<\/strong><\/p>\n\n\n\n<p id=\"6f53\">Here are some of highlights of Lua:<\/p>\n\n\n\n<p id=\"3fa3\"><strong>1) It is a dynamically-typed language.<\/strong><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/700\/1*NVGZni87xyvhHgq0zWGukg.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Lua has dynamic typing<\/figcaption><\/figure>\n<\/div>\n\n\n<p id=\"02c0\">There is no need to assign variable data types in Lua. Variable data types are dynamically set once values are assigned to them.<\/p>\n\n\n\n<p id=\"8b63\">One quirk of Lua is the <em>nil<\/em> data type. If Lua cannot find a variable with a specific name defined within the source code, <em>nil<\/em> would be returned. In other programming languages, this would result in a compilation or runtime error.<\/p>\n\n\n\n<p id=\"770e\"><strong>2) All variables have global scope by default.<\/strong><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/473\/1*qiQB4f3pIoT-6cA8qgHGdQ.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Variables are global by default<\/figcaption><\/figure>\n<\/div>\n\n\n<p id=\"23c1\">When you declare a variable in Lua, it has global scope by default. You can choose to add an optional <strong>local <\/strong>keyword to explicitly limit its scope within a function, for example.<\/p>\n\n\n\n<p id=\"97a1\"><strong>3) There is only one data structure in Lua.<\/strong><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/576\/1*8ewclKsVojOwKkYTgwgXog.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Tables and Metatables in action<\/figcaption><\/figure>\n<\/div>\n\n\n<p id=\"b9e3\">The only native data structure in Lua is the <strong>table<\/strong>. Tables are extremely flexible, though. They can act as simple linear data types like arrays and linked lists, but through the help of a special table called a <strong>metatable<\/strong>, tables can take on more complex tasks. In my next blog post, I will be talking more about tables, the forms they can take, and how to do basic objected-oriented programming using tables and metatables. Stay tuned!<\/p>\n","protected":false},"excerpt":{"rendered":"Lua is a simple and powerful scripting language. It supports a variety of programming paradigms, including procedural, object-oriented,&hellip;\n","protected":false},"author":1,"featured_media":49,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"csco_singular_sidebar":"","csco_page_header_type":"","csco_appearance_grid":"","csco_page_load_nextpost":"","csco_post_video_location":[],"csco_post_video_location_hash":"","csco_post_video_url":"","csco_post_video_bg_start_time":0,"csco_post_video_bg_end_time":0,"footnotes":""},"categories":[11,4],"tags":[],"class_list":{"0":"post-48","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-lua","8":"category-programming","9":"cs-entry","10":"cs-video-wrap"},"_links":{"self":[{"href":"https:\/\/blog.aryapurwadana.com\/index.php\/wp-json\/wp\/v2\/posts\/48","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.aryapurwadana.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.aryapurwadana.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.aryapurwadana.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.aryapurwadana.com\/index.php\/wp-json\/wp\/v2\/comments?post=48"}],"version-history":[{"count":1,"href":"https:\/\/blog.aryapurwadana.com\/index.php\/wp-json\/wp\/v2\/posts\/48\/revisions"}],"predecessor-version":[{"id":50,"href":"https:\/\/blog.aryapurwadana.com\/index.php\/wp-json\/wp\/v2\/posts\/48\/revisions\/50"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.aryapurwadana.com\/index.php\/wp-json\/wp\/v2\/media\/49"}],"wp:attachment":[{"href":"https:\/\/blog.aryapurwadana.com\/index.php\/wp-json\/wp\/v2\/media?parent=48"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.aryapurwadana.com\/index.php\/wp-json\/wp\/v2\/categories?post=48"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.aryapurwadana.com\/index.php\/wp-json\/wp\/v2\/tags?post=48"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}