User:JulienDethurens/Guide/Chapter 1

From Legacy Roblox Wiki
Revision as of 01:52, 26 January 2012 by >JulienDethurens (→‎Variables)
Jump to navigationJump to search

Note: considering we are not yet in the ROBLOX-specific section, you can use the Lua demo, on the official Lua website. You can use it to run code. However, note that it uses Lua 5.2 and that ROBLOX uses Lua 5.1. Therefore, some of the things I will teach you will not work with it. Don't worry, though, almost everything still works in Lua 5.2. And I'll try to tell you whenever something I am talking about is no longer available in Lua 5.2.

I suggest using the Lua demo to test code, as it avoids you the trouble of starting the studio and everything. But you can also use the studio if you want.

Syntax

First of all, what is syntax? What does that word mean? Well, syntax is actually like grammar, but for a programming language. It explains how you must structure your scripts.

ROBLOX Lua uses exactly the same syntax as Lua. Only functions were edited and added, as well as removed, but the syntax is exactly the same. You can find the syntax of Lua in extended BNF here, except I promise you won't understand anything: The Complete Syntax of Lua.

Don't worry, though, you're not going to have to understand any of that. It's extended BNF, which doesn't really have anything to do with what you'll need to learn.

Names

Names, in Lua, are also called identifiers. They can be any text composed of letters, digits, and underscores and not beginning with a digit. They are used to name variables and table fields (you'll know what both of these are later).

Here are some valid names:

name
hello
_
_tomatoes
me41
_thisIs_StillaValid23name

Here are some invalid names:

2hello
th$i
hel!o
563text
82_something

Also, the following keywords are reserved by Lua and can not be used as names (oh, and you'll notice that they're all colored in yellow, they will be colored the exact same way in all of this guide):

and break do else elseif end false for function if in local nil not or repeat return then true until while

When naming a variable or a table field, you must choose a valid name for it. It must therefore start with a letter or an underscore and only contain letters, underscores and digits.

You don't know yet what a variable and a table field are, but don't worry, I'll explain that soon. However, first, I'll talk about comments, then, about the different types of data.

Note: Lua is case sensitive. This means that 'Hello' and 'hello' are two different names.

Comments

I'm talking about these right now because I'll use them soon, so I want you to know what they are.

A comment is simply text that is ignored by Lua. You can use comments to describe one or many lines of code, or for any reason you want to.

A comment starts with a double hyphen, like this:

-- This is a comment!

Usually, I tend to put a space between the hyphen and the text, but it's just a preference. You can do this too:

--This is a comment!

And you can put the comment on the end of a line of code too:

print 'Hello world!' -- This is a comment that was put after a line of code!

These are simple comments. They only continue until the next line. However, sometimes, you might want to make a comment that continues on many lines. You can do this with a long comment.

A long comment starts with a long bracket. Here is an example:

--[[ This is a comment This is still in the comment This is in the comment too! ]]

Sometimes, you might want to use comments to disable code temporarily (because, remember, Lua ignores everything that is a comment and doesn't run them). In a such case, it is possible that you run in the following problem:

--[[ variable = this is a string, and the other thing is a variable, but you don't need to know about that yet ]]

Don't worry about the code, it is just an example. The point is that double brackets can also be used for other things than long comments, which will make the comment stop. But there is a simple solution:

--[===[ This is another long comment and you can still put text on any line. ]===]

As you can see, the comment is not colored here. That's because I disabled the syntax highlighting on this one, because the syntax highlighting doesn't understand this type of long comment. Trust me, though, they work.

Just as a note, you can put any number of equal signs between the two opening and closing brackets. Just make sure both the opening and the closing brackets have the same number of equal signs.

I will use comments a lot in this guide, so don't be surprised if you see one.

Types

You must know that there are simple data types, and more complex data types. Here, I am only going to talk about data types you can find in Lua, simple Lua. I'll talk again about data types later, in the ROBLOX API section, to explain the ROBLOX-specific data types.

Numbers

You probably already know what a number is. A number is a value composed of digits that represents a quantity. That's true in Lua too. And I won't have much to explain, as they work the same way as the numbers you already know.

In Lua, you can create numbers the same way as the way you learnt in school. In fact, you can use decimal points, decimal exponents and you can even write your numbers in hexadecimal, if you prefix them with 0x (hexadecimal is the base 16, it is a different way to express numbers. If you don't know about it, don't worry, you don't need to know about it to learn Lua).

Here are some valid numbers:

3 3.0 3.1416 314.16e-2 0.31416E1 0xff 0x56

You might not know what some of these are, but that doesn't matter.

Integers

Integers are not a type you will really have to worry about in Lua, but it's still good to know about it.

Integers are exactly like the integers you've learnt about at school (if you have. If not, I'll explain anyways). They are exactly like numbers, except they can't be decimal. In fact, you can't encounter integers in pure Lua, but you can in ROBLOX, because of IntValues. I don't know if you've already seen someone, at a tycoon, for example, have so much money that it turned negative. The reason that happens is because integers are limited to 2^31-1, which is equal to 2147483647. If a player's score is higher than that, it will turn negative, as the values on the leaderboard are IntValues. Usually, you won't run in problems because of this, because Lua doesn't use integers, but if you have to use IntValues, just try to stay under 2147483647. However, it shouldn't be a problem if your game is well-thought, because if a player gets 2 billion points, it's probably because your game makes it too easy to get points...

Strings

Strings are simple to understand, they are text. Yes, text. A string can contain any form of text.

Strings can be delimited by single or double quotes ('' and "", respectively). They can contain the following escape characters (and some others that I won't bother mentionning):

Escape Characters
Character Description
\n A newline.
\" A double quote (")
\' A single quote (or apostrophe) (')
\\ A backslash (\)
\t An horizontal tab
\[number] [number] must be a number from 0 to 255. The result will be the ASCII character with the corresponding code.

You can use these in strings to reproduce characters that, if put in the string under their normal form, might cause a problem.

Here is an example of using the escaped characters:

"this is a string with \" quotes and \n a new line, as well as some backslashes \\"

Strings can also be defined using a long format enclosed by long brackets. We define an opening long bracket of level n as an opening square bracket followed by n equal signs followed by another opening square bracket. So, an opening long bracket of level 0 is written as [[, an opening long bracket of level 1 is written as [=[, and so on. A closing long bracket is defined similarly; for instance, a closing long bracket of level 4 is written as ]====]. A long string starts with an opening long bracket of any level and ends at the first closing long bracket of the same level. Strings in this bracketed form can run for several lines, do not interpret any escape sequences, and ignore long brackets of any other level. They can contain anything except a closing bracket of the proper level.

For convenience, when the opening long bracket is immediately followed by a newline, the newline is not included in the string.

Here is an example of a multiline string:

[[this is a multiline string! it can continue on many lines and it can contain quotes like this: " and any other character it can even contain something like this \n and it won't be converted to a new line it can even contain a comment like this: -- and that comment will still get inserted in the string it stops when it encounters closing double brackets like this: ]]

As you can see, there is no syntax highlighting here either. I disabled it, because, once more, the syntax highlighting doesn't recognize them.

[...]

Variables

I've talked about variables not long ago. But, what are variables? Well, variables are used to store data. Each variable has its own name. Since Lua is a dynamically typed (which means that values have a type, but that variables don't), you can store any value in a variable, even if its type doesn't correspond with the previous value.

To store a value in a variable, you use the '=' sign. Here is an example:

variable = 5

This will set the value of variable to 5.