User:JulienDethurens/Guide/Chapter 1: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>JulienDethurens
Created page with "Note: considering we are not yet in the ROBLOX-specific section, you can use the [http://www.lua.org/demo.html Lua demo], on the [http://www.lua.org official Lua website]. You..."
 
>JulienDethurens
No edit summary
Line 45: Line 45:


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.
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.
==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:
<code lua>
-- This is a comment!
</code>
Usually, I tend to put a space between the hyphen and the text, but it's just a preference. You can do this too:
<code lua>
--This is a comment!
</code>
And you can put the comment on the end of a line of code too:
<code lua>
print 'Hello world!' -- This is a comment that was put after a line of code!
</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:
<code lua>
--[[ This is a comment
This is still in the comment
This is in the comment too!
]]
</code>
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:
<code lua>
--[[
variable = [[this is a string, and the other thing is a variable, but you don't need to know about that yet]]
]]
</code>
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. But there is a simple solution:
<code>
--[===[
This is another long comment
and you can still put text on any line.
]===]
</code>
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.
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:
<code>
3
3.0
3.1416
314.16e-2
0.31416E1
0xff
0x56
</code>
You might not know what some of these are, but that doesn't matter.
==='''[...]'''===
==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:
<code lua>
variable = 5
</code>
This will set the value of 'variable' to 5.

Revision as of 19:27, 22 January 2012

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:

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.

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. 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.

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.

[...]

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.