User:Crazypotato4/Test: Difference between revisions
>Crazypotato4 please don't delete my page D: |
>Crazypotato4 No edit summary |
||
Line 1: | Line 1: | ||
This is for | Hello, welcome to [[User:Crazypotato4|crazypotato4's]] complete beginners' scripting guide! This guide is meant for beginners to learn all the basics of ROBLOX Lua. Make sure you read each word carefully, and study the guide, as if it were to help you study for the biggest test of the school year, because you don't want to miss anything. In most sections, there will be a link that says "Main Article". This link is just the main wiki page about the subject that the section is about. I suggest reading the version in this tutorial first, then if you really want to, you can check out the main page. | ||
__TOC__ | __TOC__ | ||
== | ==Introduction== | ||
===What is Scripting?!?=== | |||
=== | |||
The first thing you want to know is, what exactly is scripting? Scripting is a type of programming, which is just telling a computer what to do in a language that it understands. In scripting, instead of telling a computer what to do, you are telling an application, such as the ROBLOX game, what to do. Scripting is our way of communicating with the program that runs our games, which gives us endless possibilities. | |||
On ROBLOX, we use a language called ''Lua'' to script. It is a very easy language to learn, and commonly used as a scripting language. The version that we use is called ROBLOX Lua, or RBX.Lua for short. RBX.Lua has many things added, which I won't cover until later, and it has a few things removed as well so that people can't use ROBLOX to hack your computer. | |||
===Getting Started=== | |||
[[Studio|Main Article]] | |||
Now that you know <b>what</b> scripting is, you have to figure out <b>where</b> you're going to script. The ROBLOX script editor is in the [[Studio|ROBLOX Studio]] application. To open it, go to the Start Menu on your computer, and go to 'All Programs' (maybe just 'Programs' on older versions of Windows, and Mac users will have to figure it out yourselves). Scroll down to the ROBLOX folder, and click on the ROBLOX Studio application inside it. The studio browser should open up, and look something like this: | |||
[ Studio.png image ] | |||
Now, all those buttons may be intimidating to newer users, but it is all easy once you get the hang of it. The first thing you want to do is go to the Menu bar at the very top of the Studio window, and click on the 'View' button. In the drop-down menu, you will want to open up two things. First, you check the "Output", then, go to the Toolbars menu and check 'Command'. Your studio will now have a small bar at the bottom that you can type things into, and a large white panel taking up the bottom third of the game screen. | |||
[ Studio2.png image ] | |||
The two things that you just opened are going to be the tools that we use to start scripting. The output window is there to give us information on how scripts are running. If a script is broken, chances are, an error will show up in the output telling us what is wrong with our scripts, so we can fix them. The command bar is simply a place where we can type in code and press enter, then have the code instantly run, even if the game is paused. | |||
===Hello World!=== | |||
The first script that most people learn when learning a language is a script that will write "Hello, World!" in the output. This has something to do with that being the first thing written on a computer or something, but I really don't care much to explain this. Anyways, back on topic. The way that we tell a script to write something in the output is with the [[Function_Dump/Core_Functions#print_.28.C2.B7.C2.B7.C2.B7.29|print function]]. Don't worry, all of this stuff will be explained in due time. For now, all you need to know is that you write the word "print", followed by a set of parenthesis. | |||
<pre> | |||
print() | |||
</pre> | |||
In the parenthesis, we put whatever we want to print into the output. We want to write the words Hello, World! into the output, so we put that in the parenthesis, right? Let's try it. Try typing this into your command bar. | |||
<pre> | |||
print(Hello, World!) | |||
</pre> | |||
Now, if you were to run that, the output will yell confusing things at you with red text. That's how the output shows that there's an error, by telling you stuff in <font color="Red">red text</font>. Now why is that? Well, we can't just put words in our script, because the compiler, the code that makes the script run, will try to read it as coding instead of just plain words. To tell it that we want the Hello, World! part to be just words, we put it in quotation marks. | |||
<pre> | <pre> | ||
print("Hello, World!") | |||
</pre> | </pre> | ||
====?==== | When you run that in the command bar, it will successfully output the words "Hello, World!" into your output. Now how is that useful at all? Well, for one thing, it teaches you something, but the practical use of knowing how to write things into the output using the print function lets you find errors in your script that the output doesn't tell you. Again, this will all be explained soon! | ||
===Commenting your Code=== | |||
When you write code, you may want to type things that the script won't read. Maybe you want to leave a note to people on how to edit the script if you are putting a script on Free Models, or maybe you just want to make sure you can look at the script later and still know what it will do (trust me, that '''will''' be a problem if you make a long script and want to edit it after a few months or years), or maybe you even want to make part of your code not run, but not have to delete it. Well this is where comments come in. Comments simply tell the script not to look at part of what you type. You make comments by typing two dashes (minus signs): | |||
<pre>Code here -- this is a comment, and will not be read by the script | |||
More code here</pre> | |||
If you want to make a comment go for multiple lines, or maybe you just want to comment out part of a line, you write two dashes followed by two square opening brackets, then end it with two square closing brackets: | |||
<pre>Code here --[[this is a comment, and won't be looked at by the script]] more code here | |||
--[[this is another comment | |||
as you can see, this comment goes multiple lines. | |||
:D]]more code here</pre> | |||
==Lesson 1: Data and variables== | |||
You may be thinking right now, "O NOES! DERE ARE CONFUUZIN WURDS IN DA PAGE, SCRIPTING ISH 2 HARD 4 MEH!!!1!!11!!one!". Well don't worry, this is all very easy to understand, just read this part slowly and carefully. | |||
===Booleans=== | |||
[[Boolean|Main Article]] | |||
The first data type that I will explain is the boolean type. Yes, the name is silly, and you probably haven't heard it before. Well, booleans are very simple, as they are just the values <b>true</b> and <b>false</b>. It's really as simple as that, true and false, yes and no, on and off. That's it. See, simple! | |||
===Numbers=== | |||
Numbers are a more complex data type, but you have probably heard of numbers before. If you haven't, I suggest paying a lot more attention in school. There are actually two types of numbers in programming, although in Lua, only one of them is really used. | |||
====Integers==== | |||
[[Integer|Main Article]] | |||
Integers are the more basic type of number. They are just all <whole> numbers, both positive and negative. In programming, there is a limit to how high a number can go, because only a certain amount of data is given to each number, so it can only take up that much data. It's as if the numbers (and all other data) are water and they are held in certain sizes of bottles, they can't have too much liquid or it will overflow. In programming, the integer type goes to a maximum of <b>2147483647</b> and can go all the way down to <b>-2147483648</b>. Don't worry, you shouldn't be working with numbers that big anytime soon. Plus, the scripting language that we use, <b>Lua</b>, doesn't use integers, they use the float type, which we will discuss next. | |||
====Floats==== | |||
[[Number|Main Article]] | |||
What exactly is a float? Why is it categorized under "Number"? We aren't talking about the big balloon cars at parades or anything, we are talking about numbers. <b>Float</b> is short for <i>floating point single precision number</i>. Don't worry, it's not complicated. Let's break it down. | |||
<b>floating point</b> - That means that the decimal point can move around, so you can have your number be 2.4, then move the decimal to the left and change the number to something like 1.7309. | |||
<b>single precision</b> - That is just saying that the number has single precision, so it loses its accuracy around six decimal places. | |||
<b>number</b> - This should be obvious. If it needs explanation, then go ask your mom or your math teacher or someone. You can even do a Google search and get all sorts of stuff about programming and math folklore. | |||
In Lua, all numbers are actually ''doubles'', which are just like floats, but more accurate. There is no other number type in Lua. Yes, we just talked about an integer type, but that is just in general programming. Lua doesn't use integers. | |||
===Strings=== | |||
[[String|Main Article]] | |||
This should be the first thing in this tutorial to actually make your brain work to understand it. The rest of it has been child's play. | |||
Strings are just a bunch of characters strung together. But what are characters? They aren't the little people in your video games, they're just single letters, numbers, and anything else you can type on your keyboard. Even spaces. Even things that you <b>can't</b> type in on your keyboard. Everything is a character. | |||
To create a string, you can do one of four things. | |||
# Surround stuff with quotation marks (remember back in the Hello World part of this tutorial? That was a string!) Ex. | |||
<pre> "This is a string" </pre> | |||
# Surround stuff with apostrophes (single quote marks) Ex. | |||
<pre> 'This is also a string' </pre> | |||
# Surround stuff with two brackets (the sqaure thingys). These strings can take up multiple lines. Ex. | |||
<pre> [[This is a string. | |||
This is still a string :D | |||
ya im still a string. ujelly?]] </pre> | |||
# Surround stuff with one bracket, then any amount of equals signs, then another bracket. The amount of equals signs on both sides of the string must match. This type of string can also contain multiple lines. Ex. | |||
<pre> [===[This is an epic string | |||
:D]==] </pre> | |||
The first method of making strings is the preferred one, since it colors the text pink when you type it in an actual script (which I surprisingly haven't shown how to insert). One reason that there are so many types of strings, though, is so that you can stack the symbols inside each other. For example: | |||
<pre> "hi this is a string "this is supposed to be a quote mark inside the string, but instead it just ends the string, so now this is treated like real code"this is another string"</pre> | |||
Instead of breaking your script by doing that, you can just use different kinds of strings. | |||
<pre> [[this is a string "this is still in the first string, it didn't start a new string or end the first one :D" and this is all still a string]] </pre> | |||
===Variables=== | |||
[[Variables|Main Article]] | |||
Now, you have all these different types of data, but where are you going to put them? We need something to hold the data in so we can keep track of it until later, otherwise it will just get lost in oblivion. Well that's why we have variables. Variables are a way of naming your data so that you can find the data whenever you want in your code. They can also be used to make your code a lot shorter, so you can just type a piece of data once and then just be able to type in a little word and all that data is right back at your fingertips. | |||
Now like I said, a variable is a name for your data, so you are going to make the name of a variable equal the data. To do that, we just use one equals sign. On the left of the equals sign you put the name of the variable, and on the right, you put the data. The data can be any of the types of data mentioned above, and more that I haven't talked about yet. | |||
<pre> Variable = 7 </pre> | |||
Now the variable "Variable" holds the data 7. Now, in our script, whenever we type Variable, we mean 7. A simple example of how you can use this to shorten data is with the number "pi". it is a number with millions of decimal places discovered, and still more being found. Obviously, our numbers can't hold millions of decimal points, but it can hold enough that it would be annoying to have to type it over and over again. We can use a variable to solve this problem! | |||
<pre> pi = 3.14159265358 </pre> | |||
There, now instead of having to type that long number out, we can just type the short word "pi". Mmmmm, pi... | |||
And then also with strings: | |||
<pre> str = "this is a really long string that would be annoying to type out every time you needed to access it instead of just being able to type 'str'." </pre> | |||
See? It works with other data types too, but we haven't gotten to that. | |||
===Tables=== | |||
[[Tables|Main Article]] | |||
Now, you have these simple data types, and you have variables to hold that data, but now, what if I want to store multiple pieces of data and only have to use one variable to do it? Well, obviously, since we are talking about this in a section about tables, those are what can do this. Tables are a type of Object in Lua, which just means that it is passed by reference instead of by value. We'll get to that later, first we want to learn the basics of tables. | |||
Tables are defined by a pair of braces. | |||
<pre> Table = { } </pre> | |||
If you can't see clearly, they're the curly things you get by pressing Shift + square bracket (first and second keys to the right of 'P' on a QWERTY keyboard). | |||
The table holds what are known as ''key-value pairs''. The value is any type of data (string, number, boolean, even tables!), and the key is usually either a number or a string. If you don't manually assign a key, then the keys are numbers representing the position of the value in the table (first value has a key of 1 unless you say otherwise, etc.) To list values in the table, you simply add values seperated by commas or semicolons. | |||
<pre> Table = { "Value", "Another value", true, 1.7, { "This is a value inside a table inside a table :D" } } </pre> | |||
The last value in that table is another table, which contains 1 value. To access one of the values, also known as ''indexing'' the table, you use a set of square brackets with the key inside them. Like I said, the first value has a key of 1, the second has a key of 2, etc. | |||
<pre> print( Table[4] ) </pre> | |||
That would print '1.7' to your output (assuming you have 'Table' defined as I did above). | |||
Now, you may be wondering why we didn't put quotation marks around the stuff inside the print. I said to do that earlier, right? Well, as you have now learned, quotation marks show that you are making a ''string'', so none of the stuff you write in the string is actually read by the script. We '''want''' the stuff inside the print to be read by the script, so we don't make it a literal string (literal strings are just strings that are defined explicitly: you aren't using a variable to refer to the string, you're actually putting the string there). | |||
So now, you might be wondering why the print function didn't error when we tryed to put a number into it. Well, print simply uses a function that turns anything into a string before it tries to put it into the output. It's quite simple, and we will get to that later. I promise. | |||
Now, I mentioned keys earlier, and how they can be both numbers and strings. Well how might you define a key as a string? How can you define a string at all, it seems that the script does it for you! | |||
To put a key, it's just like assigning a variable to a value. You just put the name of the variable (or the key), then an equals sign, then the value that you want to assign it to. | |||
<pre>Table = { Potato = 4, Tomato = "hi" } | |||
print(Table["Tomato"])</pre> | |||
Output: hi | |||
If you still are fuzzy on the concept of key-value pairs, here's a better way of looking at the table: | |||
<pre>Table = { | |||
Potato = 4, | |||
Tomato = "hi" | |||
}</pre> | |||
As you can see from that, Potato and Tomato are the keys, organized in one row, and 4 and "hi" are the values, organized in the other row. | |||
You can index string keys both by using the square brackets, or by simply adding a dot between the table name and the name of the key. | |||
<pre>print(Table.Potato)</pre> | |||
Output: 4 | |||
If you use a table inside the table (called a multidimensional table), you can do this multiple times, since indexing the table inside the table will give you another table to index. | |||
<pre>Something = { | |||
OtherTable = { Something = { "Cool" } } | |||
} | |||
print( Something["OtherTable"].Something[1] )</pre> | |||
Output: Cool | |||
As you can see, you can use both indexing methods at the same time. Or not. It's up to you. | |||
'''PROTIP''' If you want to set a number as a key, or a string with irregular characters (most punctuation characters), you must define it by surrounding it in square brackets ({ [Key] = Value }), and index it using square brackets as well (Table[Key], not Table.Key). | |||
====Reference vs. Value==== | |||
Now, earlier, I told you that tables were a type of object, which means that they get passed by reference. Here, I'll explain that: | |||
There are two ways for values to be passed: by reference and by value. When a value is passed by reference, that means that a variable referring to that value is really looking at where the data for that value is stored in the computer. When it is passed by value, a variable referring to the value will simply take a copy of the value. In Lua, all of the simple data types that we talked about earlier (strings, booleans, and numbers) are passed by value, and objects, which are more complex data types, are passed by reference. Here is a short example that shows this in action: | |||
<pre>Var1 = 4 -- this is a number, which is passed by value | |||
Var2 = Var1 -- making Var2 equal to the value that Var1 holds. Since it's a number, and is passed by value, Var2 is now just a copy of Var1, so Var2 = 4 | |||
Var1 = 7 -- now we change Var1 to 7, but Var2 will not change since the number was passed by value rather than reference | |||
print(Var1) | |||
print(Var2)</pre> | |||
Output: | |||
7 | |||
4 | |||
<pre>Tab1 = { "hi" } -- Tab1 is a table with its first index (Tab[1]) equal to "hi". | |||
Tab2 = Tab1 -- now, Tab2 refers to the table that Tab1 contains | |||
Tab1[1] = 4 -- change the first index in Tab1 from "hi" to 4 | |||
print(Tab1[1]) | |||
print(Tab2[1])</pre> | |||
Output: | |||
4 | |||
4 | |||
As you can see, the value in both tables changed because the table was passed by reference, so Tab1 and Tab2 both are the same table, not just one table and a clone of that table. |
Revision as of 21:15, 24 July 2011
Hello, welcome to crazypotato4's complete beginners' scripting guide! This guide is meant for beginners to learn all the basics of ROBLOX Lua. Make sure you read each word carefully, and study the guide, as if it were to help you study for the biggest test of the school year, because you don't want to miss anything. In most sections, there will be a link that says "Main Article". This link is just the main wiki page about the subject that the section is about. I suggest reading the version in this tutorial first, then if you really want to, you can check out the main page.
Introduction
What is Scripting?!?
The first thing you want to know is, what exactly is scripting? Scripting is a type of programming, which is just telling a computer what to do in a language that it understands. In scripting, instead of telling a computer what to do, you are telling an application, such as the ROBLOX game, what to do. Scripting is our way of communicating with the program that runs our games, which gives us endless possibilities.
On ROBLOX, we use a language called Lua to script. It is a very easy language to learn, and commonly used as a scripting language. The version that we use is called ROBLOX Lua, or RBX.Lua for short. RBX.Lua has many things added, which I won't cover until later, and it has a few things removed as well so that people can't use ROBLOX to hack your computer.
Getting Started
Now that you know what scripting is, you have to figure out where you're going to script. The ROBLOX script editor is in the ROBLOX Studio application. To open it, go to the Start Menu on your computer, and go to 'All Programs' (maybe just 'Programs' on older versions of Windows, and Mac users will have to figure it out yourselves). Scroll down to the ROBLOX folder, and click on the ROBLOX Studio application inside it. The studio browser should open up, and look something like this:
[ Studio.png image ]
Now, all those buttons may be intimidating to newer users, but it is all easy once you get the hang of it. The first thing you want to do is go to the Menu bar at the very top of the Studio window, and click on the 'View' button. In the drop-down menu, you will want to open up two things. First, you check the "Output", then, go to the Toolbars menu and check 'Command'. Your studio will now have a small bar at the bottom that you can type things into, and a large white panel taking up the bottom third of the game screen.
[ Studio2.png image ]
The two things that you just opened are going to be the tools that we use to start scripting. The output window is there to give us information on how scripts are running. If a script is broken, chances are, an error will show up in the output telling us what is wrong with our scripts, so we can fix them. The command bar is simply a place where we can type in code and press enter, then have the code instantly run, even if the game is paused.
Hello World!
The first script that most people learn when learning a language is a script that will write "Hello, World!" in the output. This has something to do with that being the first thing written on a computer or something, but I really don't care much to explain this. Anyways, back on topic. The way that we tell a script to write something in the output is with the print function. Don't worry, all of this stuff will be explained in due time. For now, all you need to know is that you write the word "print", followed by a set of parenthesis.
print()
In the parenthesis, we put whatever we want to print into the output. We want to write the words Hello, World! into the output, so we put that in the parenthesis, right? Let's try it. Try typing this into your command bar.
print(Hello, World!)
Now, if you were to run that, the output will yell confusing things at you with red text. That's how the output shows that there's an error, by telling you stuff in red text. Now why is that? Well, we can't just put words in our script, because the compiler, the code that makes the script run, will try to read it as coding instead of just plain words. To tell it that we want the Hello, World! part to be just words, we put it in quotation marks.
print("Hello, World!")
When you run that in the command bar, it will successfully output the words "Hello, World!" into your output. Now how is that useful at all? Well, for one thing, it teaches you something, but the practical use of knowing how to write things into the output using the print function lets you find errors in your script that the output doesn't tell you. Again, this will all be explained soon!
Commenting your Code
When you write code, you may want to type things that the script won't read. Maybe you want to leave a note to people on how to edit the script if you are putting a script on Free Models, or maybe you just want to make sure you can look at the script later and still know what it will do (trust me, that will be a problem if you make a long script and want to edit it after a few months or years), or maybe you even want to make part of your code not run, but not have to delete it. Well this is where comments come in. Comments simply tell the script not to look at part of what you type. You make comments by typing two dashes (minus signs):
Code here -- this is a comment, and will not be read by the script More code here
If you want to make a comment go for multiple lines, or maybe you just want to comment out part of a line, you write two dashes followed by two square opening brackets, then end it with two square closing brackets:
Code here --[[this is a comment, and won't be looked at by the script]] more code here --[[this is another comment as you can see, this comment goes multiple lines. :D]]more code here
Lesson 1: Data and variables
You may be thinking right now, "O NOES! DERE ARE CONFUUZIN WURDS IN DA PAGE, SCRIPTING ISH 2 HARD 4 MEH!!!1!!11!!one!". Well don't worry, this is all very easy to understand, just read this part slowly and carefully.
Booleans
The first data type that I will explain is the boolean type. Yes, the name is silly, and you probably haven't heard it before. Well, booleans are very simple, as they are just the values true and false. It's really as simple as that, true and false, yes and no, on and off. That's it. See, simple!
Numbers
Numbers are a more complex data type, but you have probably heard of numbers before. If you haven't, I suggest paying a lot more attention in school. There are actually two types of numbers in programming, although in Lua, only one of them is really used.
Integers
Integers are the more basic type of number. They are just all <whole> numbers, both positive and negative. In programming, there is a limit to how high a number can go, because only a certain amount of data is given to each number, so it can only take up that much data. It's as if the numbers (and all other data) are water and they are held in certain sizes of bottles, they can't have too much liquid or it will overflow. In programming, the integer type goes to a maximum of 2147483647 and can go all the way down to -2147483648. Don't worry, you shouldn't be working with numbers that big anytime soon. Plus, the scripting language that we use, Lua, doesn't use integers, they use the float type, which we will discuss next.
Floats
What exactly is a float? Why is it categorized under "Number"? We aren't talking about the big balloon cars at parades or anything, we are talking about numbers. Float is short for floating point single precision number. Don't worry, it's not complicated. Let's break it down.
floating point - That means that the decimal point can move around, so you can have your number be 2.4, then move the decimal to the left and change the number to something like 1.7309.
single precision - That is just saying that the number has single precision, so it loses its accuracy around six decimal places.
number - This should be obvious. If it needs explanation, then go ask your mom or your math teacher or someone. You can even do a Google search and get all sorts of stuff about programming and math folklore.
In Lua, all numbers are actually doubles, which are just like floats, but more accurate. There is no other number type in Lua. Yes, we just talked about an integer type, but that is just in general programming. Lua doesn't use integers.
Strings
This should be the first thing in this tutorial to actually make your brain work to understand it. The rest of it has been child's play.
Strings are just a bunch of characters strung together. But what are characters? They aren't the little people in your video games, they're just single letters, numbers, and anything else you can type on your keyboard. Even spaces. Even things that you can't type in on your keyboard. Everything is a character.
To create a string, you can do one of four things.
- Surround stuff with quotation marks (remember back in the Hello World part of this tutorial? That was a string!) Ex.
"This is a string"
- Surround stuff with apostrophes (single quote marks) Ex.
'This is also a string'
- Surround stuff with two brackets (the sqaure thingys). These strings can take up multiple lines. Ex.
[[This is a string. This is still a string :D ya im still a string. ujelly?]]
- Surround stuff with one bracket, then any amount of equals signs, then another bracket. The amount of equals signs on both sides of the string must match. This type of string can also contain multiple lines. Ex.
[===[This is an epic string :D]==]
The first method of making strings is the preferred one, since it colors the text pink when you type it in an actual script (which I surprisingly haven't shown how to insert). One reason that there are so many types of strings, though, is so that you can stack the symbols inside each other. For example:
"hi this is a string "this is supposed to be a quote mark inside the string, but instead it just ends the string, so now this is treated like real code"this is another string"
Instead of breaking your script by doing that, you can just use different kinds of strings.
[[this is a string "this is still in the first string, it didn't start a new string or end the first one :D" and this is all still a string]]
Variables
Now, you have all these different types of data, but where are you going to put them? We need something to hold the data in so we can keep track of it until later, otherwise it will just get lost in oblivion. Well that's why we have variables. Variables are a way of naming your data so that you can find the data whenever you want in your code. They can also be used to make your code a lot shorter, so you can just type a piece of data once and then just be able to type in a little word and all that data is right back at your fingertips.
Now like I said, a variable is a name for your data, so you are going to make the name of a variable equal the data. To do that, we just use one equals sign. On the left of the equals sign you put the name of the variable, and on the right, you put the data. The data can be any of the types of data mentioned above, and more that I haven't talked about yet.
Variable = 7
Now the variable "Variable" holds the data 7. Now, in our script, whenever we type Variable, we mean 7. A simple example of how you can use this to shorten data is with the number "pi". it is a number with millions of decimal places discovered, and still more being found. Obviously, our numbers can't hold millions of decimal points, but it can hold enough that it would be annoying to have to type it over and over again. We can use a variable to solve this problem!
pi = 3.14159265358
There, now instead of having to type that long number out, we can just type the short word "pi". Mmmmm, pi...
And then also with strings:
str = "this is a really long string that would be annoying to type out every time you needed to access it instead of just being able to type 'str'."
See? It works with other data types too, but we haven't gotten to that.
Tables
Now, you have these simple data types, and you have variables to hold that data, but now, what if I want to store multiple pieces of data and only have to use one variable to do it? Well, obviously, since we are talking about this in a section about tables, those are what can do this. Tables are a type of Object in Lua, which just means that it is passed by reference instead of by value. We'll get to that later, first we want to learn the basics of tables.
Tables are defined by a pair of braces.
Table = { }
If you can't see clearly, they're the curly things you get by pressing Shift + square bracket (first and second keys to the right of 'P' on a QWERTY keyboard).
The table holds what are known as key-value pairs. The value is any type of data (string, number, boolean, even tables!), and the key is usually either a number or a string. If you don't manually assign a key, then the keys are numbers representing the position of the value in the table (first value has a key of 1 unless you say otherwise, etc.) To list values in the table, you simply add values seperated by commas or semicolons.
Table = { "Value", "Another value", true, 1.7, { "This is a value inside a table inside a table :D" } }
The last value in that table is another table, which contains 1 value. To access one of the values, also known as indexing the table, you use a set of square brackets with the key inside them. Like I said, the first value has a key of 1, the second has a key of 2, etc.
print( Table[4] )
That would print '1.7' to your output (assuming you have 'Table' defined as I did above).
Now, you may be wondering why we didn't put quotation marks around the stuff inside the print. I said to do that earlier, right? Well, as you have now learned, quotation marks show that you are making a string, so none of the stuff you write in the string is actually read by the script. We want the stuff inside the print to be read by the script, so we don't make it a literal string (literal strings are just strings that are defined explicitly: you aren't using a variable to refer to the string, you're actually putting the string there).
So now, you might be wondering why the print function didn't error when we tryed to put a number into it. Well, print simply uses a function that turns anything into a string before it tries to put it into the output. It's quite simple, and we will get to that later. I promise.
Now, I mentioned keys earlier, and how they can be both numbers and strings. Well how might you define a key as a string? How can you define a string at all, it seems that the script does it for you!
To put a key, it's just like assigning a variable to a value. You just put the name of the variable (or the key), then an equals sign, then the value that you want to assign it to.
Table = { Potato = 4, Tomato = "hi" } print(Table["Tomato"])
Output: hi
If you still are fuzzy on the concept of key-value pairs, here's a better way of looking at the table:
Table = { Potato = 4, Tomato = "hi" }
As you can see from that, Potato and Tomato are the keys, organized in one row, and 4 and "hi" are the values, organized in the other row.
You can index string keys both by using the square brackets, or by simply adding a dot between the table name and the name of the key.
print(Table.Potato)
Output: 4
If you use a table inside the table (called a multidimensional table), you can do this multiple times, since indexing the table inside the table will give you another table to index.
Something = { OtherTable = { Something = { "Cool" } } } print( Something["OtherTable"].Something[1] )
Output: Cool
As you can see, you can use both indexing methods at the same time. Or not. It's up to you.
PROTIP If you want to set a number as a key, or a string with irregular characters (most punctuation characters), you must define it by surrounding it in square brackets ({ [Key] = Value }), and index it using square brackets as well (Table[Key], not Table.Key).
Reference vs. Value
Now, earlier, I told you that tables were a type of object, which means that they get passed by reference. Here, I'll explain that:
There are two ways for values to be passed: by reference and by value. When a value is passed by reference, that means that a variable referring to that value is really looking at where the data for that value is stored in the computer. When it is passed by value, a variable referring to the value will simply take a copy of the value. In Lua, all of the simple data types that we talked about earlier (strings, booleans, and numbers) are passed by value, and objects, which are more complex data types, are passed by reference. Here is a short example that shows this in action:
Var1 = 4 -- this is a number, which is passed by value Var2 = Var1 -- making Var2 equal to the value that Var1 holds. Since it's a number, and is passed by value, Var2 is now just a copy of Var1, so Var2 = 4 Var1 = 7 -- now we change Var1 to 7, but Var2 will not change since the number was passed by value rather than reference print(Var1) print(Var2)
Output: 7 4
Tab1 = { "hi" } -- Tab1 is a table with its first index (Tab[1]) equal to "hi". Tab2 = Tab1 -- now, Tab2 refers to the table that Tab1 contains Tab1[1] = 4 -- change the first index in Tab1 from "hi" to 4 print(Tab1[1]) print(Tab2[1])
Output: 4 4
As you can see, the value in both tables changed because the table was passed by reference, so Tab1 and Tab2 both are the same table, not just one table and a clone of that table.