The Basics of LUA
Operation Flashpoint Dragon Rising OFDR Forums :: OPERATION FLASHPOINT: DRAGON RISING :: OFDR EDITING :: EDITING Q+A
Page 1 of 1
The Basics of LUA
The coding language for the OFDR Editor is LUA. This thread is meant to inform new coders of the most basic elements of lua in an attempt to give you an understanding of the basic terminology and principals used in coding a mission for OFDR.
Guest- Guest
LUA Vocabulary
First things first. If we're going to have a coherent discussion about the lua language, we better learn the meaning of lua words. There are a few things to get out of the way first before we can begin to understand each other.
Numbers
Numbers are one of the easiest to understand data types, as we use them in math class every day in school. A number is defined as any numerical value. It can be a decimal, it can be whole, it can be negative, it can be positive, it can be anything you think up of that involves numerical characters. Here are some good examples of numbers:
•0.5
•-96
•104467
•-0.01
•0
•-2874
All of those are classified as numbers by the compiler and will be executed as such (except for a few, which are integers).
Integers
Integers are like numbers except for the fact that they cannot include decimals. This means that they must be whole. Integers generally take up less memory than numbers do, and if a numerical value has no decimal inside of it, it will be read as an integer. Also, Integers may be negative or positive. Following this are a list of examples of integers:
•-5
•300
•-65
•23
That's pretty much all there is for integers. Just a more basic type of number.
Booleans
A boolean is a true or false value. That's it. True or false, on and off, yes or no. I personally think booleans are the simplest of all the data types, as easy as they are. But there's one other thing you have to know. The compiler interprets anything that is nil (to be explained later) and false as false. Anything else is equal to true. So here's a little example of what I mean:
•nil --> false, because anything that's nil is false.
•true --> true, because true is obviously true.
•false --> false, because false is obviously false.
For now, just memorize this, as it can make your code much more efficient later on.
Strings
A string is a data type that holds a series of characters, numbers, and symbols. Strings are enclosed in double quotation marks ("") or single quotation marks (''). Most programmers, though, use double quotes because it's traditional. A few examples of strings can be found below:
•"I am a string. Here are some symbols: !@#$%^&*()"
•""
•"12345"
All of those are valid strings in Lua. Notice the third string, though. It has an integer inside of it!
You can concatenate (combine) strings by using two consecutive periods (..). This is called an operator, which I'll explain in greater detail in a later post.
•"Today is a " .. "great day!"
That string would now be "Today is a great day!" Now, why couldn't we just have done this?
•"Today is a great day!"
The answer to that is that I'm showing you this for learning's sake, and there are a number of times where this fits in just right.
When using the two consecutive periods as shown in the first string example, try to have a space inbetween each one. Why? This is because when you try to concatenate two numbers (not in a string) your code will error. Saying this:
11..11
Will not combine eleven and eleven, creating one thousand one hundred and eleven, but rather think that there's a decimal point after the first number. No decimal point numbers are ever followed by another decimal. However, doing this:
11 .. 11
Will result in one thousand one hundred and eleven, giving you the desired results. Hooray! This is a great habit to get into, and I encourage you do this whenever you involve the .. operator (often called whitespacing).
Nil
The definition of nil isn't too hard a concept to grasp: nothingness, the absence of data, no value, and so on. Nil is good for destroying objects you have in your game, and creating variables in the global scope (don't worry too much about the second one, we'll get to it in time).
Math
Doing math in Lua is very easy. You can do math like this:
print(4+4)
Note: We don't put the math inside quotations, because we want it to actually execute the equation.
Here's a list of all of the mathematical operators you can use in Lua:
+ — Addition — 4+4=8
- — Subtraction — 8-4=4
* — Multiplication — 5*5=25
/ — Division — 81/9=9
- — Unary Negation — (-45)
% — Modulation — 10%3=1
^ — Exponentiation — 7^3=343
Variables
A variable is something that holds a value. They can be called whatever you like (with a few exceptions). Here is an example of using variables: (Note the >> indicates what would be seen in the Output window. It is not actual code.)
Comments
A comment in Lua is something that allows you to write things in your code, but the computer won't execute it as code. To do so, put two hyphens (--) in front of your comment.
This should cover enough of the very basics. It is imperative that you understand the terminology above if you intend to create a mission. Some amount of script is going to be necessary, so building a lua vocabulary will take you a long way in understanding. It will help you understand other peoples answers when you ask questions or see discussions about the code.
More to come!!
Cheers
Numbers
Numbers are one of the easiest to understand data types, as we use them in math class every day in school. A number is defined as any numerical value. It can be a decimal, it can be whole, it can be negative, it can be positive, it can be anything you think up of that involves numerical characters. Here are some good examples of numbers:
•0.5
•-96
•104467
•-0.01
•0
•-2874
All of those are classified as numbers by the compiler and will be executed as such (except for a few, which are integers).
Integers
Integers are like numbers except for the fact that they cannot include decimals. This means that they must be whole. Integers generally take up less memory than numbers do, and if a numerical value has no decimal inside of it, it will be read as an integer. Also, Integers may be negative or positive. Following this are a list of examples of integers:
•-5
•300
•-65
•23
That's pretty much all there is for integers. Just a more basic type of number.
Booleans
A boolean is a true or false value. That's it. True or false, on and off, yes or no. I personally think booleans are the simplest of all the data types, as easy as they are. But there's one other thing you have to know. The compiler interprets anything that is nil (to be explained later) and false as false. Anything else is equal to true. So here's a little example of what I mean:
•nil --> false, because anything that's nil is false.
•true --> true, because true is obviously true.
•false --> false, because false is obviously false.
For now, just memorize this, as it can make your code much more efficient later on.
Strings
A string is a data type that holds a series of characters, numbers, and symbols. Strings are enclosed in double quotation marks ("") or single quotation marks (''). Most programmers, though, use double quotes because it's traditional. A few examples of strings can be found below:
•"I am a string. Here are some symbols: !@#$%^&*()"
•""
•"12345"
All of those are valid strings in Lua. Notice the third string, though. It has an integer inside of it!
You can concatenate (combine) strings by using two consecutive periods (..). This is called an operator, which I'll explain in greater detail in a later post.
•"Today is a " .. "great day!"
That string would now be "Today is a great day!" Now, why couldn't we just have done this?
•"Today is a great day!"
The answer to that is that I'm showing you this for learning's sake, and there are a number of times where this fits in just right.
When using the two consecutive periods as shown in the first string example, try to have a space inbetween each one. Why? This is because when you try to concatenate two numbers (not in a string) your code will error. Saying this:
11..11
Will not combine eleven and eleven, creating one thousand one hundred and eleven, but rather think that there's a decimal point after the first number. No decimal point numbers are ever followed by another decimal. However, doing this:
11 .. 11
Will result in one thousand one hundred and eleven, giving you the desired results. Hooray! This is a great habit to get into, and I encourage you do this whenever you involve the .. operator (often called whitespacing).
Nil
The definition of nil isn't too hard a concept to grasp: nothingness, the absence of data, no value, and so on. Nil is good for destroying objects you have in your game, and creating variables in the global scope (don't worry too much about the second one, we'll get to it in time).
Math
Doing math in Lua is very easy. You can do math like this:
print(4+4)
Note: We don't put the math inside quotations, because we want it to actually execute the equation.
Here's a list of all of the mathematical operators you can use in Lua:
+ — Addition — 4+4=8
- — Subtraction — 8-4=4
* — Multiplication — 5*5=25
/ — Division — 81/9=9
- — Unary Negation — (-45)
% — Modulation — 10%3=1
^ — Exponentiation — 7^3=343
Variables
A variable is something that holds a value. They can be called whatever you like (with a few exceptions). Here is an example of using variables: (Note the >> indicates what would be seen in the Output window. It is not actual code.)
- Code:
a = 1
print(a)
>> 1
b=5*2
print(b)
>> 10
hi=3
bye=2
print(hi*bye)
>> 6
lol=4
print(lol*2)
>> 8
asdf="Hello, World!"
print(asdf)
>> Hello, World!
Comments
A comment in Lua is something that allows you to write things in your code, but the computer won't execute it as code. To do so, put two hyphens (--) in front of your comment.
- Code:
--This is a Lua comment.
print(1+1) --This will print what 1+1 is
>> 2
--This won't do anything: print(1+1)
>>
This should cover enough of the very basics. It is imperative that you understand the terminology above if you intend to create a mission. Some amount of script is going to be necessary, so building a lua vocabulary will take you a long way in understanding. It will help you understand other peoples answers when you ask questions or see discussions about the code.
More to come!!
Cheers
Guest- Guest
Re: The Basics of LUA
RockCop wrote:Am I glad to see this again!!!
I rushed this up as fast as I could with you in mind. I'll try to expand on this as time goes on.
Cheers
Guest- Guest
Re: The Basics of LUA
Hello again!
This time lets talk a little bit about coding and how to make it work for you. Coding is easier done when you use a visual indicator to alert you to the beginnings and ends of code statements. A piece of code is often refered to as a "chunk" of code. It is good practice to indent all of the code inside of any given chunk. It is also important to understand that you can and often will have chunks inside of chunks. You will often hear this refered to as nesting... one inside the other. Each time you have a new chunk, maintain the same practice of indenting the code inside of the chunk. Here are a couple of examples of chunks of code.
I plan to tackle the if .. then statement and operators in the next volume. Stay tuned!
Happy coding
This time lets talk a little bit about coding and how to make it work for you. Coding is easier done when you use a visual indicator to alert you to the beginnings and ends of code statements. A piece of code is often refered to as a "chunk" of code. It is good practice to indent all of the code inside of any given chunk. It is also important to understand that you can and often will have chunks inside of chunks. You will often hear this refered to as nesting... one inside the other. Each time you have a new chunk, maintain the same practice of indenting the code inside of the chunk. Here are a couple of examples of chunks of code.
- Code:
if a + b == c then --this is the beginning of a chunk
OFP:displaySystemMessage("this code is indented") --notice how the code inside of the chunk is indented
end --this is the end of the chunk
if c == 1 then --this is also the beginning of a chunk
if d == c then -- this is the beginning of a new chunk nested inside of a chunk. Note the indentation of this line indicating it's inside the first chunk
OFP:displaySystemMessage("this is part of the nested chunk") --note the further indentation of this line indicating it's inside the nested chunk
end -- this closes the nested chunk --we indent this the same as the beginning of the chunk so we can see the beginning and end by just looking
OFP:displaySystemMessage("this is still part of the first chunk") -- this is part of the first chunk thus indented the same as the nested chunk
end --this is the close of the first chunk
I plan to tackle the if .. then statement and operators in the next volume. Stay tuned!
Happy coding
Guest- Guest
Operation Flashpoint Dragon Rising OFDR Forums :: OPERATION FLASHPOINT: DRAGON RISING :: OFDR EDITING :: EDITING Q+A
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|