Advertisement
Guest User

Untitled

a guest
Jan 25th, 2015
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.29 KB | None | 0 0
  1.  
  2. Hello! It seems you've come across my Lua book for programming newbies!
  3. This book will guide you through the Lua language, explaining every step of the way, and maybe step outside the Lua world on occasion to give you a better idea of how things work. We'll also have some small projects including making our own small game to make things more fun for you and so you can test your newfound Lua and programming knowledge.
  4.  
  5. What is Lua? What is programming?
  6.  
  7. Depending on where you're coming from, you may not know what Lua is, or you might not have the right idea.
  8.  
  9. In programming, we have what are known as "programming languages": these are what we write in to tell the computer what to do. A programming language gets translated from something humans can easily read and write in, down to, in some way or another, CPU instructions which the computer can use to do something with. The quality of this translation determines how fast the program will run and how much memory it'll use up. Since we have ability to write these translators, and given such freedom, we've ended up with not just 1 or 2 or 3 programming languages, but hundreds, and new ones come up all the time. Lua is an example of a programming language, and so are the popular languages C, C++, Java, JavaScript (which isn't Java at all), Python, Ruby, and PHP (I told you there were many).
  10.  
  11. You can think of a programming language as a tool for the programmer to get his point across to the computer. Somewhat like how a construction worker would have a crane, hammer, and screwdriver, programming languages can be quite different and can be used for different things. However, a language is usually capable of doing anything (or most things) any other language can do.
  12.  
  13. In a nutshell, a programming language tells a computer (be it a phone, laptop, gaming console, robot, whatever) what it's supposed to do. Which means any functionality in your operating system, favourite game, website, or robot, was once written in code.
  14.  
  15. Why Lua though?
  16.  
  17. If there are so many programming languages and you know nothing about them, why have you chosen Lua and not something else?
  18. Lua makes an excellent first language. It's simple and small (perhaps the most important point here!), but it'll still teach you a wide variety of concepts in common with other languages. It's fun - easy to write in, and gives a lot of freedom. It can also run very, very fast, and on limited memory.
  19.  
  20. You can use Lua for almost anything, but it's most famous for being used in games: http://en.wikipedia.org/wiki/Category:Lua-scripted_video_games
  21.  
  22. Getting Lua installed
  23.  
  24. Before you can use Lua, you'll need to install an interpreter (what runs the code). There are 2 main interpreters - LuaJIT and the Lua STI (the "official" interpreter).
  25.  
  26. I'll go over the topic of interpreters in more detail later in the book, but for now all you need to know is that we're going to use Lua STI, which is the stable, up-to-date standard, and is easier to get installed and runs code fast, but you'll probably end up using LuaJIT later on, which is mostly stable (don't worry about it), not so up to date and harder to get installed, but super mega awesome "haha you can't catch me" fast.
  27.  
  28. Before I get carried away: INSTALLING LUA:
  29.  
  30. You'll need a good reliable installation or you won't be able to run your programs cleanly or even at all. Be sure to get this right, it's really important, sorry if it's a pain.
  31.  
  32. LuaDist is a nice project which lets you easily install Lua and a few extras. Go to http://luadist.org/ and go to the "Downloads" section, pick your platform. Then,
  33.  
  34. -On Windows you'll want to unzip the folder and place it wherever you like (I recommend either one of your Program Files folders under C:/ or in your downloads, you could also place it straight into C:/ if you want quick access).
  35.  
  36. -On a Mac...
  37.  
  38. -On Linux...
  39.  
  40. I also recommend you set up a folder somewhere to put all your Lua projects in, perhaps on the desktop if you're that kind of person, or in your downloads folder if you're that kind of person.
  41.  
  42. Running Lua files and accessing the Lua REPL
  43.  
  44. This might be annoying at first.
  45.  
  46. Sadly, you won't be able to run Lua files like you would a normal program. It's a bit more involved.
  47.  
  48. On Windows:
  49. Open up the command prompt by searching for cmd and then clicking on it or pressing the Winkey+R and then typing "cmd" and pressing enter.
  50. Type in "lua" and press enter.
  51. If you got something like "'lua' is not recognised as an internal or external command", you may have screwed up when setting your environment variable, or at an earlier stage. Sorry! I'm not sure of every single problem, but look over the installation section again, and if it still isn't working you can ask for help (see the "Getting Help" section).
  52. Otherwise, type "print('Heyo captain Jack!')" and press enter. Did you get back "Heyo captain Jack!"? If not, there may be something wrong.
  53.  
  54. This is the Lua REPL.
  55.  
  56. Now that you've got a nice Lua setup, let's dive right in, with a small-ish example.
  57.  
  58. -- small-ish example
  59. a_number = 5
  60. a_number = 6
  61. print(a_number)
  62. a_string = "Hey ;)" -- I promise I'm not creepy...
  63. print(a_string)
  64.  
  65. Go ahead and copy-paste this into a new file, save it with the extension ".lua" and run it.
  66. You should get this shown on your screen (the output):
  67.  
  68. >6
  69. >Hey ;)
  70.  
  71. Allow me to explain the code line-by-line.
  72.  
  73. "-- small-ish example"
  74.  
  75. This line right here is a comment. It doesn't do anything. At all. The "--" tells Lua to ignore the rest of the line, and you can write whatever you like.
  76. Comments are FAR from useless though! Whenever what you're doing might confuse another programmer (or even the future you), comments will help out!
  77. So...document, or "comment" your code where necessary, or you'll get shot in the ballsack/ovaries (perhaps by yourself...)
  78.  
  79. "a_number = 5"
  80.  
  81. This is a variable declaration. A variable in a programming language like Lua is some data/a value which can be changed, binded to a name.
  82. To declare a variable you write its name on the right, followed by an equals sign, and then its value.
  83.  
  84. "a_number = 6"
  85.  
  86. Just changing the a_number variable we have to a value of 6. In Lua changing and declaring (making new) variables is mostly similar.
  87.  
  88. "print(a_number)"
  89.  
  90. This might be a bit more confusing, partly because of the name 'print', and the parenthesis, but, this "print" thing is what's known as a function - functions are things, that do things, sometimes with things, that might give you back things too (but in this case we're just doing something.)
  91. We can call (run) functions by writing their name, followed by a pair of parenthesis. A lot of the time you'll want to give the function some value/s, and you can write those inside the parens.
  92.  
  93. So, here, we are calling the "print" function - which will display some text to the screen (not open up the printer selection menu and ask how many copies you want made), and we're telling it to print the value of our a_number variable.
  94.  
  95. "a_string = "Hey ;)" -- I promise I'm not creepy..."
  96.  
  97. Another variable declaration! This time instead of making our variable of type number, it is of type string. Strings are text. There are a few ways to write string values but most commonly just write some text inside of "" or ''
  98. Then, we add a very useful comment beside it.
  99.  
  100. print(a_string)
  101.  
  102. Print out our string (again, to the screen!!!)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement